Преглед на файлове

fix/illegal-move

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.
Alexander Bays преди 1 година
родител
ревизия
675e1456f0
променени са 2 файла, в които са добавени 29 реда и са изтрити 6 реда
  1. 10 0
      .gitignore
  2. 19 6
      views/flipchess_scene_1.c

+ 10 - 0
.gitignore

@@ -50,3 +50,13 @@ modules.order
 Module.symvers
 Mkfile.old
 dkms.conf
+
+.DS_Store
+dist/*
+.vscode
+.nvim
+.clang-format
+.clangd
+.editorconfig
+.env
+.ufbt

+ 19 - 6
views/flipchess_scene_1.c

@@ -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) {