From 2c38da4b798368095f6a7d2b9fdb77eee2e3a3a6 Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Mon, 3 Nov 2025 13:09:37 -0500 Subject: [PATCH] changes from m_row & m_col to x & y --- include/element.hpp | 8 ++++---- src/element.cpp | 36 ++++++++++++++++++------------------ src/main.cpp | 9 ++++----- src/window.cpp | 17 +++++++++-------- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/include/element.hpp b/include/element.hpp index 7c5260a..6067640 100644 --- a/include/element.hpp +++ b/include/element.hpp @@ -11,12 +11,12 @@ enum Direction { }; struct Point { - int m_row, m_col; + int x, y; char m_symbol; enum Direction m_dir; - Point() : m_row(0), m_col(0), m_symbol('@') {} - Point(int x, int y, char s) : m_row(x), m_col(y), m_symbol(s) {} - void set_new_point(int row, int col); + Point() : x(0), y(0), m_symbol('@') {} + Point(int x, int y, char s) : x(x), y(y), m_symbol(s) {} + void set_new_point(int x, int y); }; int get_score(); diff --git a/src/element.cpp b/src/element.cpp index af9de7d..5cd3e1a 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -1,8 +1,8 @@ #include "element.hpp" #include "globals.hpp" -#include #include #include +#include int _score = 0; int _snake_size = 3; @@ -11,44 +11,42 @@ int get_score() { return _score; } -void Point::set_new_point(int row, int col) { - m_row = row; - m_col = col; +void Point::set_new_point(int _x, int _y) { + y = _y; + x = _x; } void determine_index_zero_pos(Point** buffer) { auto& head = buffer[HEAD]; - buffer[PREVIOUS_HEAD]->set_new_point(head->m_row, head->m_col); + buffer[PREVIOUS_HEAD]->set_new_point(head->x, head->y); 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; + case LEFT: head->x = (head->x + COL - 1) % COL; break; + case RIGHT: head->x = (head->x + 1) % COL; break; + case UP: head->y = (head->y + ROW - 1) % ROW; break; + case DOWN: head->y = (head->y + 1) % ROW; break; } } void move_tail_indicies(Point** buffer) { if(_snake_size <= 3) return; - 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*)); + std::memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*)); buffer[3] = last_point; - if(buffer[3] != nullptr) buffer[3]->set_new_point(new_row, new_col); + if(buffer[3] != nullptr) buffer[3]->set_new_point(buffer[PREVIOUS_HEAD]->x, buffer[PREVIOUS_HEAD]->y); } void on_fruit_eaten(Point (*board)[COL], Point** buffer) { int row, col; - if(buffer[FRUIT]->m_row == buffer[HEAD]->m_row && buffer[FRUIT]->m_col == buffer[HEAD]->m_col) { + if(buffer[FRUIT]->y == buffer[HEAD]->y && buffer[FRUIT]->x == buffer[HEAD]->x) { do { row = (rand() % ROW); col = (rand() % COL); } while (board[row][col].m_symbol != '.'); buffer[FRUIT]->set_new_point(row, col); - memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*)); + std::memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*)); buffer[3] = new Point { ROW, COL, '@' }; _score++; _snake_size++; @@ -56,9 +54,11 @@ 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[HEAD]->m_row && buffer[i + 3]->m_col == buffer[HEAD]->m_col) { + for(int i : std::ranges::views::iota(3, _snake_size)) { + if(buffer[i] != nullptr && + buffer[i]->y == buffer[HEAD]->y && + buffer[i]->x == buffer[HEAD]->x) + { return true; } } diff --git a/src/main.cpp b/src/main.cpp index b8e0e11..1e33648 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,9 +11,9 @@ int main () { bool running = true; Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail - 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 + buffer[HEAD] = new Point { COL / 2, ROW / 2, '*' }; + buffer[PREVIOUS_HEAD] = new Point { buffer[HEAD]->x, buffer[HEAD]->y, '.' }; + buffer[FRUIT] = new Point { (rand() % COL), (rand() % ROW), 'o' }; std::thread input_thread([&] { get_input(buffer[HEAD]->m_dir, running ); } ); @@ -25,8 +25,7 @@ int main () { if(buffer[MAX_LEN - 1] != nullptr) { std::cout << "You filled the whole board and won! Score: " << get_score() << std::endl; running = false; - } - if(segment_overlap(buffer)) { + } if(segment_overlap(buffer)) { std::cout << "You collided with yourself and died! Score: " << get_score() << std::endl; running = false; } diff --git a/src/window.cpp b/src/window.cpp index 1c17b70..017fb8d 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,20 +1,21 @@ #include "window.hpp" -#include #include #include #include #include void Window::clear_display() { - 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 = '.'; + for(int i : std::ranges::views::iota(0, ROW)) { + for(int j : 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(int i = 0; i < ROW; i++) { - for(int j = 0; j < COL; j++) { + for(int i : std::ranges::views::iota(0, ROW)) { + for(int j : std::ranges::views::iota(0, COL)) { if(m_board[i][j].m_symbol != '.') { std::cout << m_board[i][j].m_symbol << " "; } else { std::cout << ". "; } } @@ -24,8 +25,8 @@ void Window::draw_display(int milliseconds) { } void Window::update_display(Point** buffer) { - for(int i = 0; i < ROW * COL; i++) { - if(buffer[i] == nullptr || i == 1 || buffer[i]->m_row > ROW - 1 || buffer[i]->m_col > COL - 1) continue; - m_board[buffer[i]->m_row][buffer[i]->m_col].m_symbol = buffer[i]->m_symbol; + for(int i : std::ranges::views::iota(0, ROW * COL)) { + if(buffer[i] == nullptr || i == 1 || buffer[i]->y > ROW - 1 || buffer[i]->x > COL - 1) continue; + m_board[buffer[i]->y][buffer[i]->x].m_symbol = buffer[i]->m_symbol; } }