Skip to content

Commit

Permalink
fix/illegal-move
Browse files Browse the repository at this point in the history
Fixes issue #3 where clicking on opponents piece while in human vs.
human allows for illegal move and causes game "suicide". Check is put in
to only select pieces of the same color as the current turn.
  • Loading branch information
956MB committed Dec 9, 2024
1 parent 96cc2da commit 675e145
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

.DS_Store
dist/*
.vscode
.nvim
.clang-format
.clangd
.editorconfig
.env
.ufbt
25 changes: 19 additions & 6 deletions views/flipchess_scene_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,26 @@ uint8_t flipchess_turn(FlipChessScene1Model* model) {
// break;

if(model->turnState == 0 && model->squareSelected != 255) {
model->squareFrom = model->squareSelected;
model->turnState = 1;
// Color check before allowing piece selection
char piece = model->game.board[model->squareSelected];
if(piece != '.' &&
SCL_pieceIsWhite(piece) == SCL_boardWhitesTurn(model->game.board)) {
model->squareFrom = model->squareSelected;
model->turnState = 1;
}
} else if(model->turnState == 1 && model->squareSelected != 255) {
model->squareTo = model->squareSelected;
model->turnState = 2;
model->squareSelectedLast = model->squareSelected;
//model->squareSelected = 255;
// Validate before executing
if(SCL_boardMoveIsLegal(model->game.board,
model->squareFrom,
model->squareSelected)) {
model->squareTo = model->squareSelected;
model->turnState = 2;
model->squareSelectedLast = model->squareSelected;
} else {
// Invalid move, reset state
model->turnState = 0;
SCL_squareSetClear(model->moveHighlight);
}
}

if(model->turnState == 1 && model->squareFrom != 255) {
Expand Down

0 comments on commit 675e145

Please sign in to comment.