refactor: removing a bunch of random bad code and fixing it with more readable code
This commit is contained in:
+20
-23
@@ -1,6 +1,9 @@
|
||||
#include "element.hpp"
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
|
||||
int _score = 0;
|
||||
|
||||
void Point::set_new_point(int row, int col, int dir) {
|
||||
m_row = row;
|
||||
@@ -8,13 +11,19 @@ void Point::set_new_point(int row, int col, int 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]) {
|
||||
int row = (rand() % ROW);
|
||||
int col = (rand() % COL);
|
||||
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;
|
||||
if(m_point.m_row == head.m_head.m_row && m_point.m_col == head.m_head.m_col) {
|
||||
do {
|
||||
@@ -22,7 +31,7 @@ void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head, int &score) {
|
||||
col = (rand() % COL);
|
||||
} while (board[row][col] != '.');
|
||||
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++) {
|
||||
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) {
|
||||
m_collision = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Body::check_new_tail() {
|
||||
if(m_score >= 1) {
|
||||
m_score = 0;
|
||||
m_body.push_back(Tail{});
|
||||
}
|
||||
if(_score > m_body.size()) m_body.push_back(Tail{});
|
||||
}
|
||||
|
||||
void Body::get_new_pos(Head &head) {
|
||||
for (int i = 0; i < m_body.size(); i++) {
|
||||
m_body.front().m_previous_tail_pos.set_new_point(
|
||||
head.m_previous_head.m_row,
|
||||
head.m_previous_head.m_col,
|
||||
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);
|
||||
}
|
||||
if(m_body.empty()) return;
|
||||
m_body[0].m_previous_tail_pos.set_new_point(head.m_previous_head);
|
||||
for (int i = 1; i < m_body.size(); i++) {
|
||||
m_body.at(i).m_previous_tail_pos.set_new_point(m_body[i - 1].m_tail_pos);
|
||||
}
|
||||
for (int i = 0; i < m_body.size(); i++) {
|
||||
m_body.at(i).m_tail_pos.set_new_point(
|
||||
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);
|
||||
m_body.at(i).m_tail_pos.set_new_point(m_body[i].m_previous_tail_pos);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user