From 5675e99f0f5b7148a8ea2711b9dcaf7fb345dd62 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 1 Mar 2023 21:34:46 -0800 Subject: [PATCH] Some Logic tidy --- src/dawn/games/tictactoe/TicTacToeLogic.cpp | 11 ++++++++++ src/dawn/games/tictactoe/TicTacToeLogic.hpp | 11 ++++++++++ .../components/TicTacToeGame.cpp | 20 +++++++------------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/dawn/games/tictactoe/TicTacToeLogic.cpp b/src/dawn/games/tictactoe/TicTacToeLogic.cpp index d1919f04..35d597ca 100644 --- a/src/dawn/games/tictactoe/TicTacToeLogic.cpp +++ b/src/dawn/games/tictactoe/TicTacToeLogic.cpp @@ -44,6 +44,17 @@ enum TicTacToeTileState Dawn::ticTacToeDetermineWinner( return TIC_TAC_TOE_EMPTY; } +bool_t Dawn::ticTacToeIsGameOver( + const std::map board +) { + auto it = board.begin(); + while(it != board.end()) { + if(it->second == TIC_TAC_TOE_EMPTY) return false; + ++it; + } + return true; +} + int32_t Dawn::ticTacToeGetBoardScore( std::map board, enum TicTacToeTileState player diff --git a/src/dawn/games/tictactoe/TicTacToeLogic.hpp b/src/dawn/games/tictactoe/TicTacToeLogic.hpp index 7961da72..5cefd695 100644 --- a/src/dawn/games/tictactoe/TicTacToeLogic.hpp +++ b/src/dawn/games/tictactoe/TicTacToeLogic.hpp @@ -25,6 +25,17 @@ namespace Dawn { std::vector *winningCombo ); + /** + * Returns true if the tic tac toe game is over. Will also consider ties as a + * game over state. + * + * @param board Board to check if game has ended. + * @return True if game is over, otherwise false. + */ + bool_t ticTacToeIsGameOver( + const std::map board + ); + /** * Returns the score / value of a given board for the given player. Mostly * used by the AI to determine whether a given board is better or worse than diff --git a/src/dawntictactoe/components/TicTacToeGame.cpp b/src/dawntictactoe/components/TicTacToeGame.cpp index 2a0aa675..1f4e8281 100644 --- a/src/dawntictactoe/components/TicTacToeGame.cpp +++ b/src/dawntictactoe/components/TicTacToeGame.cpp @@ -28,6 +28,11 @@ void TicTacToeGame::onStart() { } useEffect([&]{ + if(!gameOver) return; + + auto board = this->getBoard(); + std::vector winningCombo; + winner = ticTacToeDetermineWinner(board, &winningCombo); std::cout << "Winner is " << winner << std::endl; }, gameOver); @@ -70,7 +75,6 @@ void TicTacToeGame::onStart() { // Now update the hover state(s) auto itTiles = tiles.begin(); - uint8_t tilesLeft = 0; while(itTiles != tiles.end()) { auto t = itTiles->second; if(t == hovered) { @@ -78,16 +82,9 @@ void TicTacToeGame::onStart() { } else { t->hovered = false; } - if(t->tileState == TIC_TAC_TOE_EMPTY) tilesLeft++; ++itTiles; } - if(tilesLeft == 0) { - winner = TIC_TAC_TOE_EMPTY; - gameOver = true; - return; - } - if(isPlayerMove) { if(getGame()->inputManager.isPressed(INPUT_BIND_MOUSE_CLICK)) { this->makeMove(hovered->tile, nextMove); @@ -98,11 +95,8 @@ void TicTacToeGame::onStart() { this->makeMove(move, nextMove); } - // Determine winner - auto board = this->getBoard(); - std::vector winningCombo; - winner = ticTacToeDetermineWinner(board, &winningCombo); - if(winner != TIC_TAC_TOE_EMPTY) gameOver = true; + // Did game just end? + if(ticTacToeIsGameOver(this->getBoard())) gameOver = true; }, getScene()->eventSceneUpdate); }