diff --git a/include/elements.hpp b/include/elements.hpp index 7086583..3c60ac8 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -1,6 +1,11 @@ #ifndef ELEMENTS_HPP #define ELEMENTS_HPP +struct Player; +struct Point; +struct Tile; +struct Border; + struct Point { Point(); Point(int row, int col) : m_row(row), m_col(col) {}; @@ -11,9 +16,11 @@ struct Point { }; struct Player { - Player(char symbol, bool turn) : m_symbol(symbol), m_turn(turn) {}; + Player(char symbol, bool turn) : m_symbol(symbol), m_turn(turn), m_win(false) {}; char m_symbol; bool m_turn; + bool m_win; + void check_line(Tile (&tile_list)[9]); }; struct Tile { diff --git a/include/input.hpp b/include/input.hpp index 8c6c0e7..80ab955 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -3,7 +3,7 @@ #include "elements.hpp" struct Input { - int m_getch = 0; + int _getch = 0; void get_input(bool &running, Tile (&tile_list)[9], Player &player_one, Player &player_two); }; diff --git a/src/elements.cpp b/src/elements.cpp index 665a9bf..9bbd9d5 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -15,7 +15,23 @@ 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 = ' '; -// } + +void Player::check_line(Tile (&tile_list)[9]) { + for(int i = 0; i <= 6; i += 3) { + if(tile_list[i].m_symbol == tile_list[i+1].m_symbol && tile_list[i+1].m_symbol == tile_list[i+2].m_symbol) { + if(tile_list[i].m_symbol == m_symbol) { + m_win = true; + } + } + } + if(tile_list[0].m_symbol == tile_list[4].m_symbol && tile_list[4].m_symbol == tile_list[8].m_symbol) { + if(tile_list[0].m_symbol == m_symbol) { + m_win = true; + } + } + if(tile_list[2].m_symbol == tile_list[4].m_symbol && tile_list[4].m_symbol == tile_list[6].m_symbol) { + if(tile_list[2].m_symbol == m_symbol) { + m_win = true; + } + } +} diff --git a/src/input.cpp b/src/input.cpp index 1acb53a..fd76b00 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -17,22 +17,22 @@ void Input::get_input(bool &running, Tile (&tile_list)[9], Player &player_one, P cbreak(); noecho(); - m_getch = getch(); + _getch = getch(); endwin(); - if(m_getch == 113) { + if(_getch == 113) { running = false; } - if(!tile_list[m_getch - 49]._delete) { + if(!tile_list[_getch - 49]._delete) { if(player_one.m_turn) { - tile_list[m_getch - 49].m_symbol = player_one.m_symbol; + tile_list[_getch - 49].m_symbol = player_one.m_symbol; _switch_turn(2, player_one, player_two); } else if(player_two.m_turn) { - tile_list[m_getch - 49].m_symbol = player_two.m_symbol; + tile_list[_getch - 49].m_symbol = player_two.m_symbol; _switch_turn(1, player_one, player_two); } - tile_list[m_getch - 49]._delete = true; - m_getch = 48; + tile_list[_getch - 49]._delete = true; + _getch = 48; } } } diff --git a/src/main.cpp b/src/main.cpp index 8463d79..652fb80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,8 +26,18 @@ int main() { }; 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) { + if(player_one.m_win) { + running = false; + std::cout << "Player 1 wins!" << std::endl; + } else if(player_two.m_win) { + running = false; + std::cout << "Player 2 wins!" << std::endl; + } window.update_display(tile_list, border_list); + player_one.check_line(tile_list); + player_two.check_line(tile_list); } input_thread.join(); diff --git a/src/window.cpp b/src/window.cpp index 997476b..7d07165 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -5,7 +5,7 @@ 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; }