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