diff --git a/CMakeLists.txt b/CMakeLists.txt index e1a5654..924f776 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ include_directories(include) set(SOURCES src/main.cpp src/input.cpp + src/elements.cpp src/window.cpp ) diff --git a/include/elements.hpp b/include/elements.hpp new file mode 100644 index 0000000..cd79ab5 --- /dev/null +++ b/include/elements.hpp @@ -0,0 +1,23 @@ +#ifndef ELEMENTS_HPP +#define ELEMENTS_HPP + +#include +#include "globals.hpp" + +struct Point { + Point() : m_row(0), m_col(0), m_symbol(2), m_direction('n') {}; + Point(int row, int col) : m_row(row), m_col(col), m_symbol(2), m_direction('n') {}; + char m_direction; + int m_row; + int m_col; + int m_symbol; + void assign(int row, int col); +}; + +struct PointCollection { + std::vector m_point_list; + void move(); + void set_direction(char direction); +}; + +#endif diff --git a/include/input.hpp b/include/input.hpp index 7a43ce7..b4b93a1 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; - void get_input(bool &running); + void get_input(bool &running, PointCollection &collection); }; #endif diff --git a/include/window.hpp b/include/window.hpp index 88aefe1..6859e7c 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -2,11 +2,12 @@ #define WINDOW_HPP #include "globals.hpp" +#include "elements.hpp" #include struct Window { std::array, ROW> m_board = {}; - void draw_window(); + void draw_window(PointCollection &collection); }; #endif diff --git a/src/elements.cpp b/src/elements.cpp new file mode 100644 index 0000000..c07b5a1 --- /dev/null +++ b/src/elements.cpp @@ -0,0 +1,30 @@ +#include "elements.hpp" + +void Point::assign(int row, int col) { + m_row = row; + m_col = col; +} + +void PointCollection::set_direction(char direction) { + for(int i = m_point_list.size() - 1; i >= 0; i--) { + m_point_list[i].m_direction = direction; + } +} + +void PointCollection::move() { + for(int i = m_point_list.size() - 1; i >= 0; i--) { + if(m_point_list[i].m_direction == 'u') { + if(m_point_list[i].m_row <= 0) continue; + m_point_list[i].assign(m_point_list[i].m_row - 1, m_point_list[i].m_col); + } else if(m_point_list[i].m_direction == 'd') { + if(m_point_list[i].m_row >= ROW - 1) continue; + m_point_list[i].assign(m_point_list[i].m_row + 1, m_point_list[i].m_col); + } else if(m_point_list[i].m_direction == 'l') { + if(m_point_list[i].m_col <= 0) continue; + m_point_list[i].assign(m_point_list[i].m_row, m_point_list[i].m_col - 1); + } else if(m_point_list[i].m_direction == 'r') { + if(m_point_list[i].m_col >= COL - 1) continue; + m_point_list[i].assign(m_point_list[i].m_row, m_point_list[i].m_col + 1); + } + } +} diff --git a/src/input.cpp b/src/input.cpp index 1944140..d6ea198 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,7 +1,7 @@ #include "input.hpp" #include -void Input::get_input(bool &running) { +void Input::get_input(bool &running, PointCollection &collection) { while (running) { initscr(); cbreak(); @@ -10,6 +10,22 @@ void Input::get_input(bool &running) { _getch = getch(); endwin(); - if(_getch == 113) running = false; + if(_getch == 65) { + _getch = 0; + collection.set_direction('u'); + } + else if(_getch == 66) { + _getch = 0; + collection.set_direction('d'); + } + else if(_getch == 67) { + _getch = 0; + collection.set_direction('r'); + } + else if(_getch == 68) { + _getch = 0; + collection.set_direction('l'); + } + else if(_getch == 113) running = false; } } diff --git a/src/main.cpp b/src/main.cpp index 2c0e3a7..dfdc87d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,17 +2,20 @@ #include #include "input.hpp" #include "window.hpp" +#include "elements.hpp" int main() { Input input; Window window; + PointCollection collection; 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(collection)); + collection.m_point_list.push_back(Point()); - window.m_board[0][0] = 4; while (running) { - window.draw_window(); + collection.move(); + window.draw_window(collection); } input_thread.join(); diff --git a/src/window.cpp b/src/window.cpp index 644980e..e64f382 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,10 +1,14 @@ #include "window.hpp" +#include #include #include #include -void Window::draw_window() { - // for(std::array &row : m_board) { row.fill(0); } +void Window::draw_window(PointCollection &collection) { + for(std::array &row : m_board) { row.fill(0); } + for(int i = collection.m_point_list.size() - 1; i >= 0; i--) { + m_board[collection.m_point_list[i].m_row][collection.m_point_list[i].m_col] = collection.m_point_list[i].m_symbol; + } std::cout << "\033[H" << std::flush; for(int i = 0; i < ROW; i++) { for(int j = 0; j < COL; j++) { @@ -13,5 +17,5 @@ void Window::draw_window() { } std::cout << "\r\n"; } - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + std::this_thread::sleep_for(std::chrono::milliseconds(33)); }