diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-08-30 23:04:19 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-08-30 23:04:19 +0300 |
commit | b99cda9530a54e459eb49b50cf1922b38c7432ec (patch) | |
tree | 76b5243bdc022a1646d69b5f2bd596fa70237779 | |
parent | 8efa8499eaae45a2bb2658142f720bb2bcd77088 (diff) | |
download | c-chess-b99cda9530a54e459eb49b50cf1922b38c7432ec.tar.gz |
feaT: add pawn promotions
-rw-r--r-- | src/main.c | 19 | ||||
-rw-r--r-- | src/pieces.h | 2 |
2 files changed, 21 insertions, 0 deletions
@@ -233,6 +233,17 @@ int apply_move(Move move, int* board) { // printf("Captured %s\n", pieces[target_piece]); } + if ((piece & NO_COLOR) == PAWN) { + int rank = move.destination / 16; + if (rank == 0) { + board[move.destination] = QUEEN | BLACK; + return target_piece | PROMOTION_HAPPENED; + } else if (rank == 7) { + board[move.destination] = QUEEN | WHITE; + return target_piece | PROMOTION_HAPPENED; + } + } + if ((piece & NO_COLOR) == KING && abs(move.destination - move.origin) == 2) { // CASTLE! if (move.destination == 2) { @@ -260,6 +271,14 @@ int apply_move(Move move, int* board) { void reverse_move(Move move, int captured_piece, int* board) { int piece = board[move.destination]; + if (captured_piece & PROMOTION_HAPPENED) { + board[move.destination] = captured_piece ^ PROMOTION_HAPPENED; + if (piece % 2 == WHITE) { + board[move.origin] = PAWN | WHITE; + } else board[move.origin] = PAWN | BLACK; + return; + } + board[move.origin] = piece; board[move.destination] = captured_piece; diff --git a/src/pieces.h b/src/pieces.h index fd0abaf..fe4d307 100644 --- a/src/pieces.h +++ b/src/pieces.h @@ -15,6 +15,7 @@ char* COLORS[] = { "white", "black" }; #define QUEEN 0b1010 #define KING 0b1100 +#define PROMOTION_HAPPENED 0b100000 char* pieces[] = { ".", "*", @@ -41,3 +42,4 @@ int blackPawnAttackMoves[] = { -17, -15, 0 }; #define FILE_MASK 0b1111 #define DESTINATION_MASK 0b1111111 + |