From c57fdac8fb91a29205bda4a8d8a6f91f4c1ec172 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Wed, 7 Dec 2022 15:20:01 -0800 Subject: fixed check detection. changed all board[8][8] arguments to int** --- chess.c | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'chess.c') diff --git a/chess.c b/chess.c index c48315c..099c78c 100644 --- a/chess.c +++ b/chess.c @@ -5,7 +5,7 @@ #include "console.h" #include "chesshelpers.h" -vectorp_t get_pawn_moves(int board[8][8], vector_t position, int color) { +vectorp_t get_pawn_moves(int** board, vector_t position, int color) { vector_t* moves = malloc(6 * sizeof(vector_t)); int moveCount = 0; int forwardDir = (color == 0 ? -1 : 1); @@ -33,7 +33,7 @@ vectorp_t get_pawn_moves(int board[8][8], vector_t position, int color) { } return init_vectorp(moves, moveCount); } -vectorp_t get_knight_moves(int board[8][8], vector_t position, int color) { +vectorp_t get_knight_moves(int** board, vector_t position, int color) { vector_t* moves = malloc(8 * sizeof(vector_t)); int moveCount = 0; for(int i = 0; i < 8; i++) { @@ -47,7 +47,7 @@ vectorp_t get_knight_moves(int board[8][8], vector_t position, int color) { } return init_vectorp(moves, moveCount); } -vectorp_t get_bishop_moves(int board[8][8], vector_t position, int color) { +vectorp_t get_bishop_moves(int** board, vector_t position, int color) { vector_t* moves = malloc(14 * sizeof(vector_t)); int moveCount = 0; for(int i = 0; i < 4; i++) { @@ -78,7 +78,7 @@ vectorp_t get_bishop_moves(int board[8][8], vector_t position, int color) { } return init_vectorp(moves, moveCount); } -vectorp_t get_rook_moves(int board[8][8], vector_t position, int color) { +vectorp_t get_rook_moves(int** board, vector_t position, int color) { vector_t* moves = malloc(14 * sizeof(vector_t)); int moveCount = 0; for(int i = 0; i < 4; i++) { @@ -109,7 +109,7 @@ vectorp_t get_rook_moves(int board[8][8], vector_t position, int color) { } return init_vectorp(moves, moveCount); } -vectorp_t get_queen_moves(int board[8][8], vector_t position, int color) { +vectorp_t get_queen_moves(int** board, vector_t position, int color) { vector_t* moves = malloc(28 * sizeof(vector_t)); int moveCount = 0; for(int i = 0; i < 8; i++) { @@ -151,7 +151,7 @@ vectorp_t get_queen_moves(int board[8][8], vector_t position, int color) { return init_vectorp(moves, moveCount); } //this function returns moves that will place the player in check! -vectorp_t get_moves(int board[8][8], vector_t position, piece_t piece) { +vectorp_t get_moves(int** board, vector_t position, piece_t piece) { int color = 0; if(piece > 6) { color = 1; @@ -172,9 +172,12 @@ vectorp_t get_moves(int board[8][8], vector_t position, piece_t piece) { else if(piece == queen) { return get_queen_moves(board, position, color); } + else { + return init_vectorp(0, 0); + } } //array of arrays representing all the moves each piece of color can make -vectorpp_t get_all_moves(int board[8][8], int color) { +vectorpp_t get_all_moves(int** board, int color) { vectorp_t* allMoves = malloc(16 * sizeof(vectorp_t)); int pieceCount = 0; for(int r = 0; r < 8; r++) { @@ -190,7 +193,7 @@ vectorpp_t get_all_moves(int board[8][8], int color) { return init_vectorpp(allMoves, pieceCount); } //returns the position of color's king -vector_t get_king_position(int board[8][8], int color) { +vector_t get_king_position(int** board, int color) { for(int r = 0; r < 8; r++) { for(int c = 0; c < 8; c++) { if(board[c][r] == (king + color * 6)) { @@ -200,7 +203,7 @@ vector_t get_king_position(int board[8][8], int color) { } } //returns true if the player of color is currently in check -int is_in_check(int board[8][8], int color) { +int is_in_check(int** board, int color) { int returnV = 0; vector_t kingPos = get_king_position(board, color); //get all the moves for the opponent @@ -217,8 +220,8 @@ int is_in_check(int board[8][8], int color) { free(allMoves.vectorps); return returnV; } -//this function removes ALL illegal moves -vectorp_t get_moves_final(int board[8][8], vector_t position, piece_t piece) { +//this function removes moves that place you in check +vectorp_t get_moves_final(int** board, vector_t position, piece_t piece) { int color = 0; if(piece > 6) { color = 1; @@ -229,7 +232,9 @@ vectorp_t get_moves_final(int board[8][8], vector_t position, piece_t piece) { int invalidMovesCount = 0; for(int i = 0; i < moves.length; i++) { int** tempBoard = clone_board(board); - board[moves.vectors[i].mx][moves.vectors[i].my] = piece; + 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)) { invalidMoves[invalidMovesCount]; invalidMovesCount++; @@ -242,7 +247,8 @@ vectorp_t get_moves_final(int board[8][8], vector_t position, piece_t piece) { return finalMoves; } int main() { - int board[8][8] = { + //set up the board + int boardA[8][8] = { { rook + 6, pawn + 6, 0, 0, 0, 0, pawn, rook }, { knight + 6, pawn + 6, 0, 0, 0, 0, pawn, knight }, { bishop + 6, pawn + 6, 0, 0, 0, 0, pawn, bishop }, @@ -252,6 +258,14 @@ int main() { { knight + 6, pawn + 6, 0, 0, 0, 0, pawn, knight }, { rook + 6, pawn + 6, 0, 0, 0, 0, pawn, rook } }; + //convert the array to pointers + int** board = malloc(8 * sizeof(int*)); + for(int r = 0; r < 8; r++) { + board[r] = malloc(8 * sizeof(int)); + for(int c = 0; c < 8; c++) { + board[r][c] = boardA[r][c]; + } + } int turn = 0; while(1) { //draw the board, write some info -- cgit v1.3.1