feat: a more intuitive move function

This commit is contained in:
DarthMooMancer
2025-09-17 09:25:08 -04:00
parent 3091026d95
commit d6a769900f
2 changed files with 25 additions and 107 deletions
+25 -65
View File
@@ -1,8 +1,6 @@
#include "elements.hpp" #include "elements.hpp"
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <iostream>
#include <string>
void Point::assign() { void Point::assign() {
std::srand(std::time(0)); std::srand(std::time(0));
@@ -25,70 +23,32 @@ void PointCollection::set_direction(char direction) {
} }
} }
void PointCollection::move(Point* m_board[ROW][COL]) { void PointCollection::move(Point* board[ROW][COL]) {
for(int i = m_point_list.size() - 1; i >= 0; i--) { for(int r = 0; r < ROW; r++) {
Point &p = m_point_list[i]; for(int c = 0; c < COL; c++) {
Point *p = board[r][c];
int cur_row = p.m_row; if(p != nullptr) {
int cur_col = p.m_col; if(p->m_direction == 'u') {
int new_row = cur_row; if(p->m_row <= 0) continue;
int new_col = cur_col; if(board[p->m_row - 1][p->m_col] != nullptr) continue;
p->m_row--;
if(p.m_direction == 'u') { }
Point *target = m_board[p.m_row - 1][p.m_col]; else if(p->m_direction == 'd') {
if(target == nullptr && p.m_row >= 0) { if(p->m_row >= ROW - 1) continue;
new_row--; if(board[p->m_row + 1][p->m_col] != nullptr) continue;
} p->m_row++;
else if(target->m_symbol == m_board[cur_row][cur_col]->m_symbol }
&& !target->m_collide) { else if(p->m_direction == 'l') {
target->m_symbol *= 2; if(p->m_col <= 0) continue;
target->m_collide = true; if(board[p->m_row][p->m_col - 1] != nullptr) continue;
m_board[cur_row][cur_col] = nullptr; p->m_col--;
continue; }
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++;
}
} }
} }
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;
} }
} }
-42
View File
@@ -4,39 +4,6 @@
#include "window.hpp" #include "window.hpp"
#include "elements.hpp" #include "elements.hpp"
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 _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;
}
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);
}
}
}
int main() { int main() {
Input input; Input input;
Window window; Window window;
@@ -48,17 +15,8 @@ int main() {
collection.m_point_list.back().assign(); collection.m_point_list.back().assign();
while (running) { while (running) {
// collection.check_collision();
collection.move(window.m_board); collection.move(window.m_board);
window.draw_window(collection); 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) 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(); input_thread.join();