Explorar el Código

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 hace 1 año
padre
commit
675e1456f0
Se han modificado 2 ficheros con 29 adiciones y 6 borrados
  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) {