diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bfc35b..4fa0728 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,8 @@ include_directories(include) set(SOURCES src/main.cpp src/input.cpp + src/window.cpp + src/elements.cpp ) add_executable(connect_four ${SOURCES}) diff --git a/include/elements.hpp b/include/elements.hpp new file mode 100644 index 0000000..ebe15a9 --- /dev/null +++ b/include/elements.hpp @@ -0,0 +1,27 @@ +#ifndef ELEMENTS_HPP +#define ELEMENTS_HPP + +#include + +struct Point { + Point() : m_row(0), m_col(0), m_id(0), m_active(true) {} + Point(int row, int col, int id) : m_row(row), m_col(col), m_id(id), m_active(true) {} + int m_row; + int m_col; + int m_id; + bool m_active; + char m_symbol; + void assign(int row, int col); + void move(); + void check_collision(std::vector &points_list); +}; + +struct Player { + Player(char symbol, bool turn) : m_symbol(symbol), m_turn(turn) {} + char m_symbol; + bool m_turn; + std::vector m_points; + void input_tile(int col, std::vector &points); +}; + +#endif diff --git a/include/globals.hpp b/include/globals.hpp new file mode 100644 index 0000000..d1bda33 --- /dev/null +++ b/include/globals.hpp @@ -0,0 +1,7 @@ +#ifndef GLOBALS_HPP +#define GLOBALS_HPP + +constexpr int ROW = 6; +constexpr int COL = 7; + +#endif diff --git a/include/input.hpp b/include/input.hpp index 1391276..3a2cf03 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -1,9 +1,11 @@ #ifndef INPUT_HPP #define INPUT_HPP +#include "elements.hpp" + struct Input { int _getch = 0; - void get_input(bool &running); + void get_input(bool &running, Player &one, Player &two, std::vector &points); }; #endif diff --git a/include/window.hpp b/include/window.hpp new file mode 100644 index 0000000..b3b1a7b --- /dev/null +++ b/include/window.hpp @@ -0,0 +1,14 @@ +#ifndef WINDOW_HPP +#define WINDOW_HPP + +#include +#include +#include "globals.hpp" +#include "elements.hpp" + +struct Window { + std::array, ROW> m_board = {}; + void update_display(std::vector &points_list); +}; + +#endif diff --git a/src/elements.cpp b/src/elements.cpp new file mode 100644 index 0000000..b3fb80d --- /dev/null +++ b/src/elements.cpp @@ -0,0 +1,30 @@ +#include "elements.hpp" +#include "globals.hpp" +#include + +void Point::assign(int row, int col) { + m_row = row; + m_col = col; +} + +void Point::move() { + if(m_row < ROW - 1) { + m_row++; + } +} + +void Point::check_collision(std::vector &points_list) {; + for(int i = 0; i < points_list.size(); i++) { + if(points_list[i].m_id == m_id) continue; + if(points_list[i].m_row != m_row) continue; + if(points_list[i].m_col != m_col) continue; + m_row = points_list[i].m_row - 1; + m_active = false; + } +} + +void Player::input_tile(int col, std::vector &points) { + if(points.size() == 0) points.push_back(Point({ 0, col, 0 })); + else points.push_back(Point({ 0, col, points.back().m_id + 1 })); + points.back().m_symbol = m_symbol; +} diff --git a/src/input.cpp b/src/input.cpp index b7ee291..cfd98fb 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,7 +1,8 @@ -#include "input.hpp" +#include #include +#include "input.hpp" -void Input::get_input(bool &running) { +void Input::get_input(bool &running, Player &one, Player &two, std::vector &points) { while(running) { initscr(); cbreak(); @@ -10,8 +11,33 @@ void Input::get_input(bool &running) { _getch = getch(); endwin(); - if(_getch == 113) { - running = false; + switch (_getch) { + case 49: + one.input_tile(0, points); + break; + case 50: + one.input_tile(1, points); + break; + case 51: + one.input_tile(2, points); + break; + case 52: + one.input_tile(3, points); + break; + case 53: + one.input_tile(4, points); + break; + case 54: + one.input_tile(5, points); + break; + case 55: + one.input_tile(6, points); + break; + case 113: + running = false; + break; + default: + _getch = 0; } } } diff --git a/src/main.cpp b/src/main.cpp index 7441766..33d8874 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,27 @@ #include #include +#include #include "input.hpp" +#include "window.hpp" +#include "elements.hpp" int main() { + std::vector points_list = {}; Input input; + Window window; + Player one('o', true); + Player two('x', false); bool running = true; - std::thread input_thread(&Input::get_input, &input, std::ref(running)); + std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(one), std::ref(two), std::ref(points_list)); while(running) { - std::cout << "Hello World" << std::endl; + window.update_display(points_list); + for(int i = 0; i < points_list.size(); i++) { + if(!points_list[i].m_active) continue; + points_list[i].move(); + points_list[i].check_collision(points_list); + } + std::cout << points_list.size() << std::endl; } input_thread.join(); diff --git a/src/window.cpp b/src/window.cpp new file mode 100644 index 0000000..33626c2 --- /dev/null +++ b/src/window.cpp @@ -0,0 +1,20 @@ +#include "window.hpp" +#include +#include +#include + +void Window::update_display(std::vector &points_list) { + for(std::array &row : m_board) { row.fill('.'); } + for(int i = 0; i < points_list.size(); i++) { + m_board[points_list[i].m_row][points_list[i].m_col] = '*'; + } + + std::cout << "\033[H" << std::flush; + for(std::array &row : m_board) { + for(char &col : row) { + std::cout << col << " "; + } + std::cout << "\r\n"; + } + std::this_thread::sleep_for(std::chrono::milliseconds(200)); // 1000 / fps; 200ms = 5fps +}