removing magic numbers

This commit is contained in:
DarthMooMancer
2025-11-02 21:46:24 -05:00
parent 02ff154b81
commit ab16f18137
5 changed files with 25 additions and 37 deletions
+1 -3
View File
@@ -4,7 +4,6 @@
#include "globals.hpp" #include "globals.hpp"
enum Direction { enum Direction {
NONE = 0,
UP = -2, UP = -2,
DOWN = 2, DOWN = 2,
LEFT = -1, LEFT = -1,
@@ -12,8 +11,7 @@ enum Direction {
}; };
struct Point { struct Point {
int m_row; int m_row, m_col;
int m_col;
char m_symbol; char m_symbol;
enum Direction m_dir; enum Direction m_dir;
Point() : m_row(0), m_col(0), m_symbol('@') {} Point() : m_row(0), m_col(0), m_symbol('@') {}
+4
View File
@@ -2,4 +2,8 @@
#define GLOBALS_HPP #define GLOBALS_HPP
#define ROW 11 #define ROW 11
#define COL 11 #define COL 11
#define MAX_LEN (ROW * COL) + 3
#define HEAD 0
#define PREVIOUS_HEAD 1
#define FRUIT 2
#endif // GLOBALS_HPP #endif // GLOBALS_HPP
+13 -22
View File
@@ -1,4 +1,5 @@
#include "element.hpp" #include "element.hpp"
#include "globals.hpp"
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
@@ -16,30 +17,21 @@ void Point::set_new_point(int row, int col) {
} }
void determine_index_zero_pos(Point** buffer) { void determine_index_zero_pos(Point** buffer) {
buffer[1]->set_new_point(buffer[0]->m_row, buffer[0]->m_col); auto& head = buffer[HEAD];
buffer[PREVIOUS_HEAD]->set_new_point(head->m_row, head->m_col);
if (buffer[0]->m_dir == LEFT) { switch(head->m_dir) {
if (buffer[0]->m_col <= 0) buffer[0]->m_col = COL - 1; case LEFT: head->m_col = (head->m_col + COL - 1) % COL; break;
else buffer[0]->m_col--; case RIGHT: head->m_col = (head->m_col + 1) % COL; break;
} case UP: head->m_row = (head->m_row + ROW - 1) % ROW; break;
else if (buffer[0]->m_dir == RIGHT) { case DOWN: head->m_row = (head->m_row + 1) % ROW; break;
if (buffer[0]->m_col >= COL - 1) buffer[0]->m_col = 0;
else buffer[0]->m_col++;
}
else if (buffer[0]->m_dir == UP) {
if (buffer[0]->m_row <= 0) buffer[0]->m_row = ROW - 1;
else buffer[0]->m_row--;
}
else if (buffer[0]->m_dir == DOWN) {
if (buffer[0]->m_row >= ROW - 1) buffer[0]->m_row = 0;
else buffer[0]->m_row++;
} }
} }
void move_tail_indicies(Point** buffer) { void move_tail_indicies(Point** buffer) {
if(_snake_size <= 3) return; if(_snake_size <= 3) return;
int new_row = buffer[1]->m_row; int new_row = buffer[PREVIOUS_HEAD]->m_row;
int new_col = buffer[1]->m_col; int new_col = buffer[PREVIOUS_HEAD]->m_col;
Point* last_point = buffer[_snake_size - 1]; Point* last_point = buffer[_snake_size - 1];
memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*)); memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*));
@@ -50,13 +42,12 @@ void move_tail_indicies(Point** buffer) {
void on_fruit_eaten(Point (*board)[COL], Point** buffer) { void on_fruit_eaten(Point (*board)[COL], Point** buffer) {
int row, col; int row, col;
if(buffer[2]->m_row == buffer[0]->m_row && buffer[2]->m_col == buffer[0]->m_col) { if(buffer[FRUIT]->m_row == buffer[HEAD]->m_row && buffer[FRUIT]->m_col == buffer[HEAD]->m_col) {
do { do {
row = (rand() % ROW); row = (rand() % ROW);
col = (rand() % COL); col = (rand() % COL);
// } while (board[row][col] != nullptr);
} while (board[row][col].m_symbol != '.'); } while (board[row][col].m_symbol != '.');
buffer[2]->set_new_point(row, col); buffer[FRUIT]->set_new_point(row, col);
memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*)); memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*));
buffer[3] = new Point { ROW, COL, '@' }; buffer[3] = new Point { ROW, COL, '@' };
_score++; _score++;
@@ -67,7 +58,7 @@ void on_fruit_eaten(Point (*board)[COL], Point** buffer) {
bool segment_overlap(Point** buffer) { bool segment_overlap(Point** buffer) {
for(int i = 0; i < _snake_size; i++) { for(int i = 0; i < _snake_size; i++) {
if(buffer[i + 3] == nullptr) continue; if(buffer[i + 3] == nullptr) continue;
if(buffer[i + 3]->m_row == buffer[0]->m_row && buffer[i + 3]->m_col == buffer[0]->m_col) { if(buffer[i + 3]->m_row == buffer[HEAD]->m_row && buffer[i + 3]->m_col == buffer[HEAD]->m_col) {
return true; return true;
} }
} }
+4 -5
View File
@@ -8,15 +8,14 @@
int main () { int main () {
std::srand(std::time(0)); std::srand(std::time(0));
Window window; Window window;
const int MAX_LEN = (ROW * COL) + 3;
bool running = true; bool running = true;
Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail
buffer[0] = new Point { ROW / 2, COL / 2, '*' }; // Head buffer[HEAD] = new Point { ROW / 2, COL / 2, '*' }; // Head
buffer[1] = new Point { buffer[0]->m_row, buffer[0]->m_col, '.' }; buffer[PREVIOUS_HEAD] = new Point { buffer[HEAD]->m_row, buffer[HEAD]->m_col, '.' };
buffer[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit buffer[FRUIT] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit
std::thread input_thread([&] { get_input(buffer[0]->m_dir, running ); } ); std::thread input_thread([&] { get_input(buffer[HEAD]->m_dir, running ); } );
while (running) { while (running) {
on_fruit_eaten(window.m_board, buffer); on_fruit_eaten(window.m_board, buffer);
+3 -7
View File
@@ -2,21 +2,17 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <ranges>
#include <thread> #include <thread>
void Window::clear_display() { void Window::clear_display() {
for(auto &row : m_board) { for(auto [i, j] : std::ranges::views::cartesian_product(std::ranges::views::iota(0, ROW), std::ranges::views::iota(0, COL))) {
for(auto &val : row) { m_board[i][j].m_symbol = '.';
val.m_symbol = '.';
}
} }
} }
void Window::draw_display(int milliseconds) { void Window::draw_display(int milliseconds) {
std::cout << "\033[H" << std::flush; // Clear screen std::cout << "\033[H" << std::flush; // Clear screen
// for(auto row : ROW) {
//
// }
for(int i = 0; i < ROW; i++) { for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) { for(int j = 0; j < COL; j++) {
if(m_board[i][j].m_symbol != '.') { std::cout << m_board[i][j].m_symbol << " "; } if(m_board[i][j].m_symbol != '.') { std::cout << m_board[i][j].m_symbol << " "; }