From 3091026d95d771c5ec6431ada52abf49a8db294e Mon Sep 17 00:00:00 2001 From: DarthMooMancer Date: Tue, 16 Sep 2025 20:24:53 -0400 Subject: [PATCH] feat: move with collision but doesnt work --- include/elements.hpp | 3 ++ include/input.hpp | 3 +- include/window.hpp | 3 +- src/elements.cpp | 115 ++++++++++++++++++++++++------------------- src/input.cpp | 22 ++++++++- src/main.cpp | 41 ++++++++++----- src/window.cpp | 9 ++-- 7 files changed, 123 insertions(+), 73 deletions(-) diff --git a/include/elements.hpp b/include/elements.hpp index 370c987..8cd0dc3 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -2,6 +2,8 @@ #define ELEMENTS_HPP #include +#include +#include "globals.hpp" struct Point { Point() : m_row(0), m_col(0), m_symbol(2), m_direction('n'), m_collide(false) {}; @@ -19,6 +21,7 @@ struct Point { struct PointCollection { std::vector m_point_list; void move(); + void move(Point* m_board[ROW][COL]); void set_direction(char direction); void check_collision(); }; diff --git a/include/input.hpp b/include/input.hpp index b4b93a1..0aefcd1 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -2,10 +2,11 @@ #define INPUT_HPP #include "elements.hpp" +#include "globals.hpp" struct Input { int _getch; - void get_input(bool &running, PointCollection &collection); + void get_input(bool &running, PointCollection &collection, Point* board[ROW][COL]); }; #endif diff --git a/include/window.hpp b/include/window.hpp index 6859e7c..b0ecdc7 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -6,7 +6,8 @@ #include struct Window { - std::array, ROW> m_board = {}; + // std::array, ROW> m_board = {}; + Point* m_board[ROW][COL] = { nullptr }; void draw_window(PointCollection &collection); }; diff --git a/src/elements.cpp b/src/elements.cpp index d22f89c..8b5e53a 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -1,5 +1,4 @@ #include "elements.hpp" -#include "globals.hpp" #include #include #include @@ -26,58 +25,70 @@ void PointCollection::set_direction(char direction) { } } -void PointCollection::move() { +void PointCollection::move(Point* m_board[ROW][COL]) { 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); + Point &p = m_point_list[i]; + + int cur_row = p.m_row; + int cur_col = p.m_col; + int new_row = cur_row; + int new_col = cur_col; + + if(p.m_direction == 'u') { + Point *target = m_board[p.m_row - 1][p.m_col]; + if(target == nullptr && p.m_row >= 0) { + new_row--; + } + else if(target->m_symbol == m_board[cur_row][cur_col]->m_symbol + && !target->m_collide) { + target->m_symbol *= 2; + target->m_collide = true; + m_board[cur_row][cur_col] = nullptr; + continue; + } } + else if(p.m_direction == 'd') { + Point *target = m_board[p.m_row + 1][p.m_col]; + + if(target == nullptr && p.m_row < ROW) { + new_row++; + } + else if(target->m_symbol == m_board[cur_row][cur_col]->m_symbol + && !target->m_collide) { + target->m_symbol *= 2; + target->m_collide = true; + m_board[cur_row][cur_col] = nullptr; + continue; + } + } else if(p.m_direction == 'l') { + Point *target = m_board[p.m_row][p.m_col - 1]; + + if(target == nullptr && p.m_col >= 0) { + new_col--; + } + else if(target->m_symbol == m_board[cur_row][cur_col]->m_symbol + && !target->m_collide) { + target->m_symbol *= 2; + target->m_collide = true; + m_board[cur_row][cur_col] = nullptr; + continue; + } + } else if(p.m_direction == 'r') { + Point *target = m_board[p.m_row][p.m_col + 1]; + + if(target == nullptr && p.m_col < ROW) { + new_col++; + } + else if(target->m_symbol == m_board[cur_row][cur_col]->m_symbol + && !target->m_collide) { + target->m_symbol *= 2; + target->m_collide = true; + m_board[cur_row][cur_col] = nullptr; + continue; + } + } + m_board[p.m_row][p.m_col] = nullptr; + p.assign(new_row, new_col); + m_board[new_row][new_col] = &p; } } - -// bool _check_collision(Point &a, Point &b) { -// if(a.m_row == b.m_row && a.m_col == b.m_col && a.m_symbol == b.m_symbol) { -// return true; -// } -// return false; -// } - -// void PointCollection::check_collision() { - // for(int i = m_point_list.size() - 1; i >= 0; i--) { - // for(int a = m_point_list.size() - 1; a >= 0; a--) { - // if (i == a) continue; - // _check_collision(m_point_list[i], m_point_list[a]); - // if(m_point_list[i].m_row == m_point_list[a].m_row) { - // if(m_point_list[i].m_col == m_point_list[a].m_col) { - // if (m_point_list[i].m_symbol == m_point_list[a].m_symbol) { - // if(m_point_list[i].m_direction == 'u') { - // if(!m_point_list[a].m_collide) { - // m_point_list[a].m_collide = true; - // m_point_list[a].m_symbol *= 2; - // m_point_list.erase(m_point_list.begin() + i); - // } - // else { - // m_point_list[i].m_row++; - // } - // } - // if(m_point_list[i].m_direction == 'd') { - // } - // if(m_point_list[i].m_direction == 'l') { - // } - // if(m_point_list[i].m_direction == 'r') { - // } - // } - // } - // } -// } -// } -// } diff --git a/src/input.cpp b/src/input.cpp index 8e45df5..42ce458 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) { +void Input::get_input(bool &running, PointCollection &collection, Point* board[ROW][COL]) { while (running) { initscr(); cbreak(); @@ -12,24 +12,44 @@ void Input::get_input(bool &running, PointCollection &collection) { if(_getch == 65) { _getch = 0; + for(int i = 0; i < collection.m_point_list.size() - 1; i++) { + if(!collection.m_point_list[i].m_collide) { + collection.m_point_list[i].m_collide = true; + } + } collection.set_direction('u'); collection.m_point_list.push_back(Point()); collection.m_point_list.back().assign(); } else if(_getch == 66) { _getch = 0; + for(int i = 0; i < collection.m_point_list.size() - 1; i++) { + if(!collection.m_point_list[i].m_collide) { + collection.m_point_list[i].m_collide = true; + } + } collection.set_direction('d'); collection.m_point_list.push_back(Point()); collection.m_point_list.back().assign(); } else if(_getch == 67) { _getch = 0; + for(int i = 0; i < collection.m_point_list.size() - 1; i++) { + if(!collection.m_point_list[i].m_collide) { + collection.m_point_list[i].m_collide = true; + } + } collection.set_direction('r'); collection.m_point_list.push_back(Point()); collection.m_point_list.back().assign(); } else if(_getch == 68) { _getch = 0; + for(int i = 0; i < collection.m_point_list.size() - 1; i++) { + if(!collection.m_point_list[i].m_collide) { + collection.m_point_list[i].m_collide = true; + } + } collection.set_direction('l'); collection.m_point_list.push_back(Point()); collection.m_point_list.back().assign(); diff --git a/src/main.cpp b/src/main.cpp index 315e3eb..0333227 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,13 +11,29 @@ bool _check_collision(Point &a, Point &b) { return false; } +void _increment_point(Point &p) { + p.m_symbol *= 2; +} + void _handle_point(PointCollection &collection, int index, Point &a, Point &b) { if(a.m_symbol == b.m_symbol && !b.m_collide) { collection.m_point_list.erase(collection.m_point_list.begin() + index); + _increment_point(b); b.m_collide = true; } - if(!b.m_collide) { - // set a to be one behind + else if(b.m_collide) { + if(a.m_symbol == 'u' && b.m_symbol == 'u') { + a.assign(b.m_row - 1, a.m_col); + } + if(a.m_symbol == 'd' && b.m_symbol == 'd') { + a.assign(b.m_row + 1, a.m_col); + } + if(a.m_symbol == 'l' && b.m_symbol == 'l') { + a.assign(b.m_row, a.m_col + 1); + } + if(a.m_symbol == 'r' && b.m_symbol == 'r') { + a.assign(b.m_row, a.m_col - 1); + } } } @@ -26,24 +42,23 @@ int main() { Window window; PointCollection collection; bool running = true; - std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(collection)); + 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.back().assign(); while (running) { // collection.check_collision(); - collection.move(); + collection.move(window.m_board); window.draw_window(collection); - for(int i = collection.m_point_list.size() - 1; i >= 0; i--) { - for(int a = collection.m_point_list.size() - 1; a >= 0; a--) { - if (i == a) continue; - if(_check_collision(collection.m_point_list[i], collection.m_point_list[a])) { - _handle_point(collection, i, collection.m_point_list[i], collection.m_point_list[a]); - } - } - } - std::cout << collection.m_point_list.size() << "\n"; + // for(int i = collection.m_point_list.size() - 1; i >= 0; i--) { + // for(int a = collection.m_point_list.size() - 1; a >= 0; a--) { + // if (i == a) break; + // if(_check_collision(collection.m_point_list[i], collection.m_point_list[a])) { + // _handle_point(collection, i, collection.m_point_list[i], collection.m_point_list[a]); + // } + // } + // } } input_thread.join(); diff --git a/src/window.cpp b/src/window.cpp index f118f74..3a77438 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,19 +1,18 @@ #include "window.hpp" -#include #include #include #include void Window::draw_window(PointCollection &collection) { - for(std::array &row : m_board) { row.fill(0); } + 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].m_symbol; + 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++) { - if(m_board[i][j] == 0) std::cout << std::setw(4) << '.'; - else std::cout << std::setw(4) << m_board[i][j]; + if(m_board[i][j] == nullptr) std::cout << std::setw(4) << '.'; + else std::cout << std::setw(4) << m_board[i][j]->m_symbol; } std::cout << "\r\n"; }