From f7cb337d8f6b51336f69fbd4eba2da57ce69b2ed Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Thu, 18 Sep 2025 22:46:51 -0400 Subject: [PATCH] fix: collision mostly works and game is functional. Some bugs I think I need to work out but good enough to come back to later. The game is playable now --- include/elements.hpp | 24 ++++------- include/input.hpp | 2 +- include/window.hpp | 5 +-- src/elements.cpp | 94 +++++++++++++++++++------------------------- src/input.cpp | 46 ++++++++++++++++------ src/main.cpp | 22 +++-------- src/window.cpp | 11 +++--- 7 files changed, 94 insertions(+), 110 deletions(-) diff --git a/include/elements.hpp b/include/elements.hpp index ef293b5..5dc8cee 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -1,30 +1,20 @@ #ifndef ELEMENTS_HPP #define ELEMENTS_HPP -#include -#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') {}; - char m_direction; + Point(int row, int col) : m_row(row), m_col(col), m_symbol(2), m_direction(' '), m_collide(false) {}; int m_row; int m_col; + char m_direction; int m_symbol; - void assign(); - void assign(int row, int col); - std::string to_string(); + bool m_collide; }; -struct PointCollection { - std::vector m_point_list; - void collide_with_adjacent(Point* board[ROW][COL]); - void set_direction(char direction, Point* board[ROW][COL]); -}; +void move_points(Point* board[ROW][COL]); +void create_point_on_board(Point* board[ROW][COL]); +void collide_with_adjacent_point(Point* board[ROW][COL]); +void set_direction(char direction, Point* board[ROW][COL]); #endif diff --git a/include/input.hpp b/include/input.hpp index 0aefcd1..7a70521 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -6,7 +6,7 @@ struct Input { int _getch; - void get_input(bool &running, PointCollection &collection, Point* board[ROW][COL]); + void get_input(bool &running, Point* board[ROW][COL]); }; #endif diff --git a/include/window.hpp b/include/window.hpp index b0ecdc7..907278d 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -3,12 +3,11 @@ #include "globals.hpp" #include "elements.hpp" -#include struct Window { - // std::array, ROW> m_board = {}; Point* m_board[ROW][COL] = { nullptr }; - void draw_window(PointCollection &collection); + void draw_window(); + void tick(int milliseconds); }; #endif diff --git a/src/elements.cpp b/src/elements.cpp index 29c9050..3dfaf53 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -2,21 +2,7 @@ #include #include -void Point::assign() { - m_row = (rand() % ROW); - m_col = (rand() % COL); -} - -void Point::assign(int row, int col) { - m_row = row; - m_col = col; -} - -std::string Point::to_string() { - return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + " )\n"; -} - -void PointCollection::set_direction(char direction, Point* board[ROW][COL]) { +void 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; @@ -26,44 +12,53 @@ void PointCollection::set_direction(char direction, 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); - } + int row, col; + do { + row = (rand() % ROW); + col = (rand() % COL); + } while (board[row][col] != nullptr); + board[row][col] = new Point(row, col); } -void move(Point* board[ROW][COL]) { +void move_points(Point* board[ROW][COL]) { for(int r = 0; r < ROW; r++) { for(int c = 0; c < COL; c++) { Point *p = board[r][c]; if(p != nullptr) { + int new_row = p->m_row; + int new_col = p->m_col; if(p->m_direction == 'u') { if(p->m_row <= 0) continue; if(board[p->m_row - 1][p->m_col] != nullptr) continue; - p->m_row--; + new_row = p->m_row - 1; } else if(p->m_direction == 'd') { if(p->m_row >= ROW - 1) continue; if(board[p->m_row + 1][p->m_col] != nullptr) continue; - p->m_row++; + new_row = p->m_row + 1; } else if(p->m_direction == 'l') { if(p->m_col <= 0) continue; if(board[p->m_row][p->m_col - 1] != nullptr) continue; - p->m_col--; + new_col = p->m_col - 1; } else if(p->m_direction == 'r') { if(p->m_col >= COL - 1) continue; if(board[p->m_row][p->m_col + 1] != nullptr) continue; - p->m_col++; + new_col = p->m_col + 1; + } + if(new_row != p->m_row || new_col != p->m_col) { + board[new_row][new_col] = p; + board[p->m_row][p->m_col] = nullptr; + p->m_row = new_row; + p->m_col = new_col; } } } } } -void PointCollection::collide_with_adjacent(Point* board[ROW][COL]) { +void collide_with_adjacent_point(Point* board[ROW][COL]) { for(int r = 0; r < ROW; r++) { for(int c = 0; c < COL; c++) { Point* p = board[r][c]; @@ -72,39 +67,30 @@ void PointCollection::collide_with_adjacent(Point* board[ROW][COL]) { Point* npl = board[r][c - 1]; Point* npr = board[r][c + 1]; if(p != nullptr) { - if(p->m_direction == 'u' && npu != nullptr && npu->m_row >= 0) { + if(p->m_direction == 'u' && npu != nullptr && npu->m_direction == 'u' && 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 - // the PointCollection to remove the point } - // else if(p->m_direction == 'd') { - // np = board[p->m_row + 1][p->m_col]; - // if(np->m_row >= ROW - 1) continue; - // if(np == nullptr) continue; - // if(p->m_symbol != np->m_symbol) continue; - // board[r][c] = nullptr; - // np->m_symbol *= 2; - // } - // else if(p->m_direction == 'l') { - // np = board[p->m_row][p->m_col - 1]; - // if(np->m_col <= 0) continue; - // if(np == nullptr) continue; - // if(p->m_symbol != np->m_symbol) continue; - // board[r][c] = nullptr; - // np->m_symbol *= 2; - // } - // else if(p->m_direction == 'r') { - // np = board[p->m_row][p->m_col + 1]; - // if(np->m_col >= COL - 1) continue; - // if(np == nullptr) continue; - // if(p->m_symbol != np->m_symbol) continue; - // board[r][c] = nullptr; - // np->m_symbol *= 2; - // } + else if(p->m_direction == 'd' && npd != nullptr && npd->m_direction == 'd' && npd->m_row < ROW) { + if(p->m_symbol != npd->m_symbol) continue; + npd->m_symbol *= 2; + delete board[r][c]; + board[r][c] = nullptr; + } + else if(p->m_direction == 'l' && npl != nullptr && npl->m_direction == 'l' && npl->m_col >= 0) { + if(p->m_symbol != npl->m_symbol) continue; + npl->m_symbol *= 2; + delete board[r][c]; + board[r][c] = nullptr; + } + else if(p->m_direction == 'r' && npr != nullptr && npr->m_direction == 'r' && npr->m_col < COL) { + if(p->m_symbol != npr->m_symbol) continue; + npr->m_symbol *= 2; + delete board[r][c]; + board[r][c] = nullptr; + } } } } diff --git a/src/input.cpp b/src/input.cpp index 1bbbf32..f815c79 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,7 +1,7 @@ #include "input.hpp" #include -void Input::get_input(bool &running, PointCollection &collection, Point* board[ROW][COL]) { +void Input::get_input(bool &running, Point* board[ROW][COL]) { while (running) { initscr(); cbreak(); @@ -12,27 +12,47 @@ void Input::get_input(bool &running, PointCollection &collection, Point* board[R if(_getch == 65) { _getch = 0; - collection.set_direction('u', board); - // collection.m_point_list.push_back(Point()); - // collection.m_point_list.back().assign(); + set_direction('u', board); + for(int i = 0; i < ROW; i++) { + for(int j = 0; j < ROW; j++) { + if(board[i][j] == nullptr) continue; + board[i][j]->m_collide = false; + } + } + create_point_on_board(board); } else if(_getch == 66) { _getch = 0; - collection.set_direction('d', board); - // collection.m_point_list.push_back(Point()); - // collection.m_point_list.back().assign(); + set_direction('d', board); + for(int i = 0; i < ROW; i++) { + for(int j = 0; j < ROW; j++) { + if(board[i][j] == nullptr) continue; + board[i][j]->m_collide = false; + } + } + create_point_on_board(board); } else if(_getch == 67) { _getch = 0; - collection.set_direction('r', board); - // collection.m_point_list.push_back(Point()); - // collection.m_point_list.back().assign(); + set_direction('r', board); + for(int i = 0; i < ROW; i++) { + for(int j = 0; j < ROW; j++) { + if(board[i][j] == nullptr) continue; + board[i][j]->m_collide = false; + } + } + create_point_on_board(board); } else if(_getch == 68) { _getch = 0; - collection.set_direction('l', board); - // collection.m_point_list.push_back(Point()); - // collection.m_point_list.back().assign(); + set_direction('l', board); + for(int i = 0; i < ROW; i++) { + for(int j = 0; j < ROW; j++) { + if(board[i][j] == nullptr) continue; + board[i][j]->m_collide = false; + } + } + create_point_on_board(board); } else if(_getch == 113) running = false; } diff --git a/src/main.cpp b/src/main.cpp index 708385d..6e7c822 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ -#include #include #include "input.hpp" #include "window.hpp" @@ -8,26 +7,17 @@ int main() { std::srand(std::time(0)); Input input; Window window; - PointCollection collection; bool running = true; - std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(collection), std::ref(window.m_board)); + std::thread input_thread(&Input::get_input, &input, std::ref(running), 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(); + create_point_on_board(window.m_board); 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) { - move(window.m_board); - collection.collide_with_adjacent(window.m_board); - window.draw_window(collection); + window.tick(16); + collide_with_adjacent_point(window.m_board); + move_points(window.m_board); + window.draw_window(); } input_thread.join(); diff --git a/src/window.cpp b/src/window.cpp index c4df270..e3cfb9f 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,11 +3,11 @@ #include #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]; - // } +void Window::tick(int milliseconds) { + std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); +} + +void Window::draw_window() { std::cout << "\033[H" << std::flush; for(int i = 0; i < ROW; i++) { for(int j = 0; j < COL; j++) { @@ -16,5 +16,4 @@ void Window::draw_window(PointCollection &collection) { } std::cout << "\r\n"; } - std::this_thread::sleep_for(std::chrono::milliseconds(16)); }