Tic tac toe basically done

This commit is contained in:
2023-02-26 14:01:45 -08:00
parent f74ca2501f
commit 0f9e9f2ccc
15 changed files with 171 additions and 26 deletions

View File

@ -26,6 +26,7 @@ target_sources(${DAWN_TARGET_NAME}
# Subdirs
add_subdirectory(asset)
add_subdirectory(display)
add_subdirectory(games)
add_subdirectory(input)
add_subdirectory(locale)
add_subdirectory(physics)

View File

@ -0,0 +1,7 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
add_subdirectory(tictactoe)

View File

@ -0,0 +1,10 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
TicTacToeLogic.cpp
)

View 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;
}

View File

@ -0,0 +1,20 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "assert/assert.hpp"
namespace Dawn {
enum TicTacToeTileState {
TIC_TAC_TOE_EMPTY,
TIC_TAC_TOE_NOUGHT,
TIC_TAC_TOE_CROSS
};
enum TicTacToeTileState ticTacToeDetermineWinner(
const std::map<uint8_t, enum TicTacToeTileState> board,
std::vector<uint8_t> *winningCombo
);
}