Tic tac toe basically done
This commit is contained in:
@ -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)
|
||||
|
7
src/dawn/games/CMakeLists.txt
Normal file
7
src/dawn/games/CMakeLists.txt
Normal 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)
|
10
src/dawn/games/tictactoe/CMakeLists.txt
Normal file
10
src/dawn/games/tictactoe/CMakeLists.txt
Normal 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
|
||||
)
|
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;
|
||||
}
|
20
src/dawn/games/tictactoe/TicTacToeLogic.hpp
Normal file
20
src/dawn/games/tictactoe/TicTacToeLogic.hpp
Normal 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
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user