Skip to content

Commit

Permalink
fix: check for check, fix win detection, fix return behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
xtruan committed Jul 25, 2023
1 parent ea5a3ba commit 658ae86
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 102 deletions.
2 changes: 1 addition & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ App(
fap_category="Games",
fap_author="Struan Clark (xtruan)",
fap_weburl="https://github.com/xtruan/flipper-chess",
fap_version=(1, 7),
fap_version=(1, 8),
fap_description="Chess for Flipper",
)
2 changes: 1 addition & 1 deletion flipchess.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "views/flipchess_startscreen.h"
#include "views/flipchess_scene_1.h"

#define FLIPCHESS_VERSION "v1.7.0"
#define FLIPCHESS_VERSION "v1.8.0"

#define TEXT_BUFFER_SIZE 96
#define TEXT_SIZE (TEXT_BUFFER_SIZE - 1)
Expand Down
210 changes: 110 additions & 100 deletions views/flipchess_scene_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,118 +217,128 @@ uint8_t flipchess_turn(FlipChessScene1Model* model) {

if(model->game.state != SCL_GAME_STATE_PLAYING) {
model->paramExit = FlipChessStatusNone;
return model->paramExit;
}

char movePromote = 'q';

if(flipchess_isPlayerTurn(model)) {
// if(stringsEqual(string, "undo", 5))
// moveType = FlipChessStatusMoveUndo;
// else if(stringsEqual(string, "quit", 5))
// break;

if(model->turnState == 0 && model->squareSelected != 255) {
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;
}

if(model->turnState == 1 && model->squareFrom != 255) {
if((model->game.board[model->squareFrom] != '.') &&
(SCL_pieceIsWhite(model->game.board[model->squareFrom]) ==
SCL_boardWhitesTurn(model->game.board))) {
SCL_boardGetMoves(model->game.board, model->squareFrom, model->moveHighlight);
} else {
char movePromote = 'q';

if(flipchess_isPlayerTurn(model)) {
// if(stringsEqual(string, "undo", 5))
// moveType = FlipChessStatusMoveUndo;
// else if(stringsEqual(string, "quit", 5))
// break;

if(model->turnState == 0 && model->squareSelected != 255) {
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;
}
} else if(model->turnState == 2) {
if(SCL_squareSetContains(model->moveHighlight, model->squareTo)) {
moveType = FlipChessStatusMovePlayer;

if(model->turnState == 1 && model->squareFrom != 255) {
if((model->game.board[model->squareFrom] != '.') &&
(SCL_pieceIsWhite(model->game.board[model->squareFrom]) ==
SCL_boardWhitesTurn(model->game.board))) {
SCL_boardGetMoves(model->game.board, model->squareFrom, model->moveHighlight);
}
} else if(model->turnState == 2) {
if(SCL_squareSetContains(model->moveHighlight, model->squareTo)) {
moveType = FlipChessStatusMovePlayer;
}
model->turnState = 0;
SCL_squareSetClear(model->moveHighlight);
}

} else {
model->squareSelected = 255;
flipchess_makeAIMove(
model->game.board, &(model->squareFrom), &(model->squareTo), &movePromote, model);
moveType = FlipChessStatusMoveAI;
model->turnState = 0;
}

if(moveType == FlipChessStatusMovePlayer || moveType == FlipChessStatusMoveAI) {
flipchess_shiftMessages(model);

SCL_moveToString(
model->game.board,
model->squareFrom,
model->squareTo,
movePromote,
model->moveString);

SCL_gameMakeMove(&(model->game), model->squareFrom, model->squareTo, movePromote);

SCL_squareSetClear(model->moveHighlight);
SCL_squareSetAdd(model->moveHighlight, model->squareFrom);
SCL_squareSetAdd(model->moveHighlight, model->squareTo);
} else if(moveType == FlipChessStatusMoveUndo) {
flipchess_shiftMessages(model);

if(model->paramPlayerW != 0 || model->paramPlayerB != 0)
SCL_gameUndoMove(&(model->game));

SCL_gameUndoMove(&(model->game));
SCL_squareSetClear(model->moveHighlight);
}

} else {
model->squareSelected = 255;
flipchess_makeAIMove(
model->game.board, &(model->squareFrom), &(model->squareTo), &movePromote, model);
moveType = FlipChessStatusMoveAI;
model->turnState = 0;
}
switch(model->game.state) {
case SCL_GAME_STATE_WHITE_WIN:
model->msg = "white wins";
model->paramExit = FlipChessStatusReturn;
break;

if(moveType == FlipChessStatusMovePlayer || moveType == FlipChessStatusMoveAI) {
flipchess_shiftMessages(model);
case SCL_GAME_STATE_BLACK_WIN:
model->msg = "black wins";
model->paramExit = FlipChessStatusReturn;
break;

SCL_moveToString(
model->game.board, model->squareFrom, model->squareTo, movePromote, model->moveString);
case SCL_GAME_STATE_DRAW_STALEMATE:
model->msg = "stalemate";
model->paramExit = FlipChessStatusReturn;
break;

SCL_gameMakeMove(&(model->game), model->squareFrom, model->squareTo, movePromote);
case SCL_GAME_STATE_DRAW_REPETITION:
model->msg = "draw-repetition";
model->paramExit = FlipChessStatusReturn;
break;

SCL_squareSetClear(model->moveHighlight);
SCL_squareSetAdd(model->moveHighlight, model->squareFrom);
SCL_squareSetAdd(model->moveHighlight, model->squareTo);
} else if(moveType == FlipChessStatusMoveUndo) {
flipchess_shiftMessages(model);
case SCL_GAME_STATE_DRAW_DEAD:
model->msg = "draw-dead pos.";
model->paramExit = FlipChessStatusReturn;
break;

if(model->paramPlayerW != 0 || model->paramPlayerB != 0) SCL_gameUndoMove(&(model->game));
case SCL_GAME_STATE_DRAW:
model->msg = "draw";
model->paramExit = FlipChessStatusReturn;
break;

SCL_gameUndoMove(&(model->game));
SCL_squareSetClear(model->moveHighlight);
}
case SCL_GAME_STATE_DRAW_50:
model->msg = "draw-50 moves";
model->paramExit = FlipChessStatusReturn;
break;

switch(model->game.state) {
case SCL_GAME_STATE_WHITE_WIN:
model->msg = "white wins";
model->paramExit = FlipChessStatusReturn;
break;

case SCL_GAME_STATE_BLACK_WIN:
model->msg = "black wins";
model->paramExit = FlipChessStatusReturn;
break;

case SCL_GAME_STATE_DRAW_STALEMATE:
model->msg = "draw (stalemate)";
model->paramExit = FlipChessStatusReturn;
break;

case SCL_GAME_STATE_DRAW_REPETITION:
model->msg = "draw (repetition)";
model->paramExit = FlipChessStatusReturn;
break;

case SCL_GAME_STATE_DRAW_DEAD:
model->msg = "draw (dead pos.)";
model->paramExit = FlipChessStatusReturn;
break;

case SCL_GAME_STATE_DRAW:
model->msg = "draw";
model->paramExit = FlipChessStatusReturn;
break;

case SCL_GAME_STATE_DRAW_50:
model->msg = "draw (50 moves)";
model->paramExit = FlipChessStatusReturn;
break;

default:
if(model->game.ply > 0) {
model->msg =
(SCL_boardWhitesTurn(model->game.board) ? "black played" : "white played");

uint8_t s0, s1;
char p;

SCL_recordGetMove(model->game.record, model->game.ply - 1, &s0, &s1, &p);
SCL_moveToString(model->game.board, s0, s1, p, model->moveString);
default:
if(model->game.ply > 0) {
const uint8_t whitesTurn = SCL_boardWhitesTurn(model->game.board);

if(SCL_boardCheck(model->game.board, whitesTurn)) {
model->msg = (whitesTurn ? "black: check!" : "white: check!");
} else {
model->msg = (whitesTurn ? "black played" : "white played");
}

uint8_t s0, s1;
char p;

SCL_recordGetMove(model->game.record, model->game.ply - 1, &s0, &s1, &p);
SCL_moveToString(model->game.board, s0, s1, p, model->moveString);
}
break;
model->paramExit = moveType;
}
break;
model->paramExit = moveType;
}

model->thinking = 0;
Expand Down Expand Up @@ -681,9 +691,9 @@ void flipchess_scene_1_enter(void* context) {
}

// if return status, return from scene immediately
if(init == FlipChessStatusReturn) {
instance->callback(FlipChessCustomEventScene1Back, instance->context);
}
// if(init == FlipChessStatusReturn) {
// instance->callback(FlipChessCustomEventScene1Back, instance->context);
// }
},
true);
}
Expand Down

0 comments on commit 658ae86

Please sign in to comment.