From ab16f18137e5beec1401bb763eb8101de376dbaa Mon Sep 17 00:00:00 2001 From: DarthMooMancer Date: Sun, 2 Nov 2025 21:46:24 -0500 Subject: [PATCH] removing magic numbers --- include/element.hpp | 4 +--- include/globals.hpp | 4 ++++ src/element.cpp | 35 +++++++++++++---------------------- src/main.cpp | 9 ++++----- src/window.cpp | 10 +++------- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/include/element.hpp b/include/element.hpp index 0a791e1..7c5260a 100644 --- a/include/element.hpp +++ b/include/element.hpp @@ -4,7 +4,6 @@ #include "globals.hpp" enum Direction { - NONE = 0, UP = -2, DOWN = 2, LEFT = -1, @@ -12,8 +11,7 @@ enum Direction { }; struct Point { - int m_row; - int m_col; + int m_row, m_col; char m_symbol; enum Direction m_dir; Point() : m_row(0), m_col(0), m_symbol('@') {} diff --git a/include/globals.hpp b/include/globals.hpp index 829ae86..8ba42fc 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -2,4 +2,8 @@ #define GLOBALS_HPP #define ROW 11 #define COL 11 +#define MAX_LEN (ROW * COL) + 3 +#define HEAD 0 +#define PREVIOUS_HEAD 1 +#define FRUIT 2 #endif // GLOBALS_HPP diff --git a/src/element.cpp b/src/element.cpp index fe70db1..af9de7d 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -1,4 +1,5 @@ #include "element.hpp" +#include "globals.hpp" #include #include #include @@ -16,30 +17,21 @@ void Point::set_new_point(int row, int col) { } 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) { - if (buffer[0]->m_col <= 0) buffer[0]->m_col = COL - 1; - else buffer[0]->m_col--; - } - else if (buffer[0]->m_dir == RIGHT) { - 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++; + switch(head->m_dir) { + case LEFT: head->m_col = (head->m_col + COL - 1) % COL; break; + case RIGHT: head->m_col = (head->m_col + 1) % COL; break; + case UP: head->m_row = (head->m_row + ROW - 1) % ROW; break; + case DOWN: head->m_row = (head->m_row + 1) % ROW; break; } } void move_tail_indicies(Point** buffer) { if(_snake_size <= 3) return; - int new_row = buffer[1]->m_row; - int new_col = buffer[1]->m_col; + int new_row = buffer[PREVIOUS_HEAD]->m_row; + int new_col = buffer[PREVIOUS_HEAD]->m_col; Point* last_point = buffer[_snake_size - 1]; 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) { 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 { row = (rand() % ROW); col = (rand() % COL); - // } while (board[row][col] != nullptr); } 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*)); buffer[3] = new Point { ROW, COL, '@' }; _score++; @@ -67,7 +58,7 @@ void on_fruit_eaten(Point (*board)[COL], Point** buffer) { bool segment_overlap(Point** buffer) { for(int i = 0; i < _snake_size; i++) { 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; } } diff --git a/src/main.cpp b/src/main.cpp index e3f00be..b8e0e11 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,15 +8,14 @@ int main () { std::srand(std::time(0)); Window window; - const int MAX_LEN = (ROW * COL) + 3; bool running = true; Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail - buffer[0] = new Point { ROW / 2, COL / 2, '*' }; // Head - buffer[1] = new Point { buffer[0]->m_row, buffer[0]->m_col, '.' }; - buffer[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit + buffer[HEAD] = new Point { ROW / 2, COL / 2, '*' }; // Head + buffer[PREVIOUS_HEAD] = new Point { buffer[HEAD]->m_row, buffer[HEAD]->m_col, '.' }; + 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) { on_fruit_eaten(window.m_board, buffer); diff --git a/src/window.cpp b/src/window.cpp index c2d70e1..1c17b70 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -2,21 +2,17 @@ #include #include #include +#include #include void Window::clear_display() { - for(auto &row : m_board) { - for(auto &val : row) { - val.m_symbol = '.'; - } + for(auto [i, j] : std::ranges::views::cartesian_product(std::ranges::views::iota(0, ROW), std::ranges::views::iota(0, COL))) { + m_board[i][j].m_symbol = '.'; } } void Window::draw_display(int milliseconds) { std::cout << "\033[H" << std::flush; // Clear screen - // for(auto row : ROW) { - // - // } for(int i = 0; i < ROW; i++) { for(int j = 0; j < COL; j++) { if(m_board[i][j].m_symbol != '.') { std::cout << m_board[i][j].m_symbol << " "; }