diff options
Diffstat (limited to 'chess.c')
| -rw-r--r-- | chess.c | 37 |
1 files changed, 18 insertions, 19 deletions
@@ -5,7 +5,7 @@ #include "console.h" #include "chesshelpers.h" -vectorp_t get_pawn_moves(int** board, vector_t position, int color) { +vectorp_t get_pawn_moves(board_t 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, vector_t position, int color) { } return init_vectorp(moves, moveCount); } -vectorp_t get_knight_moves(int** board, vector_t position, int color) { +vectorp_t get_knight_moves(board_t 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, vector_t position, int color) { } return init_vectorp(moves, moveCount); } -vectorp_t get_bishop_moves(int** board, vector_t position, int color) { +vectorp_t get_bishop_moves(board_t 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, vector_t position, int color) { } return init_vectorp(moves, moveCount); } -vectorp_t get_rook_moves(int** board, vector_t position, int color) { +vectorp_t get_rook_moves(board_t 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, vector_t position, int color) { } return init_vectorp(moves, moveCount); } -vectorp_t get_queen_moves(int** board, vector_t position, int color) { +vectorp_t get_queen_moves(board_t board, vector_t position, int color) { vector_t* moves = malloc(28 * sizeof(vector_t)); int moveCount = 0; for(int i = 0; i < 8; i++) { @@ -150,7 +150,8 @@ vectorp_t get_queen_moves(int** board, vector_t position, int color) { } return init_vectorp(moves, moveCount); } -vectorp_t get_king_moves(int** board, vector_t position, int color) { +//TODO: castling +vectorp_t get_king_moves(board_t board, vector_t position, int color) { vector_t* moves = malloc(28 * sizeof(vector_t)); int moveCount = 0; for(int i = 0; i < 8; i++) { @@ -175,7 +176,7 @@ vectorp_t get_king_moves(int** board, 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, vector_t position, piece_t piece) { +vectorp_t get_moves(board_t board, vector_t position, piece_t piece) { int color = 0; if(piece > 6) { color = 1; @@ -204,14 +205,14 @@ vectorp_t get_moves(int** board, vector_t position, piece_t piece) { } } //array of arrays representing all the moves each piece of color can make -vectorpp_t get_all_moves(int** board, int color) { +vectorpp_t get_all_moves(board_t board, 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]); + allMoves[pieceCount] = get_moves(board, v, board.pieces[c][r]); pieceCount++; } } @@ -220,17 +221,17 @@ vectorpp_t get_all_moves(int** board, int color) { return init_vectorpp(allMoves, pieceCount); } //returns the position of color's king -vector_t get_king_position(int** board, int color) { +vector_t get_king_position(board_t board, int color) { for(int r = 0; r < 8; r++) { for(int c = 0; c < 8; c++) { - if(board[c][r] == (king + color * 6)) { + if(board.pieces[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, int color) { +int is_in_check(board_t board, int color) { int returnV = 0; vector_t kingPos = get_king_position(board, color); //get all the moves for the opponent @@ -258,9 +259,8 @@ vectorp_t get_moves_final(board_t 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++) { - //TODO: fix this! make_move function modifies pointer - board_t tempBoard = make_move(board, position, moves[i]); - if(is_in_check(tempBoard.pieces, color)) { + board_t tempBoard = make_move(clone_board(board), position, moves.vectors[i]); + if(is_in_check(tempBoard, color)) { invalidMoves[invalidMovesCount] = i; invalidMovesCount++; } @@ -298,7 +298,7 @@ int main() { clear_screen(); draw_board(board.pieces); printf(turn == 0 ? "\nWhite's turn\n" : "\nBlack's turn\n"); - if(is_in_check(board.pieces, turn)) { + if(is_in_check(board, turn)) { printf("You're in check!\n"); } printf("Enter the coordinates of the piece you want to move\n"); @@ -321,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.pieces, selection, moves.vectors[i]); + char* readable_coords = get_move_name(board, selection, moves.vectors[i]); printf(readable_coords); free(readable_coords); printf("; "); @@ -344,8 +344,7 @@ int main() { warn_user("Invalid move"); continue; } - board[selection.mx][selection.my] = 0; - board[target.mx][target.my] = selectedPiece; + board = make_move(board, selection, target); free(moves.vectors); printf("\n"); turn = !turn; |
