Some Logic tidy
This commit is contained in:
@ -44,6 +44,17 @@ enum TicTacToeTileState Dawn::ticTacToeDetermineWinner(
|
|||||||
return TIC_TAC_TOE_EMPTY;
|
return TIC_TAC_TOE_EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool_t Dawn::ticTacToeIsGameOver(
|
||||||
|
const std::map<uint8_t, enum TicTacToeTileState> 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(
|
int32_t Dawn::ticTacToeGetBoardScore(
|
||||||
std::map<uint8_t, enum TicTacToeTileState> board,
|
std::map<uint8_t, enum TicTacToeTileState> board,
|
||||||
enum TicTacToeTileState player
|
enum TicTacToeTileState player
|
||||||
|
@ -25,6 +25,17 @@ namespace Dawn {
|
|||||||
std::vector<uint8_t> *winningCombo
|
std::vector<uint8_t> *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<uint8_t, enum TicTacToeTileState> board
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the score / value of a given board for the given player. Mostly
|
* 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
|
* used by the AI to determine whether a given board is better or worse than
|
||||||
|
@ -28,6 +28,11 @@ void TicTacToeGame::onStart() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect([&]{
|
useEffect([&]{
|
||||||
|
if(!gameOver) return;
|
||||||
|
|
||||||
|
auto board = this->getBoard();
|
||||||
|
std::vector<uint8_t> winningCombo;
|
||||||
|
winner = ticTacToeDetermineWinner(board, &winningCombo);
|
||||||
std::cout << "Winner is " << winner << std::endl;
|
std::cout << "Winner is " << winner << std::endl;
|
||||||
}, gameOver);
|
}, gameOver);
|
||||||
|
|
||||||
@ -70,7 +75,6 @@ void TicTacToeGame::onStart() {
|
|||||||
|
|
||||||
// Now update the hover state(s)
|
// Now update the hover state(s)
|
||||||
auto itTiles = tiles.begin();
|
auto itTiles = tiles.begin();
|
||||||
uint8_t tilesLeft = 0;
|
|
||||||
while(itTiles != tiles.end()) {
|
while(itTiles != tiles.end()) {
|
||||||
auto t = itTiles->second;
|
auto t = itTiles->second;
|
||||||
if(t == hovered) {
|
if(t == hovered) {
|
||||||
@ -78,16 +82,9 @@ void TicTacToeGame::onStart() {
|
|||||||
} else {
|
} else {
|
||||||
t->hovered = false;
|
t->hovered = false;
|
||||||
}
|
}
|
||||||
if(t->tileState == TIC_TAC_TOE_EMPTY) tilesLeft++;
|
|
||||||
++itTiles;
|
++itTiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tilesLeft == 0) {
|
|
||||||
winner = TIC_TAC_TOE_EMPTY;
|
|
||||||
gameOver = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(isPlayerMove) {
|
if(isPlayerMove) {
|
||||||
if(getGame()->inputManager.isPressed(INPUT_BIND_MOUSE_CLICK)) {
|
if(getGame()->inputManager.isPressed(INPUT_BIND_MOUSE_CLICK)) {
|
||||||
this->makeMove(hovered->tile, nextMove);
|
this->makeMove(hovered->tile, nextMove);
|
||||||
@ -98,11 +95,8 @@ void TicTacToeGame::onStart() {
|
|||||||
this->makeMove(move, nextMove);
|
this->makeMove(move, nextMove);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine winner
|
// Did game just end?
|
||||||
auto board = this->getBoard();
|
if(ticTacToeIsGameOver(this->getBoard())) gameOver = true;
|
||||||
std::vector<uint8_t> winningCombo;
|
|
||||||
winner = ticTacToeDetermineWinner(board, &winningCombo);
|
|
||||||
if(winner != TIC_TAC_TOE_EMPTY) gameOver = true;
|
|
||||||
}, getScene()->eventSceneUpdate);
|
}, getScene()->eventSceneUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user