From 5209dfec9200d9f61793533eb4ff9c444f5b4037 Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Sun, 15 Sep 2024 19:07:08 +0200 Subject: [PATCH] fixed promotion menu for black --- include/chess/bitboard.h | 2 ++ src/chess/main.c | 37 ++++++++++++++++++----------------- src/chess/move.c | 2 +- src/common/bitboard.c | 8 ++++++++ src/common/print.c | 4 ++-- src/generateCode/moveConsts.c | 4 ++-- 6 files changed, 34 insertions(+), 23 deletions(-) diff --git a/include/chess/bitboard.h b/include/chess/bitboard.h index 732d40e..b1e5507 100644 --- a/include/chess/bitboard.h +++ b/include/chess/bitboard.h @@ -14,3 +14,5 @@ void bitboardClear(uint_least64_t *board, struct piece_t piece, uint_least8_t i) void bitboardSet(uint_least64_t *board, struct piece_t piece, uint_least8_t i); struct piece_t pieceAtField(const uint_least64_t *board, uint_least8_t i); struct gameState_t newGameState(uint_least64_t *board, char *FEN); +uint_least8_t getFile(const uint_least8_t field); +uint_least8_t getRank(const uint_least8_t field); diff --git a/src/chess/main.c b/src/chess/main.c index 9ef61cd..819596e 100644 --- a/src/chess/main.c +++ b/src/chess/main.c @@ -11,8 +11,7 @@ #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 START_FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" struct drawData_t { RsvgHandle **piecesSvg; @@ -30,24 +29,21 @@ struct drawData_t { const uint_least8_t PROMOTION_PIECES[] = {QUEEN, KNIGHT, BISHOP, ROOK}; static uint_least8_t pieceToSvgI(struct piece_t piece) { - return piece.color == WHITE ? piece.type | 8 : piece.type; + return piece.color == WHITE ? PIECES_LENGTH + piece.type : piece.type; } static void drawPiece(struct drawData_t *ctx, cairo_t *cr, uint_least8_t field, struct piece_t piece) { - const uint_least8_t file = field % BOARD_SIZE; - const uint_least8_t rank = field / BOARD_SIZE; - const RsvgRectangle rect = {ctx->xOffset + file * ctx->fieldSize, ctx->yOffset + rank * ctx->fieldSize, ctx->fieldSize, ctx->fieldSize};\ + const RsvgRectangle rect = {ctx->xOffset + getFile(field)* ctx->fieldSize, + ctx->yOffset + getRank(field) * ctx->fieldSize, ctx->fieldSize, ctx->fieldSize}; rsvg_handle_render_document(ctx->piecesSvg[pieceToSvgI(piece)], cr, &rect, NULL); } -static void selectCircle(cairo_t *cr, const struct drawData_t *drawData, uint_least8_t field, double radius) { - uint_least8_t file = field % BOARD_SIZE; - uint_least8_t rank = field / BOARD_SIZE; - double xOffset = drawData->xOffset; - double yOffset = drawData->yOffset; - double fieldSize = drawData->fieldSize; - cairo_arc(cr, xOffset + (file + 0.5) * fieldSize, yOffset + (rank + 0.5) * fieldSize, radius, 0, 2 * M_PI); +static void selectCircle(cairo_t *cr, const struct drawData_t *drawData, uint_least8_t field, double radius) { + const double xOffset = drawData->xOffset; + const double yOffset = drawData->yOffset; + const double fieldSize = drawData->fieldSize; + cairo_arc(cr, xOffset + (getFile(field) + 0.5) * fieldSize, yOffset + (getRank(field) + 0.5) * fieldSize, radius, 0, 2 * M_PI); } static struct gameState_t userMove(struct gameState_t gameState, struct move_t move) { @@ -67,6 +63,13 @@ static uint_least8_t promotionUiPos(uint_least8_t dst, bool color) { return uiPos; } +static void drawRetangle(cairo_t *cr, struct drawData_t *drawData, uint_least8_t field, double width) { + const double fieldSize = drawData->fieldSize; + cairo_rectangle(cr, drawData->xOffset + getFile(field) * fieldSize, + drawData->yOffset + getRank(field) * fieldSize, width, fieldSize); + cairo_fill(cr); +} + static void draw_event(GtkDrawingArea *area, cairo_t *cr, int width, int height, gpointer data) { const double fieldSize = (double)(width < height ? width : height) / BOARD_SIZE; const double xOffset = (width - fieldSize * BOARD_SIZE) / 2; @@ -81,10 +84,9 @@ static void draw_event(GtkDrawingArea *area, cairo_t *cr, int width, int height, for(uint_least8_t file = 0; file < BOARD_SIZE; ++file) { for(uint_least8_t rank = 0; rank < BOARD_SIZE; ++rank) { const uint_least8_t field = rank * BOARD_SIZE + file; - cairo_rectangle(cr, xOffset + file * fieldSize, yOffset + rank * fieldSize, fieldSize, fieldSize); if((file + rank) & 1) cairo_set_source_rgb(cr, 0.46274, 0.5882, 0.3373); else cairo_set_source_rgb(cr, 0.9333, 0.9333, 0.8235); - cairo_fill(cr); + drawRetangle(cr, drawData, field, fieldSize); if(bitboardGetAllPieces(board, field)) { const struct piece_t piece = pieceAtField(board, rank * BOARD_SIZE + file); drawPiece(drawData, cr, field, piece); @@ -104,8 +106,7 @@ static void draw_event(GtkDrawingArea *area, cairo_t *cr, int width, int height, if(drawData->selectDst != NOT_SELECTED) { uint_least8_t uiPos = promotionUiPos(drawData->selectDst, gameState->color); cairo_set_source_rgb(cr, 0.5, 0.5, 0.5); - cairo_rectangle(cr, xOffset + uiPos * fieldSize, yOffset, 4 * fieldSize, fieldSize); - cairo_fill(cr); + drawRetangle(cr, drawData, uiPos, 4 * fieldSize); for(uint_least8_t i = 0; i < LENGTH(PROMOTION_PIECES); ++i) { const struct piece_t piece = {PROMOTION_PIECES[i], gameState->color}; drawPiece(drawData, cr, uiPos + i, piece); @@ -186,7 +187,7 @@ static void on_click(GtkGestureClick *gesture, int n_press, double x, double y, static void app_activate(GApplication *app, gpointer data) { GtkWindow *window = GTK_WINDOW(gtk_window_new()); - static RsvgHandle *piecesSvg[16]; + static RsvgHandle *piecesSvg[2 * PIECES_LENGTH]; static uint_least64_t board[BITBOARD_LENGTH]; static struct gameState_t gameState; gameState = newGameState(board, START_FEN); diff --git a/src/chess/move.c b/src/chess/move.c index eb74664..b76a3c7 100644 --- a/src/chess/move.c +++ b/src/chess/move.c @@ -303,7 +303,7 @@ uint_least8_t pieceValidMoves(struct gameState_t gameState, struct piece_t piece uint_least8_t dst = src + DIRECTION_MODIFIER[pawnDirection]; if(!bitboardGetAllPieces(board, dst)) { movePawn(addmoveCtx, piece.color, src, dst, promotion, 0); - if(src / BOARD_SIZE == (piece.color == WHITE ? BOARD_SIZE - 2 : 1)) { + if(getFile(src) == (piece.color == WHITE ? BOARD_SIZE - 2 : 1)) { dst += DIRECTION_MODIFIER[pawnDirection]; if(!bitboardGetAllPieces(board, dst)) { movePawn(addmoveCtx, piece.color, src, dst, promotion, FUTURE_EN_PASSANT); diff --git a/src/common/bitboard.c b/src/common/bitboard.c index 9650069..5cfb02e 100644 --- a/src/common/bitboard.c +++ b/src/common/bitboard.c @@ -134,3 +134,11 @@ struct gameState_t newGameState(uint_least64_t *board, char *FEN) { gameState.halfMoveCounter = atoi(++FEN); return gameState; } + +uint_least8_t getFile(const uint_least8_t field) { + return field % BOARD_SIZE; +} + +uint_least8_t getRank(const uint_least8_t field) { + return field / BOARD_SIZE; +} diff --git a/src/common/print.c b/src/common/print.c index 5ef5128..943bcab 100644 --- a/src/common/print.c +++ b/src/common/print.c @@ -23,8 +23,8 @@ void printPieceMask(uint_least64_t mask) { } void fieldToString(uint_least8_t field, char *output) { - output[0] = 'a' + field % BOARD_SIZE; - output[1] = '0' + BOARD_SIZE - field / BOARD_SIZE; + output[0] = 'a' + getFile(field); + output[1] = '0' + BOARD_SIZE - getRank(field); output[2] = '\0'; } diff --git a/src/generateCode/moveConsts.c b/src/generateCode/moveConsts.c index c829037..163e0d3 100644 --- a/src/generateCode/moveConsts.c +++ b/src/generateCode/moveConsts.c @@ -36,8 +36,8 @@ int main() { ); uint_least8_t directionOffset[TOTAL_BOARD_SIZE * DIRECTION_LENGTH]; for(uint_least16_t field = 0; field < TOTAL_BOARD_SIZE; ++field) { - uint_least8_t file = field % BOARD_SIZE; - uint_least8_t rank = field / BOARD_SIZE; + uint_least8_t file = getFile(field); + uint_least8_t rank = getRank(field); uint_least8_t *offsetField = directionOffset + field * DIRECTION_LENGTH; offsetField[NORTH] = rank; offsetField[WEST] = file;