diff --git a/CMakeLists.txt b/CMakeLists.txt index e6992dc..2f8bb36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(SOURCES src/main.cpp src/input.cpp src/window.cpp + src/elements.cpp ) add_executable(tic_tac_toe ${SOURCES}) diff --git a/include/elements.hpp b/include/elements.hpp index bdface1..7086583 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -1,6 +1,32 @@ #ifndef ELEMENTS_HPP #define ELEMENTS_HPP -// Add code here +struct Point { + Point(); + Point(int row, int col) : m_row(row), m_col(col) {}; + int m_row; + int m_col; + void assign(int row, int col); + void to_string(); +}; + +struct Player { + Player(char symbol, bool turn) : m_symbol(symbol), m_turn(turn) {}; + char m_symbol; + bool m_turn; +}; + +struct Tile { + Tile(int row, int col) : m_pos(row, col), m_symbol(' '), _delete(false) {}; + char m_symbol; + Point m_pos; + bool _delete; +}; + +struct Border { + Border(int row, int col, char symbol) : m_pos(row, col), m_symbol(symbol) {}; + char m_symbol; + Point m_pos; +}; #endif diff --git a/include/globals.hpp b/include/globals.hpp index c203563..8331941 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -1,7 +1,7 @@ #ifndef GLOBALS_HPP #define GLOBALS_HPP -constexpr int ROW = 10; -constexpr int COL = 10; +constexpr int ROW = 5; +constexpr int COL = 5; #endif diff --git a/include/input.hpp b/include/input.hpp index 70892f4..8c6c0e7 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -1,10 +1,10 @@ #ifndef INPUT_HPP #define INPUT_HPP -// #include "elements.hpp" +#include "elements.hpp" struct Input { - char m_getch = ' '; - void get_input(bool &running); + int m_getch = 0; + void get_input(bool &running, Tile (&tile_list)[9], Player &player_one, Player &player_two); }; #endif // INPUT_HPP diff --git a/include/window.hpp b/include/window.hpp index c5a22c8..efb371b 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -3,10 +3,10 @@ #include #include "globals.hpp" -// #include "elements.hpp" +#include "elements.hpp" struct Window { std::array, ROW> m_board = {}; - void update_display(); + void update_display(Tile (&tile_list)[9], Border (&border_list)[16]); }; #endif diff --git a/src/elements.cpp b/src/elements.cpp index e69de29..665a9bf 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -0,0 +1,21 @@ +#include +#include "elements.hpp" + +Point::Point() { + m_row = 0; + m_col = 0; +} + +void Point::assign(int row, int col) { + m_row = row; + m_col = col; +} + +void Point::to_string() { + std::cout << "(" << m_row << ", " << m_col << ")" << std::endl; +} + +// Tile::Tile(int row, int col) { +// m_pos.assign(row, col); +// m_symbol = ' '; +// } diff --git a/src/input.cpp b/src/input.cpp index 63361b1..805102e 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,7 +1,17 @@ #include "input.hpp" #include -void Input::get_input(bool &running) { +void _switch_turn(int favor, Player &player_one, Player &player_two) { + if(favor == 1) { + player_one.m_turn = true; + player_two.m_turn = false; + } else if(favor == 2) { + player_one.m_turn = false; + player_two.m_turn = true; + } +} + +void Input::get_input(bool &running, Tile (&tile_list)[9], Player &player_one, Player &player_two) { while(running) { initscr(); cbreak(); @@ -10,8 +20,22 @@ void Input::get_input(bool &running) { m_getch = getch(); endwin(); - if(m_getch == 'q') { + if(m_getch == 113) { running = false; + } if(player_one.m_turn) { + if(!tile_list[m_getch - 48]._delete) { + tile_list[m_getch - 48].m_symbol = player_one.m_symbol; + m_getch = 48; + _switch_turn(2, player_one, player_two); + tile_list[m_getch - 48]._delete = true; + } + } else if(player_two.m_turn) { + if(!tile_list[m_getch - 48]._delete) { + tile_list[m_getch - 48].m_symbol = player_two.m_symbol; + m_getch = 48; + _switch_turn(1, player_one, player_two); + tile_list[m_getch - 48]._delete = true; + } } } } diff --git a/src/main.cpp b/src/main.cpp index 3ca3ed1..8463d79 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,15 +2,32 @@ #include #include "window.hpp" #include "input.hpp" +#include "elements.hpp" +#include "globals.hpp" int main() { Input input; Window window; + Player player_one('x', true); + Player player_two('o', false); bool running = true; - std::thread input_thread(&Input::get_input, &input, std::ref(running)); + Tile tile_list[9] = { + Tile(0, 0), Tile(0, 2), Tile(0, 4), + Tile(2, 0), Tile(2, 2), Tile(2, 4), + Tile(4, 0), Tile(4, 2), Tile(4, 4) + }; + Border border_list[16] = { + Border(0, 1, '|'), Border(0, 3, '|'), + Border(1, 0, '-'), Border(1, 1, '+'), Border(1, 2, '-'), Border(1, 3, '+'), Border(1, 4, '-'), + Border(2, 1, '|'), Border(2, 3, '|'), + Border(3, 0, '-'), Border(3, 1, '+'), Border(3, 2, '-'), Border(3, 3, '+'), Border(3, 4, '-'), + Border(4, 1, '|'), Border(4, 3, '|'), + }; + + std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(tile_list), std::ref(player_one), std::ref(player_two)); while (running) { - std::cout << "Hello World" << std::endl; + window.update_display(tile_list, border_list); } input_thread.join(); diff --git a/src/window.cpp b/src/window.cpp index e78a6b6..997476b 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,9 +3,15 @@ #include #include -void Window::update_display() { +void Window::update_display(Tile (&tile_list)[9], Border (&border_list)[16]) { for(std::array &row : m_board) { row.fill('.'); } // Modify board + for(int i = 0; i < 9; i++) { + m_board[tile_list[i].m_pos.m_row][tile_list[i].m_pos.m_col] = tile_list[i].m_symbol; + } + for(int i = 0; i < 16; i++) { + m_board[border_list[i].m_pos.m_row][border_list[i].m_pos.m_col] = border_list[i].m_symbol; + } // Refresh and draw screen std::cout << "\033[H" << std::flush;