fruit: fixed bug where fruit could generate under tail

This commit is contained in:
DarthMooMancer
2025-09-12 20:55:56 -04:00
parent 4bab69a3a8
commit e36ab27f2b
3 changed files with 21 additions and 18 deletions
+9 -4
View File
@@ -4,6 +4,12 @@
#include <vector> #include <vector>
#include "globals.hpp" #include "globals.hpp"
struct Point;
struct Head;
struct Tail;
struct Body;
struct Fruit;
struct Point { struct Point {
int m_row; int m_row;
int m_col; int m_col;
@@ -11,12 +17,11 @@ struct Point {
Point() {} 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);
}; };
struct Head { struct Head {
Head(); Head() : m_head(5, 5, 2) {};
Point m_head; Point m_head;
Point m_previous_head; Point m_previous_head;
void get_new_pos(); void get_new_pos();
@@ -28,7 +33,7 @@ struct Tail {
}; };
struct Body { struct Body {
Body(); Body() : m_score(0), m_collision(false) {};
std::vector<Tail> m_body; std::vector<Tail> m_body;
int m_score; int m_score;
bool m_collision; bool m_collision;
@@ -38,7 +43,7 @@ struct Body {
}; };
struct Fruit { struct Fruit {
Fruit(); 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, int &score);
}; };
+10 -13
View File
@@ -8,22 +8,24 @@ void Point::set_new_point(int row, int col, int dir) {
m_direction = dir; m_direction = dir;
} }
Fruit::Fruit() { Fruit::Fruit(char (&board)[ROW][COL]) {
std::srand(std::time(0)); int row = (rand() % ROW);
m_point.set_new_point((rand() % 9), (rand() % 9), 0); 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 &score) {
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) {
m_point.set_new_point((rand() % 9), (rand() % 9), 0); do {
row = (rand() % ROW);
col = (rand() % COL);
} while (board[row][col] != '.');
m_point.set_new_point(row, col, 0);
score++; score++;
} }
} }
Head::Head() {
m_head.set_new_point(5, 5, 2);
}
void Head::get_new_pos() { void Head::get_new_pos() {
if (m_head.m_direction == -1) { if (m_head.m_direction == -1) {
if (m_head.m_col <= 0) m_head.m_col = COL - 1; if (m_head.m_col <= 0) m_head.m_col = COL - 1;
@@ -40,11 +42,6 @@ void Head::get_new_pos() {
} }
} }
Body::Body() {
m_score = 0;
m_collision = false;
}
void Body::check_collision(Head &head) { void 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) {
+2 -1
View File
@@ -6,9 +6,10 @@
#include "input.hpp" #include "input.hpp"
int main () { int main () {
std::srand(std::time(0));
Window window; Window window;
Input input; Input input;
Fruit fruit; Fruit fruit(window.m_board);
Body body; Body body;
Head head; Head head;
bool running = true; bool running = true;