fixed promotion for the white user

This commit is contained in:
2024-09-04 08:24:54 +02:00
parent 2d49f86517
commit 6178e7c963
2 changed files with 14 additions and 10 deletions

View File

@ -3,7 +3,6 @@
#include <stdint.h> #include <stdint.h>
#include <chess/bitboard.h> #include <chess/bitboard.h>
#include <chess/print.h> #include <chess/print.h>
#include <stdio.h>
// These min and max values will not overflow, when negated // These min and max values will not overflow, when negated
#define INT_16_SAFE_MIN (INT_LEAST16_MIN + 1) #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); uint_least8_t movesLength = validMoves(gameState, moves);
if(movesLength == 0) { if(movesLength == 0) {
if(kingInCheck(gameState.board, gameState.color)) { 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) { for(uint_least8_t i = 0; i < movesLength; ++i) {
const struct move_t move = moves[i]; const struct move_t move = moves[i];

View File

@ -11,6 +11,9 @@
#include <chess/bitboard.h> #include <chess/bitboard.h>
#include "evaluate.h" #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 { \ #define drawPiece(file, rank, piece) do { \
const RsvgRectangle rect = {xOffset + (file) * fieldSize, yOffset + (rank) * fieldSize, fieldSize, fieldSize};\ const RsvgRectangle rect = {xOffset + (file) * fieldSize, yOffset + (rank) * fieldSize, fieldSize, fieldSize};\
rsvg_handle_render_document(piecesSvg[pieceToSvgI(piece)], cr, &rect, NULL); \ 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 file = adjustedX / fieldSize;
const uint_least8_t rank = adjustedY / fieldSize; const uint_least8_t rank = adjustedY / fieldSize;
const uint_least8_t field = file + rank * BOARD_SIZE; 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) { if(drawData->selectDst == NOT_SELECTED) {
const struct piece_t allPiece = {ALL_PIECES, turn}; const struct piece_t allPiece = {ALL_PIECES, turn};
if(bitboardGet(board, allPiece, field)) { if(bitboardGet(board, allPiece, field)) {
// deaktivated piece by clicking on it again // deactivated piece by clicking on it again
if(field == drawData->clickedPiece) { if(field == drawData->clickedPiece) {
drawData->clickedPiece = NOT_SELECTED; drawData->clickedPiece = NOT_SELECTED;
drawData->movesLength = 0; drawData->movesLength = 0;
@ -141,8 +145,8 @@ static void on_click(GtkGestureClick *gesture, int n_press, double x, double y,
} }
} }
if(isValidDst) { if(isValidDst) {
const struct piece_t piece = pieceAtField(board, field); const struct piece_t piece = pieceAtField(board, drawData->clickedPiece);
if(piece.type == PAWN && field < BOARD_SIZE) { // promotion if(piece.type == PAWN && promotion_field) { // promotion
drawData->selectDst = field; drawData->selectDst = field;
} }
else { else {
@ -162,12 +166,13 @@ static void on_click(GtkGestureClick *gesture, int n_press, double x, double y,
drawData->movesLength = 0; drawData->movesLength = 0;
} }
} }
else { else if(promotion_field) {
if(rank != 0) return;
const uint_least8_t uiPos = promotionUiPos(drawData->selectDst); 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 dstPiece = PROMOTION_PIECES[file - uiPos];
const uint_least8_t capturedPiece = pieceAtField(board, drawData->selectDst).type; 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); *gameState = userMove(*gameState, move);
drawData->selectDst = NOT_SELECTED; drawData->selectDst = NOT_SELECTED;
drawData->clickedPiece = NOT_SELECTED; drawData->clickedPiece = NOT_SELECTED;
@ -180,7 +185,7 @@ static void app_activate(GApplication *app, gpointer data) {
static RsvgHandle *piecesSvg[16]; static RsvgHandle *piecesSvg[16];
static uint_least64_t board[BITBOARD_LENGTH]; static uint_least64_t board[BITBOARD_LENGTH];
static struct gameState_t gameState; 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 move_t moves[TOTAL_BOARD_SIZE];
static struct drawData_t drawData = {piecesSvg, &gameState, NOT_SELECTED, moves, 0, NOT_SELECTED}; static struct drawData_t drawData = {piecesSvg, &gameState, NOT_SELECTED, moves, 0, NOT_SELECTED};