diff options
Diffstat (limited to 'chess.c')
| -rw-r--r-- | chess.c | 74 |
1 files changed, 73 insertions, 1 deletions
@@ -150,6 +150,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) { int color = 0; if(piece > 6) { @@ -172,6 +173,74 @@ vectorp_t get_moves(int board[8][8], vector_t position, piece_t piece) { return get_queen_moves(board, position, color); } } +//array of arrays representing all the moves each piece of color can make +vectorpp_t get_all_moves(int board[8][8], int color) { + vectorp_t* allMoves = malloc(16 * sizeof(vectorp_t)); + int pieceCount = 0; + for(int r = 0; r < 8; r++) { + for(int c = 0; c < 8; c++) { + vector_t v = init_vector(c, r); + if(is_color(board, v, color)) { + allMoves[pieceCount] = get_moves(board, v, board[c][r]); + pieceCount++; + } + } + } + allMoves = realloc(allMoves, pieceCount * sizeof(vectorp_t)); + return init_vectorpp(allMoves, pieceCount); +} +//returns the position of color's king +vector_t get_king_position(int board[8][8], int color) { + for(int r = 0; r < 8; r++) { + for(int c = 0; c < 8; c++) { + if(board[c][r] == (king + color * 6)) { + return init_vector(c, r); + } + } + } +} +//returns true if the player of color is currently in check +int is_in_check(int board[8][8], int color) { + int returnV = 0; + vector_t kingPos = get_king_position(board, color); + //get all the moves for the opponent + vectorpp_t allMoves = get_all_moves(board, !color); + for(int i = 0; i < allMoves.length; i++) { + vectorp_t moves = allMoves.vectorps[i]; + for(int j = 0; j < moves.length; j++) { + if(moves.vectors[j].mx == kingPos.mx && moves.vectors[j].my == kingPos.my) { + returnV = 1; + } + } + free(moves.vectors); + } + 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) { + int color = 0; + if(piece > 6) { + color = 1; + } + vectorp_t moves = get_moves(board, position, piece); + //try making that move, see if it puts the player in check + int* invalidMoves = malloc(moves.length * sizeof(int)); + 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; + if(is_in_check(tempBoard, color)) { + invalidMoves[invalidMovesCount]; + invalidMovesCount++; + } + free(tempBoard); + } + vectorp_t finalMoves = remove_vectors(moves, invalidMoves, invalidMovesCount); + free(invalidMoves); + free(moves.vectors); + return finalMoves; +} int main() { int board[8][8] = { { rook + 6, pawn + 6, 0, 0, 0, 0, pawn, rook }, @@ -189,6 +258,9 @@ int main() { clear_screen(); draw_board(board); printf(turn == 0 ? "\nWhite's turn\n" : "\nBlack's turn\n"); + if(is_in_check(board, 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(); @@ -201,7 +273,7 @@ int main() { printf("Selected piece: "); printf("%d", selectedPiece); printf("\n"); - vectorp_t moves = get_moves(board, selection, selectedPiece); + vectorp_t moves = get_moves_final(board, selection, selectedPiece); if(moves.length == 0) { warn_user("That piece has no valid moves!"); continue; |
