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
#define ELEMENTS_HPP
#include <vector>
#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') {};
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<Point> 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
+1 -1
View File
@@ -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
+2 -3
View File
@@ -3,12 +3,11 @@
#include "globals.hpp"
#include "elements.hpp"
#include <array>
struct Window {
// std::array<std::array<int, COL>, ROW> m_board = {};
Point* m_board[ROW][COL] = { nullptr };
void draw_window(PointCollection &collection);
void draw_window();
void tick(int milliseconds);
};
#endif
+39 -53
View File
@@ -2,21 +2,7 @@
#include <cstdlib>
#include <ctime>
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) {
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;
}
}
}
}
+33 -13
View File
@@ -1,7 +1,7 @@
#include "input.hpp"
#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) {
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;
}
+6 -16
View File
@@ -1,4 +1,3 @@
#include <iostream>
#include <thread>
#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();
+5 -6
View File
@@ -3,11 +3,11 @@
#include <iostream>
#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];
// }
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));
}