removing points list and trying to work with the board along

This commit is contained in:
DarthMooMancer
2025-09-18 21:32:53 -04:00
parent b446c4c158
commit 28bd852531
5 changed files with 44 additions and 21 deletions
+5 -2
View File
@@ -5,6 +5,10 @@
#include <string>
#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<Point> 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
+18 -6
View File
@@ -3,8 +3,8 @@
#include <ctime>
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
+4 -4
View File
@@ -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();
}
+13 -5
View File
@@ -1,3 +1,4 @@
#include <iostream>
#include <thread>
#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);
}
+4 -4
View File
@@ -4,10 +4,10 @@
#include <thread>
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++) {