refactor: removing a bunch of random bad code and fixing it with more readable code

This commit is contained in:
DarthMooMancer
2025-09-12 22:18:36 -04:00
parent e36ab27f2b
commit 9d3f6b3308
6 changed files with 49 additions and 51 deletions
+3 -5
View File
@@ -18,6 +18,7 @@ struct Point {
Point(int x, int y) : m_row(x), m_col(y) {} Point(int x, int y) : m_row(x), m_col(y) {}
Point(int x, int y, int dir) : m_row(x), m_col(y), m_direction(dir) {} Point(int x, int y, int dir) : m_row(x), m_col(y), m_direction(dir) {}
void set_new_point(int row, int col, int dir); void set_new_point(int row, int col, int dir);
void set_new_point(Point &point);
}; };
struct Head { struct Head {
@@ -33,19 +34,16 @@ struct Tail {
}; };
struct Body { struct Body {
Body() : m_score(0), m_collision(false) {};
std::vector<Tail> m_body; std::vector<Tail> m_body;
int m_score;
bool m_collision;
void check_new_tail(); void check_new_tail();
void check_collision(Head &head); bool check_collision(Head &head);
void get_new_pos(Head &head); void get_new_pos(Head &head);
}; };
struct Fruit { struct Fruit {
Fruit(char (&board)[ROW][COL]); Fruit(char (&board)[ROW][COL]);
Point m_point; Point m_point;
void get_new_pos(char (&board)[ROW][COL], Head &head, int &score); void get_new_pos(char (&board)[ROW][COL], Head &head);
}; };
#endif // ELEMENT_HPP #endif // ELEMENT_HPP
-1
View File
@@ -2,5 +2,4 @@
#define GLOBALS_HPP #define GLOBALS_HPP
#define ROW 10 #define ROW 10
#define COL 10 #define COL 10
#endif // GLOBALS_HPP #endif // GLOBALS_HPP
+1 -1
View File
@@ -3,7 +3,7 @@
struct Input { struct Input {
int m_current_key = 0; int m_current_key = 0;
char m_getch = ' '; int m_getch = 0;
void get_input(int &d, bool &r); void get_input(int &d, bool &r);
}; };
+20 -23
View File
@@ -1,6 +1,9 @@
#include "element.hpp" #include "element.hpp"
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <vector>
int _score = 0;
void Point::set_new_point(int row, int col, int dir) { void Point::set_new_point(int row, int col, int dir) {
m_row = row; m_row = row;
@@ -8,13 +11,19 @@ void Point::set_new_point(int row, int col, int dir) {
m_direction = dir; m_direction = dir;
} }
void Point::set_new_point(Point &point) {
m_row = point.m_row;
m_col = point.m_col;
m_direction = point.m_direction;
}
Fruit::Fruit(char (&board)[ROW][COL]) { Fruit::Fruit(char (&board)[ROW][COL]) {
int row = (rand() % ROW); int row = (rand() % ROW);
int col = (rand() % COL); int col = (rand() % COL);
m_point.set_new_point(row, col, 0); m_point.set_new_point(row, col, 0);
} }
void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head, int &score) { void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head) {
int row, col; int row, col;
if(m_point.m_row == head.m_head.m_row && m_point.m_col == head.m_head.m_col) { if(m_point.m_row == head.m_head.m_row && m_point.m_col == head.m_head.m_col) {
do { do {
@@ -22,7 +31,7 @@ void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head, int &score) {
col = (rand() % COL); col = (rand() % COL);
} while (board[row][col] != '.'); } while (board[row][col] != '.');
m_point.set_new_point(row, col, 0); m_point.set_new_point(row, col, 0);
score++; _score++;
} }
} }
@@ -42,40 +51,28 @@ void Head::get_new_pos() {
} }
} }
void Body::check_collision(Head &head) { bool Body::check_collision(Head &head) {
for(int i = 0; i < m_body.size(); i++) { for(int i = 0; i < m_body.size(); i++) {
if(m_body.at(i).m_tail_pos.m_row == head.m_head.m_row) { if(m_body.at(i).m_tail_pos.m_row == head.m_head.m_row) {
if(m_body.at(i).m_tail_pos.m_col == head.m_head.m_col) { if(m_body.at(i).m_tail_pos.m_col == head.m_head.m_col) {
m_collision = true; return true;
} }
} }
} }
return false;
} }
void Body::check_new_tail() { void Body::check_new_tail() {
if(m_score >= 1) { if(_score > m_body.size()) m_body.push_back(Tail{});
m_score = 0;
m_body.push_back(Tail{});
}
} }
void Body::get_new_pos(Head &head) { void Body::get_new_pos(Head &head) {
for (int i = 0; i < m_body.size(); i++) { if(m_body.empty()) return;
m_body.front().m_previous_tail_pos.set_new_point( m_body[0].m_previous_tail_pos.set_new_point(head.m_previous_head);
head.m_previous_head.m_row, for (int i = 1; i < m_body.size(); i++) {
head.m_previous_head.m_col, m_body.at(i).m_previous_tail_pos.set_new_point(m_body[i - 1].m_tail_pos);
head.m_previous_head.m_direction);
if(i > 0) {
m_body.at(i).m_previous_tail_pos.set_new_point(
m_body.at(i - 1).m_tail_pos.m_row,
m_body.at(i - 1).m_tail_pos.m_col,
m_body.at(i - 1).m_tail_pos.m_direction);
}
} }
for (int i = 0; i < m_body.size(); i++) { for (int i = 0; i < m_body.size(); i++) {
m_body.at(i).m_tail_pos.set_new_point( m_body.at(i).m_tail_pos.set_new_point(m_body[i].m_previous_tail_pos);
m_body.at(i).m_previous_tail_pos.m_row,
m_body.at(i).m_previous_tail_pos.m_col,
m_body.at(i).m_previous_tail_pos.m_direction);
} }
} }
+15 -10
View File
@@ -9,17 +9,22 @@ void Input::get_input(int &direction, bool &running) {
m_getch = getch(); m_getch = getch();
endwin(); endwin();
if(m_current_key != 1 && (m_getch == 'a' || m_getch == 'D')) { switch (m_getch) {
direction = m_current_key = -1; case 65:
} else if(m_current_key != - 1 && (m_getch == 'd' || m_getch == 'C')) { if(m_current_key != 2) direction = m_current_key = -2;
direction = m_current_key = 1; break;
} else if(m_current_key != -2 && (m_getch == 's' || m_getch == 'B')) { case 66:
direction = m_current_key = 2; if(m_current_key != -2) direction = m_current_key = 2;
} else if(m_current_key != 2 && (m_getch == 'w' || m_getch == 'A')) { break;
direction = m_current_key = -2; case 67:
} else if(m_getch == 'q') { if(m_current_key != -1) direction = m_current_key = 1;
break;
case 68:
if(m_current_key != 1) direction = m_current_key = -1;
break;
case 113:
running = false; running = false;
break;
} }
} }
} }
+2 -3
View File
@@ -19,17 +19,16 @@ int main () {
std::ref(running)); std::ref(running));
while (running) { while (running) {
body.check_collision(head);
body.check_new_tail(); body.check_new_tail();
if(body.m_body.size() >= (ROW * COL)) { if(body.m_body.size() >= (ROW * COL)) {
std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl; std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
running = false; running = false;
} else if(body.m_collision) { } else if(body.check_collision(head)) {
std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl; std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
running = false; running = false;
} }
head.get_new_pos(); head.get_new_pos();
fruit.get_new_pos(window.m_board, head, body.m_score); fruit.get_new_pos(window.m_board, head);
body.get_new_pos(head); body.get_new_pos(head);
head.m_previous_head.set_new_point(head.m_head.m_row, head.m_head.m_col, head.m_head.m_direction); head.m_previous_head.set_new_point(head.m_head.m_row, head.m_head.m_col, head.m_head.m_direction);