diff --git a/include/elements.hpp b/include/elements.hpp index a37020c..ef293b5 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -5,6 +5,10 @@ #include #include "globals.hpp" +struct Point; +void move(Point* board[ROW][COL]); +void create_point_on_board(Point* board[ROW][COL]); + 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') {}; @@ -19,9 +23,8 @@ struct Point { struct PointCollection { std::vector m_point_list; - void move(Point* m_board[ROW][COL]); void collide_with_adjacent(Point* board[ROW][COL]); - void set_direction(char direction); + void set_direction(char direction, Point* board[ROW][COL]); }; #endif diff --git a/src/elements.cpp b/src/elements.cpp index 4377e43..29c9050 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -3,8 +3,8 @@ #include void Point::assign() { - m_row = (rand() % (ROW)); - m_col = (rand() % (ROW)); + m_row = (rand() % ROW); + m_col = (rand() % COL); } void Point::assign(int row, int col) { @@ -16,13 +16,24 @@ std::string Point::to_string() { return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + " )\n"; } -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::set_direction(char direction, Point* board[ROW][COL]) { + for(int i = 0; i < ROW; i++) { + for(int j = 0; j < ROW; j++) { + if(board[i][j] == nullptr) continue; + board[i][j]->m_direction = direction; + } } } -void PointCollection::move(Point* board[ROW][COL]) { +void create_point_on_board(Point* board[ROW][COL]) { + int row = (rand() % (ROW - 1)); + int col = (rand() % (COL - 1)); + if(board[row][col] == nullptr) { + board[row][col] = new Point(row, col); + } +} + +void move(Point* board[ROW][COL]) { for(int r = 0; r < ROW; r++) { for(int c = 0; c < COL; c++) { Point *p = board[r][c]; @@ -64,6 +75,7 @@ void PointCollection::collide_with_adjacent(Point* board[ROW][COL]) { if(p->m_direction == 'u' && npu != nullptr && npu->m_row >= 0) { if(p->m_symbol != npu->m_symbol) continue; npu->m_symbol *= 2; + delete board[r][c]; board[r][c] = nullptr; // Board is not cleared as the point still exists in m_points_list so it needs to be removed // I recommend adding an id to the points to track which to remove and make a function in diff --git a/src/input.cpp b/src/input.cpp index 200e492..1bbbf32 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -12,25 +12,25 @@ void Input::get_input(bool &running, PointCollection &collection, Point* board[R if(_getch == 65) { _getch = 0; - collection.set_direction('u'); + collection.set_direction('u', board); // collection.m_point_list.push_back(Point()); // collection.m_point_list.back().assign(); } else if(_getch == 66) { _getch = 0; - collection.set_direction('d'); + collection.set_direction('d', board); // collection.m_point_list.push_back(Point()); // collection.m_point_list.back().assign(); } else if(_getch == 67) { _getch = 0; - collection.set_direction('r'); + collection.set_direction('r', board); // collection.m_point_list.push_back(Point()); // collection.m_point_list.back().assign(); } else if(_getch == 68) { _getch = 0; - collection.set_direction('l'); + collection.set_direction('l', board); // collection.m_point_list.push_back(Point()); // collection.m_point_list.back().assign(); } diff --git a/src/main.cpp b/src/main.cpp index 3a38e61..708385d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include #include #include "input.hpp" #include "window.hpp" @@ -11,13 +12,20 @@ int main() { bool running = true; std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(collection), std::ref(window.m_board)); - collection.m_point_list.push_back(Point()); - collection.m_point_list[0].assign(); - collection.m_point_list.push_back(Point()); - collection.m_point_list[1].assign(); + // collection.m_point_list.push_back(Point()); + // collection.m_point_list[0].assign(); + // collection.m_point_list.push_back(Point()); + // collection.m_point_list[1].assign(); + create_point_on_board(window.m_board); + + for(int i = 0; i < 4; i++) { + for(int j = 0; j < 4; j++) { + std::cout << window.m_board[i][j] << "\n"; + } + } while (running) { - collection.move(window.m_board); + move(window.m_board); collection.collide_with_adjacent(window.m_board); window.draw_window(collection); } diff --git a/src/window.cpp b/src/window.cpp index 3a77438..c4df270 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -4,10 +4,10 @@ #include void Window::draw_window(PointCollection &collection) { - for(Point** row = m_board[0]; row != m_board[0] + 16; row++) { *row = nullptr; } - 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]; - } + // for(Point** row = m_board[0]; row != m_board[0] + 16; row++) { *row = nullptr; } + // 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]; + // } std::cout << "\033[H" << std::flush; for(int i = 0; i < ROW; i++) { for(int j = 0; j < COL; j++) {