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 +++++++++++++++++++++++++++------------- chesshelpers.c | 14 ++++++++------ chesshelpers.h | 8 ++++---- console.c | 3 +-- console.h | 2 +- vector.c | 4 ++-- 6 files changed, 43 insertions(+), 28 deletions(-) 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 diff --git a/chesshelpers.c b/chesshelpers.c index a0caadd..7a8178b 100644 --- a/chesshelpers.c +++ b/chesshelpers.c @@ -1,15 +1,16 @@ #include #include +#include #include "vector.h" #include "chesshelpers.h" int in_board(vector_t position) { return position.mx < 8 && position.mx >= 0 && position.my < 8 && position.my >= 0; } -int is_empty(int board[8][8], vector_t position) { +int is_empty(int** board, vector_t position) { return board[position.mx][position.my] == 0; } -int is_color(int board[8][8], vector_t position, int color) { +int is_color(int** board, vector_t position, int color) { if(color == 0) { return board[position.mx][position.my] >= 1 && board[position.mx][position.my] <= 6; } @@ -26,7 +27,7 @@ char* get_readable_coords(vector_t position) { return output; } //gets the proper algebraic notation for the move -char* get_move_name(int board[8][8], vector_t origin, vector_t end) { +char* get_move_name(int** board, vector_t origin, vector_t end) { int piece = board[origin.mx][origin.my]; if(piece > 6) { piece -= 6; @@ -59,11 +60,12 @@ char* get_move_name(int board[8][8], vector_t origin, vector_t end) { return output; } } -int** clone_board(int board[8][8]) { - int** newBoard = malloc(8 * 8 * sizeof(int)); +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[c][r] = board[c][r]; + newBoard[r][c] = board[r][c]; } } return newBoard; diff --git a/chesshelpers.h b/chesshelpers.h index 0f1de34..55a211a 100644 --- a/chesshelpers.h +++ b/chesshelpers.h @@ -10,9 +10,9 @@ typedef enum { king = 6, } piece_t; int in_board(vector_t position); -int is_empty(int board[8][8], vector_t position); -int is_color(int board[8][8], vector_t position, int color); +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[8][8], vector_t origin, vector_t end); -int** clone_board(int board[8][8]); +char* get_move_name(int** board, vector_t origin, vector_t end); +int** clone_board(int** board); #endif diff --git a/console.c b/console.c index 7eed8ce..19e2a38 100644 --- a/console.c +++ b/console.c @@ -5,7 +5,7 @@ #include "chesshelpers.h" #include "console.h" -void draw_board(int board[8][8]) { +void draw_board(int** board) { printf(" A B C D E F G H\n"); int tileColor = 0; for(int r = 0; r < 8; r++) { @@ -103,4 +103,3 @@ void warn_user(char* message) { while(getchar() != '\n'); getchar(); } - diff --git a/console.h b/console.h index 796d009..c0ca9f7 100644 --- a/console.h +++ b/console.h @@ -5,7 +5,7 @@ #define COLOR_WARN "\033[5;31;49m" #define COLOR_RESET "\033[0m" -void draw_board(int board[8][8]); +void draw_board(int** board); vector_t user_input_pos(); void clear_screen(); void warn_user(char* message); diff --git a/vector.c b/vector.c index 61b0e81..2741a47 100644 --- a/vector.c +++ b/vector.c @@ -25,7 +25,7 @@ vectorp_t remove_vector(vectorp_t vectorp, int index) { vectorp_t remove_vectors(vectorp_t vectorp, int* indexes, int indexCount) { vector_t* vectors = malloc((vectorp.length - indexCount) * sizeof(vector_t)); int correctedIndex = 0; - for(int i = 0; i < vectorp.length - 1; i++) { + for(int i = 0; i < vectorp.length - indexCount; i++) { for(int j = 0; j < indexCount; j++) { if(i == indexes[j]) { continue; @@ -34,7 +34,7 @@ vectorp_t remove_vectors(vectorp_t vectorp, int* indexes, int indexCount) { vectors[correctedIndex] = vectorp.vectors[i]; correctedIndex++; } - return init_vectorp(vectors, vectorp.length - 1); + return init_vectorp(vectors, vectorp.length - indexCount); } vectorpp_t init_vectorpp(vectorp_t* vectorps, int length) { vectorpp_t vectorPP; -- cgit v1.3.1