From 40667c9adde27cebdc8bbe9a6ab60f231bc2dfd7 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Thu, 8 Dec 2022 22:14:17 -0800 Subject: finished migration to board state struct --- chesshelpers.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'chesshelpers.c') diff --git a/chesshelpers.c b/chesshelpers.c index a471758..1b28ed3 100644 --- a/chesshelpers.c +++ b/chesshelpers.c @@ -17,19 +17,35 @@ 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; + return board; +} +board_t clone_board(board_t board) { + board_t newBoard; + newBoard.blackKingHasMoved = board.blackKingHasMoved; + newBoard.whiteKingHasMoved = board.whiteKingHasMoved; + newBoard.lastPawnMove = board.lastPawnMove; + int** pieces = malloc(8 * sizeof(int*)); + for(int r = 0; r < 8; r++) { + pieces[r] = malloc(8 * sizeof(int)); + for(int c = 0; c < 8; c++) { + pieces[r][c] = board.pieces[r][c]; + } + } + newBoard.pieces = pieces; + return newBoard; } int in_board(vector_t position) { return position.mx < 8 && position.mx >= 0 && position.my < 8 && position.my >= 0; } -int is_empty(int** board, vector_t position) { - return board[position.mx][position.my] == 0; +int is_empty(board_t board, vector_t position) { + return board.pieces[position.mx][position.my] == 0; } -int is_color(int** board, vector_t position, int color) { +int is_color(board_t board, vector_t position, int color) { if(color == 0) { - return board[position.mx][position.my] >= 1 && board[position.mx][position.my] <= 6; + return board.pieces[position.mx][position.my] >= 1 && board.pieces[position.mx][position.my] <= 6; } else { - return board[position.mx][position.my] >= 7; + return board.pieces[position.mx][position.my] >= 7; } } //converts board coordinates into algebraic notation @@ -41,14 +57,14 @@ char* get_readable_coords(vector_t position) { return output; } //gets the proper algebraic notation for the move -char* get_move_name(int** board, vector_t origin, vector_t end) { - int piece = board[origin.mx][origin.my]; +char* get_move_name(board_t board, vector_t origin, vector_t end) { + int piece = board.pieces[origin.mx][origin.my]; if(piece > 6) { piece -= 6; } if(piece != 1) { char cPiece = piece == pawn ? 'P' : piece == knight ? 'N' : piece == bishop ? 'B' : piece == rook ? 'R' : piece == queen ? 'Q' : 'K'; - int isCapture = board[end.mx][end.my] != 0; + int isCapture = board.pieces[end.mx][end.my] != 0; char* output = calloc(isCapture ? 5 : 4, sizeof(char)); output[0] = cPiece; if(isCapture) { @@ -61,7 +77,7 @@ char* get_move_name(int** board, vector_t origin, vector_t end) { return output; } else { - int isCapture = board[end.mx][end.my] != 0; + int isCapture = board.pieces[end.mx][end.my] != 0; char* output = calloc(isCapture ? 5 : 3, sizeof(char)); if(isCapture) { output[0] = 'a' + origin.mx; -- cgit v1.3.1