diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2022-12-08 00:33:23 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2022-12-08 00:33:23 -0800 |
| commit | 44c45eb9c40e8e0cc4b598851a6577b8da26700d (patch) | |
| tree | 22ef3ccc9c354739271226c81df470cc2d424add | |
| parent | 26dff288134041d3245701b047be1d9a08c7c9d3 (diff) | |
started migration to board state as struct
| -rw-r--r-- | chess.c | 27 | ||||
| -rw-r--r-- | chesshelpers.c | 24 | ||||
| -rw-r--r-- | chesshelpers.h | 8 |
3 files changed, 34 insertions, 25 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("; "); diff --git a/chesshelpers.c b/chesshelpers.c index 7a8178b..a471758 100644 --- a/chesshelpers.c +++ b/chesshelpers.c @@ -4,6 +4,20 @@ #include "vector.h" #include "chesshelpers.h" +board_t init_board(int** pieces) { + board_t board; + board.pieces = pieces; + board.blackKingHasMoved = 0; + board.whiteKingHasMoved = 0; + board.lastPawnMove = 0; + return board; +} +//function applies side effects to board values like king move state +board_t make_move(board_t board, vector_t origin, vector_t end) { + int piece = board.pieces[origin.mx][origin.my]; + board.pieces[origin.mx][origin.my] = 0; + board.pieces[end.mx][end.my] = piece; +} int in_board(vector_t position) { return position.mx < 8 && position.mx >= 0 && position.my < 8 && position.my >= 0; } @@ -60,13 +74,3 @@ char* get_move_name(int** board, vector_t origin, vector_t end) { return output; } } -int** clone_board(int** board) { - int** newBoard = malloc(8 * sizeof(int*)); - for(int r = 0; r < 8; r++) { - newBoard[r] = malloc(8 * sizeof(int)); - for(int c = 0; c < 8; c++) { - newBoard[r][c] = board[r][c]; - } - } - return newBoard; -} diff --git a/chesshelpers.h b/chesshelpers.h index 55a211a..523505a 100644 --- a/chesshelpers.h +++ b/chesshelpers.h @@ -9,10 +9,16 @@ typedef enum { queen = 5, king = 6, } piece_t; +typedef struct BoardState { + int** pieces; + int blackKingHasMoved; + int whiteKingHasMoved; + int lastPawnMove; +} board_t; +board_t init_board(int** board); int in_board(vector_t position); int is_empty(int** board, vector_t position); int is_color(int** board, vector_t position, int color); char* get_readable_coords(vector_t position); char* get_move_name(int** board, vector_t origin, vector_t end); -int** clone_board(int** board); #endif |
