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

This commit is contained in:
DarthMooMancer
2025-09-18 22:46:51 -04:00
parent 28bd852531
commit f7cb337d8f
7 changed files with 94 additions and 110 deletions
+7 -17
View File
@@ -1,30 +1,20 @@
#ifndef ELEMENTS_HPP #ifndef ELEMENTS_HPP
#define ELEMENTS_HPP #define ELEMENTS_HPP
#include <vector>
#include <string>
#include "globals.hpp" #include "globals.hpp"
struct Point;
void move(Point* board[ROW][COL]);
void create_point_on_board(Point* board[ROW][COL]);
struct Point { 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(' '), m_collide(false) {};
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_row;
int m_col; int m_col;
char m_direction;
int m_symbol; int m_symbol;
void assign(); bool m_collide;
void assign(int row, int col);
std::string to_string();
}; };
struct PointCollection { void move_points(Point* board[ROW][COL]);
std::vector<Point> m_point_list; void create_point_on_board(Point* board[ROW][COL]);
void collide_with_adjacent(Point* board[ROW][COL]); void collide_with_adjacent_point(Point* board[ROW][COL]);
void set_direction(char direction, Point* board[ROW][COL]); void set_direction(char direction, Point* board[ROW][COL]);
};
#endif #endif
+1 -1
View File
@@ -6,7 +6,7 @@
struct Input { struct Input {
int _getch; int _getch;
void get_input(bool &running, PointCollection &collection, Point* board[ROW][COL]); void get_input(bool &running, Point* board[ROW][COL]);
}; };
#endif #endif
+2 -3
View File
@@ -3,12 +3,11 @@
#include "globals.hpp" #include "globals.hpp"
#include "elements.hpp" #include "elements.hpp"
#include <array>
struct Window { struct Window {
// std::array<std::array<int, COL>, ROW> m_board = {};
Point* m_board[ROW][COL] = { nullptr }; Point* m_board[ROW][COL] = { nullptr };
void draw_window(PointCollection &collection); void draw_window();
void tick(int milliseconds);
}; };
#endif #endif
+39 -53
View File
@@ -2,21 +2,7 @@
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
void Point::assign() { void set_direction(char direction, Point* board[ROW][COL]) {
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]) {
for(int i = 0; i < ROW; i++) { for(int i = 0; i < ROW; i++) {
for(int j = 0; j < ROW; j++) { for(int j = 0; j < ROW; j++) {
if(board[i][j] == nullptr) continue; 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]) { void create_point_on_board(Point* board[ROW][COL]) {
int row = (rand() % (ROW - 1)); int row, col;
int col = (rand() % (COL - 1)); do {
if(board[row][col] == nullptr) { row = (rand() % ROW);
col = (rand() % COL);
} while (board[row][col] != nullptr);
board[row][col] = new Point(row, col); 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 r = 0; r < ROW; r++) {
for(int c = 0; c < COL; c++) { for(int c = 0; c < COL; c++) {
Point *p = board[r][c]; Point *p = board[r][c];
if(p != nullptr) { if(p != nullptr) {
int new_row = p->m_row;
int new_col = p->m_col;
if(p->m_direction == 'u') { if(p->m_direction == 'u') {
if(p->m_row <= 0) continue; if(p->m_row <= 0) continue;
if(board[p->m_row - 1][p->m_col] != nullptr) 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') { else if(p->m_direction == 'd') {
if(p->m_row >= ROW - 1) continue; if(p->m_row >= ROW - 1) continue;
if(board[p->m_row + 1][p->m_col] != nullptr) 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') { else if(p->m_direction == 'l') {
if(p->m_col <= 0) continue; if(p->m_col <= 0) continue;
if(board[p->m_row][p->m_col - 1] != nullptr) 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') { else if(p->m_direction == 'r') {
if(p->m_col >= COL - 1) continue; if(p->m_col >= COL - 1) continue;
if(board[p->m_row][p->m_col + 1] != nullptr) 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 r = 0; r < ROW; r++) {
for(int c = 0; c < COL; c++) { for(int c = 0; c < COL; c++) {
Point* p = board[r][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* npl = board[r][c - 1];
Point* npr = board[r][c + 1]; Point* npr = board[r][c + 1];
if(p != nullptr) { 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; if(p->m_symbol != npu->m_symbol) continue;
npu->m_symbol *= 2; npu->m_symbol *= 2;
delete board[r][c]; delete board[r][c];
board[r][c] = nullptr; 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') { else if(p->m_direction == 'd' && npd != nullptr && npd->m_direction == 'd' && npd->m_row < ROW) {
// np = board[p->m_row + 1][p->m_col]; if(p->m_symbol != npd->m_symbol) continue;
// if(np->m_row >= ROW - 1) continue; npd->m_symbol *= 2;
// if(np == nullptr) continue; delete board[r][c];
// if(p->m_symbol != np->m_symbol) continue; board[r][c] = nullptr;
// board[r][c] = nullptr; }
// np->m_symbol *= 2; else if(p->m_direction == 'l' && npl != nullptr && npl->m_direction == 'l' && npl->m_col >= 0) {
// } if(p->m_symbol != npl->m_symbol) continue;
// else if(p->m_direction == 'l') { npl->m_symbol *= 2;
// np = board[p->m_row][p->m_col - 1]; delete board[r][c];
// if(np->m_col <= 0) continue; board[r][c] = nullptr;
// if(np == nullptr) continue; }
// if(p->m_symbol != np->m_symbol) continue; else if(p->m_direction == 'r' && npr != nullptr && npr->m_direction == 'r' && npr->m_col < COL) {
// board[r][c] = nullptr; if(p->m_symbol != npr->m_symbol) continue;
// np->m_symbol *= 2; npr->m_symbol *= 2;
// } delete board[r][c];
// else if(p->m_direction == 'r') { board[r][c] = nullptr;
// 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;
// }
} }
} }
} }
+33 -13
View File
@@ -1,7 +1,7 @@
#include "input.hpp" #include "input.hpp"
#include <ncurses.h> #include <ncurses.h>
void Input::get_input(bool &running, PointCollection &collection, Point* board[ROW][COL]) { void Input::get_input(bool &running, Point* board[ROW][COL]) {
while (running) { while (running) {
initscr(); initscr();
cbreak(); cbreak();
@@ -12,27 +12,47 @@ void Input::get_input(bool &running, PointCollection &collection, Point* board[R
if(_getch == 65) { if(_getch == 65) {
_getch = 0; _getch = 0;
collection.set_direction('u', board); set_direction('u', board);
// collection.m_point_list.push_back(Point()); for(int i = 0; i < ROW; i++) {
// collection.m_point_list.back().assign(); 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) { else if(_getch == 66) {
_getch = 0; _getch = 0;
collection.set_direction('d', board); set_direction('d', board);
// collection.m_point_list.push_back(Point()); for(int i = 0; i < ROW; i++) {
// collection.m_point_list.back().assign(); 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) { else if(_getch == 67) {
_getch = 0; _getch = 0;
collection.set_direction('r', board); set_direction('r', board);
// collection.m_point_list.push_back(Point()); for(int i = 0; i < ROW; i++) {
// collection.m_point_list.back().assign(); 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) { else if(_getch == 68) {
_getch = 0; _getch = 0;
collection.set_direction('l', board); set_direction('l', board);
// collection.m_point_list.push_back(Point()); for(int i = 0; i < ROW; i++) {
// collection.m_point_list.back().assign(); 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; else if(_getch == 113) running = false;
} }
+6 -16
View File
@@ -1,4 +1,3 @@
#include <iostream>
#include <thread> #include <thread>
#include "input.hpp" #include "input.hpp"
#include "window.hpp" #include "window.hpp"
@@ -8,26 +7,17 @@ int main() {
std::srand(std::time(0)); std::srand(std::time(0));
Input input; Input input;
Window window; Window window;
PointCollection collection;
bool running = true; 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()); create_point_on_board(window.m_board);
// 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) { while (running) {
move(window.m_board); window.tick(16);
collection.collide_with_adjacent(window.m_board); collide_with_adjacent_point(window.m_board);
window.draw_window(collection); move_points(window.m_board);
window.draw_window();
} }
input_thread.join(); input_thread.join();
+5 -6
View File
@@ -3,11 +3,11 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
void Window::draw_window(PointCollection &collection) { void Window::tick(int milliseconds) {
// for(Point** row = m_board[0]; row != m_board[0] + 16; row++) { *row = nullptr; } std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
// 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::draw_window() {
std::cout << "\033[H" << std::flush; std::cout << "\033[H" << std::flush;
for(int i = 0; i < ROW; i++) { for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) { for(int j = 0; j < COL; j++) {
@@ -16,5 +16,4 @@ void Window::draw_window(PointCollection &collection) {
} }
std::cout << "\r\n"; std::cout << "\r\n";
} }
std::this_thread::sleep_for(std::chrono::milliseconds(16));
} }