snake can collide and removed some magic numbers

This commit is contained in:
DarthMooMancer
2025-09-26 22:14:34 -04:00
parent 7a6df4fbe5
commit 74a5d9738a
7 changed files with 57 additions and 30 deletions
+11 -3
View File
@@ -3,20 +3,28 @@
#include "globals.hpp"
enum Direction {
NONE = 0,
UP = -2,
DOWN = 2,
LEFT = -1,
RIGHT = 1
};
struct Point {
int m_row;
int m_col;
char m_symbol;
int m_direction;
enum Direction m_dir;
Point() : m_row(-1), m_col(-1), m_symbol('@') {}
Point(int x, int y, char s) : m_row(x), m_col(y), m_symbol(s) {}
Point(int x, int y, char s, int d) : m_row(x), m_col(y), m_symbol(s), m_direction(d) {}
void to_string();
void set_new_point(int row, int col);
};
int get_score();
void determine_index_zero_pos(Point* (*board)[COL], Point** segments);
void move_tail_indicies(Point** segments);
void on_fruit_eaten(Point* (*board)[COL], Point** segments);
bool segment_overlap(Point** segments);
#endif // ELEMENT_HPP
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef GLOBALS_HPP
#define GLOBALS_HPP
#define ROW 10
#define COL 10
#define ROW 11
#define COL 11
#endif // GLOBALS_HPP
+3 -2
View File
@@ -1,10 +1,11 @@
#ifndef INPUT_HPP
#define INPUT_HPP
#include "element.hpp"
struct Input {
int m_current_key = 0;
enum Direction m_current_key = NONE;
int m_getch = 0;
void get_input(int &d, bool &r);
void get_input(enum Direction &dir, bool &r);
};
#endif // INPUT_HPP
+22 -6
View File
@@ -1,6 +1,11 @@
#include "element.hpp"
#include <cstdlib>
#include <ctime>
int _score = 0;
int get_score() {
return _score;
}
void Point::set_new_point(int row, int col) {
m_row = row;
@@ -16,16 +21,16 @@ void determine_index_zero_pos(Point* (*board)[COL], Point** segments) {
segments[1]->set_new_point(segments[0]->m_row, segments[0]->m_col);
}
if (segments[0]->m_direction == -1) {
if (segments[0]->m_dir == LEFT) {
if (new_point.m_col <= 0) new_point.m_col = COL - 1;
else new_point.m_col--;
} else if (segments[0]->m_direction == 1) {
} else if (segments[0]->m_dir == RIGHT) {
if (new_point.m_col >= COL - 1) new_point.m_col = 0;
else new_point.m_col++;
} else if (segments[0]->m_direction == -2) {
} else if (segments[0]->m_dir == UP) {
if (new_point.m_row <= 0) new_point.m_row = ROW - 1;
else new_point.m_row--;
} else if (segments[0]->m_direction == 2) {
} else if (segments[0]->m_dir == DOWN) {
if (new_point.m_row >= ROW - 1) new_point.m_row = 0;
else new_point.m_row++;
}
@@ -50,13 +55,13 @@ void move_tail_indicies(Point** segments) {
}
}
void _append_tail(Point *(*board)[10], Point** segments) {
void _append_tail(Point *(*board)[COL], Point** segments) {
for(int i = (ROW * COL) - 1; i >= 3; i--) {
segments[i] = segments[i - 1];
}
Point* old_tail = segments[4];
if(old_tail != nullptr) {
segments[3] = new Point { old_tail->m_row, old_tail->m_col, '@', -1 };
segments[3] = new Point { old_tail->m_row, old_tail->m_col, '@' };
} else {
segments[3] = new Point {};
}
@@ -71,5 +76,16 @@ void on_fruit_eaten(Point* (*board)[COL], Point** segments) {
} while (board[row][col] != nullptr);
segments[2]->set_new_point(row, col);
_append_tail(board, segments);
_score++;
}
}
bool segment_overlap(Point** segments) {
for(int i = (ROW * COL) - 1; i > 3; i--) {
if(segments[i] == nullptr) continue;
if(segments[i]->m_row == segments[0]->m_row && segments[i]->m_col == segments[0]->m_col) {
return true;
}
}
return false;
}
+5 -5
View File
@@ -1,7 +1,7 @@
#include "input.hpp"
#include <ncurses.h>
void Input::get_input(int &direction, bool &running) {
void Input::get_input(enum Direction &dir, bool &running) {
while(running) {
initscr();
cbreak();
@@ -11,16 +11,16 @@ void Input::get_input(int &direction, bool &running) {
endwin();
switch (m_getch) {
case 65: // Up arrow
if(m_current_key != 2) direction = m_current_key = -2;
if(m_current_key != DOWN) dir = m_current_key = UP;
break;
case 66: // Down arrow
if(m_current_key != -2) direction = m_current_key = 2;
if(m_current_key != UP) dir = m_current_key = DOWN;
break;
case 67: // Right arrow
if(m_current_key != -1) direction = m_current_key = 1;
if(m_current_key != LEFT) dir = m_current_key = RIGHT;
break;
case 68: // Left arrow
if(m_current_key != 1) direction = m_current_key = -1;
if(m_current_key != RIGHT) dir = m_current_key = LEFT;
break;
case 113:
running = false;
+14 -11
View File
@@ -11,26 +11,29 @@ int main () {
Input input;
bool running = true;
Point* segments[ROW * COL] = {};
segments[0] = new Point { 5, 5, '*', 1 }; // Head
segments[2] = new Point { (rand() % ROW), (rand() % COL), 'o', 0 }; // Fruit
Point* segments[(ROW * COL) + 3] = {}; // 2 for head, 1 for fruit, rest for tail
segments[0] = new Point { 5, 5, '*' }; // Head
segments[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit
std::thread input_thread(
&Input::get_input, &input,
std::ref(segments[0]->m_direction),
std::ref(segments[0]->m_dir),
std::ref(running));
while (running) {
on_fruit_eaten(window.m_board, segments);
determine_index_zero_pos(window.m_board, segments);
move_tail_indicies(segments);
// if(body.m_body.size() >= (ROW * COL)) {
// std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
// running = false;
// } else if(body.check_collision(head)) {
// std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
// running = false;
// }
for(int i = (ROW * COL) - 1; i > 3; i--) {
if(segments[i] == nullptr) break;
std::cout << "You win! Score: " << get_score() << std::endl;
running = false;
}
if(segment_overlap(segments)) {
std::cout << "You died! Score: " << get_score() << std::endl;
running = false;
}
window.clear_display();
window.update_display(segments);
window.draw_display(200);
-1
View File
@@ -9,7 +9,6 @@ void Window::clear_display() {
m_board[i][j] = nullptr;
}
}
}
void Window::draw_display(int milliseconds) {