From 658ae86a181e78012a0dcf5837139a15f88ed6af Mon Sep 17 00:00:00 2001 From: xtruan Date: Mon, 24 Jul 2023 19:59:22 -0600 Subject: [PATCH] fix: check for check, fix win detection, fix return behavior --- application.fam | 2 +- flipchess.h | 2 +- views/flipchess_scene_1.c | 210 ++++++++++++++++++++------------------ 3 files changed, 112 insertions(+), 102 deletions(-) diff --git a/application.fam b/application.fam index 9cddb67..31a5c90 100644 --- a/application.fam +++ b/application.fam @@ -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", ) diff --git a/flipchess.h b/flipchess.h index 8ed1012..39cdaa9 100644 --- a/flipchess.h +++ b/flipchess.h @@ -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) diff --git a/views/flipchess_scene_1.c b/views/flipchess_scene_1.c index 404e405..49156a0 100644 --- a/views/flipchess_scene_1.c +++ b/views/flipchess_scene_1.c @@ -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; @@ -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); }