Tic tac toe basically done
This commit is contained in:
45
src/dawn/games/tictactoe/TicTacToeLogic.cpp
Normal file
45
src/dawn/games/tictactoe/TicTacToeLogic.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "TicTacToeLogic.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
enum TicTacToeTileState Dawn::ticTacToeDetermineWinner(
|
||||
const std::map<uint8_t, enum TicTacToeTileState> board,
|
||||
std::vector<uint8_t> *winningCombo
|
||||
) {
|
||||
uint8_t i;
|
||||
assertNotNull(winningCombo);
|
||||
|
||||
// Check rows
|
||||
for(i = 0; i < 9; i += 3) {
|
||||
if(board.at(i) == board.at(i + 1) && board.at(i) == board.at(i + 2) && board.at(i) != 0) {
|
||||
*winningCombo = { i, (uint8_t)i + 1, (uint8_t)i + 2 };
|
||||
return board.at(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Check columns
|
||||
for(i = 0; i < 3; i++) {
|
||||
if(board.at(i) == board.at(i + 3) && board.at(i) == board.at(i + 6) && board.at(i) != 0) {
|
||||
*winningCombo = { i, (uint8_t)i + 3, (uint8_t)i + 6 };
|
||||
return board.at(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Check diagonals
|
||||
if(board.at(0) == board.at(4) && board.at(0) == board.at(8) && board.at(0) != 0) {
|
||||
*winningCombo = {0, 4, 8};
|
||||
return board.at(0);
|
||||
}
|
||||
|
||||
if(board.at(2) == board.at(4) && board.at(2) == board.at(6) && board.at(2) != 0) {
|
||||
*winningCombo = { 2, 4, 6 };
|
||||
return board.at(2);
|
||||
}
|
||||
|
||||
return TIC_TAC_TOE_EMPTY;
|
||||
}
|
Reference in New Issue
Block a user