This commit is contained in:
DarthMooMancer
2025-09-12 22:46:59 -04:00
parent 9d3f6b3308
commit 626d7261d4
4 changed files with 23 additions and 38 deletions
+5 -12
View File
@@ -4,27 +4,20 @@
#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;
int m_direction;
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) {} void set_new_point(int row, int col);
void set_new_point(int row, int col, int dir);
void set_new_point(Point &point); void set_new_point(Point &point);
}; };
struct Head { struct Head {
Head() : m_head(5, 5, 2) {}; Head() : m_direction(2) {};
Point m_head; Point m_head {5, 5};
Point m_previous_head; Point m_previous_head;
int m_direction;
void get_new_pos(); void get_new_pos();
}; };
@@ -41,7 +34,7 @@ struct Body {
}; };
struct Fruit { struct Fruit {
Fruit(char (&board)[ROW][COL]); Fruit();
Point m_point; Point m_point;
void get_new_pos(char (&board)[ROW][COL], Head &head); void get_new_pos(char (&board)[ROW][COL], Head &head);
}; };
+14 -22
View File
@@ -1,26 +1,20 @@
#include "element.hpp" #include "element.hpp"
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <vector>
int _score = 0; int _score = 0;
void Point::set_new_point(int row, int col, int dir) { void Point::set_new_point(int row, int col) {
m_row = row; m_row = row;
m_col = col; m_col = col;
m_direction = dir;
} }
void Point::set_new_point(Point &point) { void Point::set_new_point(Point &point) {
m_row = point.m_row; set_new_point(point.m_row, point.m_col);
m_col = point.m_col;
m_direction = point.m_direction;
} }
Fruit::Fruit(char (&board)[ROW][COL]) { Fruit::Fruit() {
int row = (rand() % ROW); m_point.set_new_point((rand() % ROW), (rand() % COL));
int col = (rand() % COL);
m_point.set_new_point(row, col, 0);
} }
void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head) { void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head) {
@@ -30,22 +24,22 @@ void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head) {
row = (rand() % ROW); row = (rand() % ROW);
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);
_score++; _score++;
} }
} }
void Head::get_new_pos() { void Head::get_new_pos() {
if (m_head.m_direction == -1) { if (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;
else m_head.m_col--; else m_head.m_col--;
} else if (m_head.m_direction == 1) { } else if (m_direction == 1) {
if (m_head.m_col >= COL - 1) m_head.m_col = 0; if (m_head.m_col >= COL - 1) m_head.m_col = 0;
else m_head.m_col++; else m_head.m_col++;
} else if (m_head.m_direction == -2) { } else if (m_direction == -2) {
if (m_head.m_row <= 0) m_head.m_row = ROW - 1; if (m_head.m_row <= 0) m_head.m_row = ROW - 1;
else m_head.m_row--; else m_head.m_row--;
} else if (m_head.m_direction == 2) { } else if (m_direction == 2) {
if (m_head.m_row >= ROW - 1) m_head.m_row = 0; if (m_head.m_row >= ROW - 1) m_head.m_row = 0;
else m_head.m_row++; else m_head.m_row++;
} }
@@ -53,11 +47,9 @@ void Head::get_new_pos() {
bool 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[i].m_tail_pos.m_row != head.m_head.m_row) continue;
if(m_body.at(i).m_tail_pos.m_col == head.m_head.m_col) { if(m_body[i].m_tail_pos.m_col != head.m_head.m_col) continue;
return true; return true;
}
}
} }
return false; return false;
} }
@@ -70,9 +62,9 @@ void Body::get_new_pos(Head &head) {
if(m_body.empty()) return; if(m_body.empty()) return;
m_body[0].m_previous_tail_pos.set_new_point(head.m_previous_head); m_body[0].m_previous_tail_pos.set_new_point(head.m_previous_head);
for (int i = 1; i < m_body.size(); i++) { 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); m_body[i].m_previous_tail_pos.set_new_point(m_body[i - 1].m_tail_pos);
} }
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[i].m_previous_tail_pos); m_body[i].m_tail_pos.set_new_point(m_body[i].m_previous_tail_pos);
} }
} }
+3 -3
View File
@@ -9,13 +9,13 @@ int main () {
std::srand(std::time(0)); std::srand(std::time(0));
Window window; Window window;
Input input; Input input;
Fruit fruit(window.m_board); Fruit fruit;
Body body; Body body;
Head head; Head head;
bool running = true; bool running = true;
std::thread input_thread( std::thread input_thread(
&Input::get_input, &input, &Input::get_input, &input,
std::ref(head.m_head.m_direction), std::ref(head.m_direction),
std::ref(running)); std::ref(running));
while (running) { while (running) {
@@ -31,7 +31,7 @@ int main () {
fruit.get_new_pos(window.m_board, head); 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);
window.update_display(fruit, head, body); window.update_display(fruit, head, body);
} }
+1 -1
View File
@@ -10,7 +10,7 @@ void Window::update_display(Fruit &fruit, Head &head, Body &body) {
} }
} }
m_board[head.m_head.m_row][head.m_head.m_col] = '*'; m_board[head.m_head.m_row][head.m_head.m_col] = '*';
m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X'; m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'o';
for(int i = 0; i < body.m_body.size(); i++) { for(int i = 0; i < body.m_body.size(); i++) {
m_board[body.m_body.at(i).m_tail_pos.m_row][body.m_body.at(i).m_tail_pos.m_col] = '@'; m_board[body.m_body.at(i).m_tail_pos.m_row][body.m_body.at(i).m_tail_pos.m_col] = '@';
} }