From b99cda9530a54e459eb49b50cf1922b38c7432ec Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 30 Aug 2022 23:04:19 +0300 Subject: feaT: add pawn promotions --- src/main.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 2b7275f..4471c37 100644 --- a/src/main.c +++ b/src/main.c @@ -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; -- cgit v1.2.3