Skip to content

Commit

Permalink
First version of the game
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiancristea00 committed Sep 24, 2021
1 parent 3adf341 commit 81b5998
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 42 deletions.
26 changes: 23 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
cmake_minimum_required(VERSION 3.13)

# Set C and C++ and versions
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)

# Set default build type to Release
if (NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
message("Using by default the Release build")
message("To build the Debug version pass the option -DCMAKE_BUILD_TYPE=Debug to CMake")
endif ()

# Include the Raspberry Pi Pico SDK import script
include(pico_sdk_import.cmake)

# Project
project(tic-tac-toe C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Add executable
add_executable(tic-tac-toe main.cpp Game.cpp Move.cpp LCD_I2C.cpp TM1637.cpp Keypad.cpp Player.cpp Player.hpp BoardManager.cpp BoardManager.hpp Utility.hpp IPlayerStrategy.cpp IPlayerStrategy.hpp)
add_executable(tic-tac-toe main.cpp Game.cpp Move.cpp LCD_I2C.cpp TM1637.cpp Keypad.cpp Player.cpp BoardManager.cpp IPlayerStrategy.cpp)
include_directories(${CMAKE_CURRENT_LIST_DIR})
pico_generate_pio_header(tic-tac-toe ${CMAKE_CURRENT_LIST_DIR}/TM1637.pio)

# Add program info
pico_set_program_name(tic-tac-toe "Tic-Tac-Toe LCD Game")
pico_set_program_version(tic-tac-toe "2.0")
pico_set_program_version(tic-tac-toe "1.0.0")
pico_set_program_url(tic-tac-toe "https://github.com/cristiancristea00/tic-tac-toe")
pico_set_program_description(tic-tac-toe "Copyright (c) 2021 Cristian Cristea")

Expand All @@ -28,7 +38,17 @@ pico_enable_stdio_usb(tic-tac-toe 0)
# Add the libraries to the build
target_link_libraries(tic-tac-toe pico_stdlib pico_multicore hardware_i2c hardware_pio)

# Add pico extras
pico_add_extra_outputs(tic-tac-toe)

# Set float and double implementation
pico_set_float_implementation(tic-tac-toe pico)
pico_set_double_implementation(tic-tac-toe pico)
pico_set_double_implementation(tic-tac-toe pico)

# Set Debug build compiler arguments
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wfatal-errors -Wpedantic -Wall -Wextra -Wshadow=local -Wdouble-promotion -Wformat=2 -Wformat-overflow=2 -Wformat-nonliteral -Wformat-security -Wformat-truncation=2 -Wnull-dereference -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wswitch-default -Wunused-parameter -Wunused-const-variable=2 -Wstrict-overflow=4 -Wstringop-overflow=3 -Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wmissing-noreturn -Wsuggest-attribute=malloc -Wsuggest-attribute=format -Wmissing-format-attribute -Wsuggest-attribute=cold -Walloc-zero -Walloca -Wattribute-alias=2 -Wduplicated-branches -Wcast-qual")
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -Wfatal-errors -Wpedantic -Wall -Wextra -Wshadow=local -Wdouble-promotion -Wformat=2 -Wformat-overflow=2 -Wformat-nonliteral -Wformat-security -Wformat-truncation=2 -Wnull-dereference -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wswitch-default -Wunused-parameter -Wunused-const-variable=2 -Wstrict-overflow=4 -Wstringop-overflow=3 -Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wmissing-noreturn -Wsuggest-attribute=malloc -Wsuggest-attribute=format -Wmissing-format-attribute -Wsuggest-attribute=cold -Walloc-zero -Walloca -Wattribute-alias=2 -Wduplicated-branches -Wcast-qual")

# Set Release build compiler arguments
set(CMAKE_CXX_FLAGS_RELEASE "-Ofast -Wfatal-errors -Wpedantic -Wall -Wextra -Wshadow=local -Wdouble-promotion -Wformat=2 -Wformat-overflow=2 -Wformat-nonliteral -Wformat-security -Wformat-truncation=2 -Wnull-dereference -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wswitch-default -Wunused-parameter -Wunused-const-variable=2 -Wstrict-overflow=4 -Wstringop-overflow=3 -Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wmissing-noreturn -Wsuggest-attribute=malloc -Wsuggest-attribute=format -Wmissing-format-attribute -Wsuggest-attribute=cold -Walloc-zero -Walloca -Wattribute-alias=2 -Wduplicated-branches -Wcast-qual -funroll-loops -finline-functions -fno-builtin")
set(CMAKE_C_FLAGS_RELEASE "-Ofast -Wfatal-errors -Wpedantic -Wall -Wextra -Wshadow=local -Wdouble-promotion -Wformat=2 -Wformat-overflow=2 -Wformat-nonliteral -Wformat-security -Wformat-truncation=2 -Wnull-dereference -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wswitch-default -Wunused-parameter -Wunused-const-variable=2 -Wstrict-overflow=4 -Wstringop-overflow=3 -Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wmissing-noreturn -Wsuggest-attribute=malloc -Wsuggest-attribute=format -Wmissing-format-attribute -Wsuggest-attribute=cold -Walloc-zero -Walloca -Wattribute-alias=2 -Wduplicated-branches -Wcast-qual -funroll-loops -finline-functions -fno-builtin")
6 changes: 3 additions & 3 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void Game::Internal_Play() noexcept
{
if (second_player_turn)
{
auto move = second_player->GetNextMove(BoardManager::Instance()->GetGameBoard(), keypad.get());
auto move = second_player->GetNextMove(BoardManager::Instance()->GetGameBoard());
BoardManager::Instance()->GetGameBoard() = BoardManager::Instance()->GetResultBoard(
BoardManager::Instance()->GetGameBoard(), move, second_player->GetSymbol());
second_player_turn = false;
Expand All @@ -255,7 +255,7 @@ void Game::Internal_Play() noexcept
{
static Move move;

move = first_player->GetNextMove(BoardManager::Instance()->GetGameBoard(), keypad.get());
move = first_player->GetNextMove(BoardManager::Instance()->GetGameBoard());
BoardManager::Instance()->GetGameBoard() = BoardManager::Instance()->GetResultBoard(
BoardManager::Instance()->GetGameBoard(), move, first_player->GetSymbol());
}
Expand Down Expand Up @@ -310,7 +310,7 @@ inline auto Game::Get_User() const noexcept -> PlayerSymbol
void Game::Key_Poller_Runner() noexcept
{
static bool light_on {false};
static Key key {Key::UNKOWN};
static Key key {Key::UNKNOWN};

auto * keypad = reinterpret_cast<Keypad *> (multicore_fifo_pop_blocking());
auto * lcd = reinterpret_cast<LCD_I2C *>(multicore_fifo_pop_blocking());
Expand Down
6 changes: 3 additions & 3 deletions Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
#include <hardware/regs/rosc.h>
#include <pico/multicore.h>

#include "IPlayerStrategy.hpp"
#include "LCD_I2C.hpp"
#include "Player.hpp"
#include "Move.hpp"
#include "TM1637.hpp"
#include "Keypad.hpp"
#include "IPlayerStrategy.hpp"
#include "Player.hpp"
#include "Move.hpp"

#include <unordered_map>
#include <algorithm>
Expand Down
15 changes: 7 additions & 8 deletions IPlayerStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ auto IPlayerStrategy::GetRNG() noexcept -> std::mt19937 &
return random_number_generator;
}

auto EasyStrategy::GetNextMove(Utility::Board const & current_board, Keypad * const /*keypad*/) noexcept -> Move
auto EasyStrategy::GetNextMove(Utility::Board const & current_board) noexcept -> Move
{
if (BoardManager::Instance()->IsTerminal(current_board))
{
Expand All @@ -61,7 +61,7 @@ auto EasyStrategy::GetName() const noexcept -> std::string_view
return "EASY";
}

auto MediumStrategy::GetNextMove(Utility::Board const & current_board, Keypad * const /*keypad*/) noexcept -> Move
auto MediumStrategy::GetNextMove(Utility::Board const & current_board) noexcept -> Move
{
if (BoardManager::Instance()->IsTerminal(current_board))
{
Expand Down Expand Up @@ -185,7 +185,7 @@ auto HardStrategy::Get_Possible_Moves(Board const & current_board) const
return possible_moves;
}

auto HardStrategy::GetNextMove(Utility::Board const & current_board, Keypad * const /*keypad*/) noexcept -> Move
auto HardStrategy::GetNextMove(Utility::Board const & current_board) noexcept -> Move
{
if (BoardManager::Instance()->IsTerminal(current_board))
{
Expand All @@ -199,9 +199,8 @@ auto HardStrategy::GetNextMove(Utility::Board const & current_board, Keypad * co
{
if (BoardManager::Instance()->
IsWinner(BoardManager::Instance()->GetCurrentPlayer(current_board),
BoardManager::Instance()->GetResultBoard(current_board, ACTION,
BoardManager::Instance()->
GetCurrentPlayer(current_board))))
BoardManager::Instance()->GetResultBoard(current_board, ACTION, BoardManager::Instance()->
GetCurrentPlayer(current_board))))
{
return ACTION;
}
Expand All @@ -215,13 +214,13 @@ auto HardStrategy::GetName() const noexcept -> std::string_view
return "HARD";
}

auto HumanStrategy::GetNextMove(Board const & current_board, Keypad * const keypad) noexcept -> Move
auto HumanStrategy::GetNextMove(Utility::Board const & current_board) noexcept -> Move
{
static Move move;

do
{
move = Keypad::ActionFromKey(keypad->GetPressedKey());
move = Keypad::ActionFromKey(Keypad::GetPressedKey());
}
while (!BoardManager::Instance()->IsValidAction(current_board, move));
return move;
Expand Down
12 changes: 5 additions & 7 deletions IPlayerStrategy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include <unordered_map>
#include <random>

class Keypad;

class IPlayerStrategy
{
private:
Expand Down Expand Up @@ -66,7 +64,7 @@ class IPlayerStrategy
* @param current_board The board to be analysed
* @return A move
*/
[[nodiscard]] virtual auto GetNextMove(Utility::Board const & current_board, Keypad * keypad) noexcept -> Move = 0;
[[nodiscard]] virtual auto GetNextMove(Utility::Board const & current_board) noexcept -> Move = 0;

/**
* Gets the strategy's name.
Expand Down Expand Up @@ -116,7 +114,7 @@ class EasyStrategy final : public IPlayerStrategy
* @param current_board The board to be analysed
* @return A random move
*/
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board, Keypad * keypad) noexcept -> Move final;
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board) noexcept -> Move final;

/**
* Gets the strategy's name.
Expand Down Expand Up @@ -167,7 +165,7 @@ class MediumStrategy final : public IPlayerStrategy
* @param current_board The board to be analysed
* @return A somewhat good move
*/
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board, Keypad * keypad) noexcept -> Move final;
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board) noexcept -> Move final;

/**
* Gets the strategy's name.
Expand Down Expand Up @@ -254,7 +252,7 @@ class HardStrategy final : public IPlayerStrategy
* @param current_board The board to be analysed
* @return The best move
*/
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board, Keypad * keypad) noexcept -> Move final;
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board) noexcept -> Move final;

/**
* Gets the strategy's name.
Expand Down Expand Up @@ -304,7 +302,7 @@ class HumanStrategy final : public IPlayerStrategy
* @param current_board The board to be analysed
* @return The input move
*/
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board, Keypad * keypad) noexcept -> Move final;
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board) noexcept -> Move final;

/**
* Gets the strategy's name.
Expand Down
9 changes: 4 additions & 5 deletions Keypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

using Utility::PlayerSymbol;

Keypad::Keypad(array const & rows, array const & columns) noexcept
: rows(rows), columns(columns)
Keypad::Keypad(array const & rows, array const & columns) noexcept : rows(rows), columns(columns)
{
Init();
}
Expand Down Expand Up @@ -57,18 +56,18 @@ auto Keypad::Key_Poller() const noexcept -> Key
}
}
}
return Key::UNKOWN;
return Key::UNKNOWN;
}

auto Keypad::GetKeyFromPoller() const noexcept -> Key
{
Key key {Key::UNKOWN};
Key key {Key::UNKNOWN};

do
{
key = Key_Poller();
}
while (key == Key::UNKOWN);
while (key == Key::UNKNOWN);

return key;
}
Expand Down
2 changes: 1 addition & 1 deletion Keypad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum class Key
KEY5, KEY6, KEY7, KEY8,
KEY9, KEY10, KEY11, KEY12,
KEY13, KEY14, KEY15, KEY16,
UNKOWN
UNKNOWN
};

class Keypad final
Expand Down
2 changes: 1 addition & 1 deletion LCD_I2C.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class LCD_I2C final
* Creates a custom character by specifying the location in memory to be
* stored (8 locations maximum, starting from 0) and an array of 8 bytes.
* @code
* constexpr uint8_t BELL[8] = {0x04, 0x0E, 0x0E, 0x1F, 0x00, 0x04, 0x00, 0x00};
* constexpr std::array BELL = {0x04, 0x0E, 0x0E, 0x1F, 0x00, 0x04, 0x00, 0x00};
* @endcode
*
* @param location The memory address
Expand Down
5 changes: 3 additions & 2 deletions Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Player.hpp"

using Utility::PlayerSymbol;
using Utility::Board;

Player::Player(PlayerSymbol symbol, IPlayerStrategy * const strategy) noexcept : symbol(symbol), strategy(strategy) {}

Expand All @@ -28,7 +29,7 @@ void Player::SetSymbol(PlayerSymbol player_symbol) noexcept
symbol = player_symbol;
}

auto Player::GetNextMove(Utility::Board const & current_board, Keypad * const keypad) noexcept -> Move
auto Player::GetNextMove(Board const & current_board) noexcept -> Move
{
return strategy->GetNextMove(current_board, keypad);
return strategy->GetNextMove(current_board);
}
2 changes: 1 addition & 1 deletion Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ class Player
* @param current_board The board to be analysed
* @return A move
*/
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board, Keypad * keypad) noexcept -> Move;
[[nodiscard]] auto GetNextMove(Utility::Board const & current_board) noexcept -> Move;
};

4 changes: 1 addition & 3 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include <pico/binary_info/code.h>

#include <memory>

#include "Game.hpp"
#include "Keypad.hpp"
#include "Game.hpp"

int main()
{
Expand Down
5 changes: 0 additions & 5 deletions pico_sdk_import.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake

# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()

if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
Expand Down

0 comments on commit 81b5998

Please sign in to comment.