diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-01-07 14:54:28 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-01-07 14:54:57 -0800 |
| commit | 441bf949eb45ff99963addb24ffbedfada510979 (patch) | |
| tree | d4c9588515eb8f3b06eee8c8207d348613feb4e1 | |
| parent | 476c3bc7692442974c6d7533a59f7d26c5848fb5 (diff) | |
added en passant, version 1
| -rw-r--r-- | chess.c | 13 | ||||
| -rw-r--r-- | chesshelpers.c | 15 | ||||
| -rw-r--r-- | chesshelpers.h | 1 |
3 files changed, 25 insertions, 4 deletions
@@ -6,7 +6,13 @@ #include "chesshelpers.h" #include "chess.h" -int main() { +int main(int argc, char **argv) { + if(argc == 2) { + if(strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) { + printf("Version 1.0.0\n"); + exit(0); + } + } //set up the board int boardA[8][8] = { { rook + 6, pawn + 6, 0, 0, 0, 0, pawn, rook }, @@ -198,7 +204,6 @@ vectorp_t get_moves_final(board_t board, vector_t position, piece_t piece) { free(moves.vectors); return finalMoves; } -//TODO: add en passant vectorp_t get_pawn_moves(board_t board, vector_t position, int color) { vector_t* moves = malloc(6 * sizeof(vector_t)); int moveCount = 0; @@ -216,12 +221,12 @@ vectorp_t get_pawn_moves(board_t board, vector_t position, int color) { } } vector_t captureL = init_vector(position.mx - 1, position.my + forwardDir); - if(in_board(captureL) && is_color(board, captureL, !color)) { + if(in_board(captureL) && (is_color(board, captureL, !color) || would_have_been_pawn(board, captureL, !color))) { moves[moveCount] = captureL; moveCount++; } vector_t captureR = init_vector(position.mx + 1, position.my + forwardDir); - if(in_board(captureR) && is_color(board, captureR, !color)) { + if(in_board(captureR) && (is_color(board, captureR, !color) || would_have_been_pawn(board, captureR, !color))) { moves[moveCount] = captureR; moveCount++; } diff --git a/chesshelpers.c b/chesshelpers.c index 2e046c9..b64a0f4 100644 --- a/chesshelpers.c +++ b/chesshelpers.c @@ -17,6 +17,7 @@ board_t init_board(int** pieces) { //function applies side effects to board values like king move state board_t make_move(board_t board, vector_t origin, vector_t end) { int piece = board.pieces[origin.mx][origin.my]; + int captured = board.pieces[end.mx][end.my]; board.pieces[origin.mx][origin.my] = 0; board.pieces[end.mx][end.my] = piece; //white king moved @@ -62,6 +63,11 @@ board_t make_move(board_t board, vector_t origin, vector_t end) { //this is not an else if! lastPawnMove must be reset if(piece == pawn || piece == pawn + 6) { board.lastPawnMove = origin.mx; + //check if move was an en passant capture + if(captured == 0 && origin.mx != end.mx) { + //kill the enemy pawn + board.pieces[end.mx][origin.my] = 0; + } } else { //en passant no longer possible @@ -95,6 +101,15 @@ int is_color(board_t board, vector_t position, int color) { return board.pieces[position.mx][position.my] >= 7; } } +//returns true if a pawn that moved two squares would have been here if it moved one square +int would_have_been_pawn(board_t board, vector_t position, int color) { + if(board.lastPawnMove == position.mx) { + if((position.my == 6 && color == 0) || (position.my == 2 && color == 1)) { + return 1; + } + } + return 0; +} //converts board coordinates into algebraic notation char* get_readable_coords(vector_t position) { char* output = malloc(3 * sizeof(char)); diff --git a/chesshelpers.h b/chesshelpers.h index b975740..b3a3d21 100644 --- a/chesshelpers.h +++ b/chesshelpers.h @@ -25,6 +25,7 @@ board_t clone_board(board_t board); int in_board(vector_t position); int is_empty(board_t board, vector_t position); int is_color(board_t board, vector_t position, int color); +int would_have_been_pawn(board_t board, vector_t position, int color); char* get_readable_coords(vector_t position); char* get_move_name(board_t board, vector_t origin, vector_t end); #endif |
