From 6178e7c96395748162601e7b17ae18c6e5ccd74f Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Wed, 4 Sep 2024 08:24:54 +0200 Subject: [PATCH] fixed promotion for the white user --- src/chess/evaluate.c | 5 ++--- src/chess/main.c | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/chess/evaluate.c b/src/chess/evaluate.c index 41cc3f0..817d253 100644 --- a/src/chess/evaluate.c +++ b/src/chess/evaluate.c @@ -3,7 +3,6 @@ #include #include #include -#include // These min and max values will not overflow, when negated #define INT_16_SAFE_MIN (INT_LEAST16_MIN + 1) @@ -56,9 +55,9 @@ static int_least16_t alphaBeta(const struct gameState_t gameState, int_fast16_t uint_least8_t movesLength = validMoves(gameState, moves); if(movesLength == 0) { if(kingInCheck(gameState.board, gameState.color)) { - return INT_16_SAFE_MIN + 1; + return INT_16_SAFE_MIN + 1; // checkmate } - return 0; + return 0; // stalemate } for(uint_least8_t i = 0; i < movesLength; ++i) { const struct move_t move = moves[i]; diff --git a/src/chess/main.c b/src/chess/main.c index e8e4973..2c35086 100644 --- a/src/chess/main.c +++ b/src/chess/main.c @@ -11,6 +11,9 @@ #include #include "evaluate.h" +//#define START_FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" +#define START_FEN "rnbqkbnr/PPppppPP/8/8/8/8/ppPPPPpp/RNBQKBNR w KQkq - 0 1" + #define drawPiece(file, rank, piece) do { \ const RsvgRectangle rect = {xOffset + (file) * fieldSize, yOffset + (rank) * fieldSize, fieldSize, fieldSize};\ rsvg_handle_render_document(piecesSvg[pieceToSvgI(piece)], cr, &rect, NULL); \ @@ -118,10 +121,11 @@ static void on_click(GtkGestureClick *gesture, int n_press, double x, double y, const uint_least8_t file = adjustedX / fieldSize; const uint_least8_t rank = adjustedY / fieldSize; const uint_least8_t field = file + rank * BOARD_SIZE; + const bool promotion_field = rank == (gameState->color == WHITE ? 0 : BOARD_SIZE - 1); if(drawData->selectDst == NOT_SELECTED) { const struct piece_t allPiece = {ALL_PIECES, turn}; if(bitboardGet(board, allPiece, field)) { - // deaktivated piece by clicking on it again + // deactivated piece by clicking on it again if(field == drawData->clickedPiece) { drawData->clickedPiece = NOT_SELECTED; drawData->movesLength = 0; @@ -141,8 +145,8 @@ static void on_click(GtkGestureClick *gesture, int n_press, double x, double y, } } if(isValidDst) { - const struct piece_t piece = pieceAtField(board, field); - if(piece.type == PAWN && field < BOARD_SIZE) { // promotion + const struct piece_t piece = pieceAtField(board, drawData->clickedPiece); + if(piece.type == PAWN && promotion_field) { // promotion drawData->selectDst = field; } else { @@ -162,12 +166,13 @@ static void on_click(GtkGestureClick *gesture, int n_press, double x, double y, drawData->movesLength = 0; } } - else { - if(rank != 0) return; + else if(promotion_field) { const uint_least8_t uiPos = promotionUiPos(drawData->selectDst); + const uint_least8_t i = file - uiPos; + if(i >= LENGTH(PROMOTION_PIECES)) return; const uint_least8_t dstPiece = PROMOTION_PIECES[file - uiPos]; const uint_least8_t capturedPiece = pieceAtField(board, drawData->selectDst).type; - const struct move_t move = {drawData->clickedPiece, drawData->selectDst, 0, PAWN, dstPiece}; + const struct move_t move = {drawData->clickedPiece, drawData->selectDst, 0, PAWN, dstPiece, capturedPiece}; *gameState = userMove(*gameState, move); drawData->selectDst = NOT_SELECTED; drawData->clickedPiece = NOT_SELECTED; @@ -180,7 +185,7 @@ static void app_activate(GApplication *app, gpointer data) { static RsvgHandle *piecesSvg[16]; static uint_least64_t board[BITBOARD_LENGTH]; static struct gameState_t gameState; - gameState = newGameState(board, "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + gameState = newGameState(board, START_FEN); { static struct move_t moves[TOTAL_BOARD_SIZE]; static struct drawData_t drawData = {piecesSvg, &gameState, NOT_SELECTED, moves, 0, NOT_SELECTED};