diff options
Diffstat (limited to 'chess.c')
| -rw-r--r-- | chess.c | 27 |
1 files changed, 13 insertions, 14 deletions
@@ -248,7 +248,7 @@ int is_in_check(int** board, int color) { return returnV; } //this function removes moves that place you in check -vectorp_t get_moves_final(int** board, vector_t position, piece_t piece) { +vectorp_t get_moves_final(board_t board, vector_t position, piece_t piece) { int color = 0; if(piece > 6) { color = 1; @@ -258,15 +258,13 @@ vectorp_t get_moves_final(int** board, vector_t position, piece_t piece) { int* invalidMoves = malloc(moves.length * sizeof(int)); int invalidMovesCount = 0; for(int i = 0; i < moves.length; i++) { - int** tempBoard = clone_board(board); - tempBoard[moves.vectors[i].mx][moves.vectors[i].my] = piece; - //remove the piece from where it was - tempBoard[position.mx][position.my] = 0; - if(is_in_check(tempBoard, color)) { + //TODO: fix this! make_move function modifies pointer + board_t tempBoard = make_move(board, position, moves[i]); + if(is_in_check(tempBoard.pieces, color)) { invalidMoves[invalidMovesCount] = i; invalidMovesCount++; } - free(tempBoard); + free(tempBoard.pieces); } vectorp_t finalMoves = remove_vectors(moves, invalidMoves, invalidMovesCount); free(invalidMoves); @@ -286,26 +284,27 @@ int main() { { rook + 6, pawn + 6, 0, 0, 0, 0, pawn, rook } }; //convert the array to pointers - int** board = malloc(8 * sizeof(int*)); + int** pieces = malloc(8 * sizeof(int*)); for(int r = 0; r < 8; r++) { - board[r] = malloc(8 * sizeof(int)); + pieces[r] = malloc(8 * sizeof(int)); for(int c = 0; c < 8; c++) { - board[r][c] = boardA[r][c]; + pieces[r][c] = boardA[r][c]; } } + board_t board = init_board(pieces); int turn = 0; while(1) { //draw the board, write some info clear_screen(); - draw_board(board); + draw_board(board.pieces); printf(turn == 0 ? "\nWhite's turn\n" : "\nBlack's turn\n"); - if(is_in_check(board, turn)) { + if(is_in_check(board.pieces, turn)) { printf("You're in check!\n"); } printf("Enter the coordinates of the piece you want to move\n"); //get the selection for the piece to be moved vector_t selection = user_input_pos(); - int selectedPiece = board[selection.mx][selection.my]; + int selectedPiece = board.pieces[selection.mx][selection.my]; //check if the piece is of the right colour, and that there is a piece there if(selectedPiece == 0 || (selectedPiece > 6 && turn == 0) || (selectedPiece <= 6 && turn == 1)) { warn_user("Invalid selection!"); @@ -322,7 +321,7 @@ int main() { printf("Valid moves: "); //write all the valid moves for(int i = 0; i < moves.length; i++) { - char* readable_coords = get_move_name(board, selection, moves.vectors[i]); + char* readable_coords = get_move_name(board.pieces, selection, moves.vectors[i]); printf(readable_coords); free(readable_coords); printf("; "); |
