From 02ff154b816190955e377c9678990bf9e876b7fb Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Sat, 1 Nov 2025 18:06:21 -0400 Subject: [PATCH] optimizations --- .clangd | 2 -- include/element.hpp | 2 +- include/input.hpp | 5 +---- include/window.hpp | 2 +- src/element.cpp | 15 +++++---------- src/input.cpp | 27 +++++++++------------------ src/main.cpp | 14 ++++++-------- src/window.cpp | 18 +++++++++++------- 8 files changed, 34 insertions(+), 51 deletions(-) delete mode 100644 .clangd diff --git a/.clangd b/.clangd deleted file mode 100644 index 91e78fc..0000000 --- a/.clangd +++ /dev/null @@ -1,2 +0,0 @@ -CompileFlags: - Add: [-Iinclude] diff --git a/include/element.hpp b/include/element.hpp index 11e52bb..0a791e1 100644 --- a/include/element.hpp +++ b/include/element.hpp @@ -24,7 +24,7 @@ struct Point { int get_score(); void determine_index_zero_pos(Point** buffer); void move_tail_indicies(Point** buffer); -void on_fruit_eaten(Point* (*board)[COL], Point** buffer); +void on_fruit_eaten(Point (*board)[COL], Point** buffer); bool segment_overlap(Point** buffer); #endif // ELEMENT_HPP diff --git a/include/input.hpp b/include/input.hpp index 3b20576..567f778 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -2,9 +2,6 @@ #define INPUT_HPP #include "element.hpp" -struct Input { - int m_getch = 0; - void get_input(enum Direction &dir, bool &r); -}; +void get_input(enum Direction &dir, bool &r); #endif // INPUT_HPP diff --git a/include/window.hpp b/include/window.hpp index 353a479..e0693e3 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -5,7 +5,7 @@ #include "element.hpp" struct Window { - Point* m_board[ROW][COL] = {}; + Point m_board[ROW][COL] = {}; void clear_display(); void draw_display(int milliseconds); void update_display(Point** buffer); diff --git a/src/element.cpp b/src/element.cpp index 08d30af..fe70db1 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -16,11 +16,7 @@ void Point::set_new_point(int row, int col) { } void determine_index_zero_pos(Point** buffer) { - if(buffer[1] == nullptr) { - buffer[1] = new Point { buffer[0]->m_row, buffer[0]->m_col, '.' }; - } else { - buffer[1]->set_new_point(buffer[0]->m_row, buffer[0]->m_col); - } + buffer[1]->set_new_point(buffer[0]->m_row, buffer[0]->m_col); if (buffer[0]->m_dir == LEFT) { if (buffer[0]->m_col <= 0) buffer[0]->m_col = COL - 1; @@ -49,18 +45,17 @@ void move_tail_indicies(Point** buffer) { 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(new_row, new_col); } -void on_fruit_eaten(Point* (*board)[COL], 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) { do { row = (rand() % ROW); col = (rand() % COL); - } while (board[row][col] != nullptr); + // } while (board[row][col] != nullptr); + } while (board[row][col].m_symbol != '.'); buffer[2]->set_new_point(row, col); memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*)); buffer[3] = new Point { ROW, COL, '@' }; diff --git a/src/input.cpp b/src/input.cpp index 14a4362..98cdb94 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,7 +1,9 @@ #include "input.hpp" +#include #include -void Input::get_input(enum Direction &dir, bool &running) { +void get_input(enum Direction &dir, bool &running) { + uint16_t m_getch; while(running) { initscr(); cbreak(); @@ -9,22 +11,11 @@ void Input::get_input(enum Direction &dir, bool &running) { m_getch = getch(); endwin(); - switch (m_getch) { - case 65: // Up arrow - if(dir != DOWN) dir = UP; - break; - case 66: // Down arrow - if(dir != UP) dir = DOWN; - break; - case 67: // Right arrow - if(dir != LEFT) dir = RIGHT; - break; - case 68: // Left arrow - if(dir != RIGHT) dir = LEFT; - break; - case 113: - running = false; - break; - } + + if(m_getch == 65) if(dir != DOWN) dir = UP; + if(m_getch == 66) if(dir != UP) dir = DOWN; + if(m_getch == 67) if(dir != LEFT) dir = RIGHT; + if(m_getch == 68) if(dir != RIGHT) dir = LEFT; + if(m_getch == 113) running = false; } } diff --git a/src/main.cpp b/src/main.cpp index e623a19..e3f00be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,23 +8,22 @@ int main () { std::srand(std::time(0)); Window window; - Input input; 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 - std::thread input_thread([&] { input.get_input(buffer[0]->m_dir, running); } ); + std::thread input_thread([&] { get_input(buffer[0]->m_dir, running ); } ); while (running) { on_fruit_eaten(window.m_board, buffer); determine_index_zero_pos(buffer); move_tail_indicies(buffer); - for(int i = MAX_LEN - 1; i > 3; i--) { - if(buffer[i] == nullptr) break; + if(buffer[MAX_LEN - 1] != nullptr) { std::cout << "You filled the whole board and won! Score: " << get_score() << std::endl; running = false; } @@ -32,14 +31,13 @@ int main () { std::cout << "You collided with yourself and died! Score: " << get_score() << std::endl; running = false; } + window.clear_display(); window.update_display(buffer); window.draw_display(200); } input_thread.join(); - for(int i = 0; i < MAX_LEN - 1; i++) { - delete buffer[i]; - } + for(auto& p : buffer) delete p; return 0; } diff --git a/src/window.cpp b/src/window.cpp index 64ac54d..c2d70e1 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,22 +1,26 @@ #include "window.hpp" +#include #include #include #include void Window::clear_display() { - for(int i = 0; i < ROW; i++) { - for(int j = 0; j < COL; j++) { - m_board[i][j] = nullptr; + for(auto &row : m_board) { + for(auto &val : row) { + val.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 < ROW; j++) { - if(m_board[i][j] == nullptr) { std::cout << ". "; } - if(m_board[i][j] != nullptr) { std::cout << m_board[i][j]->m_symbol << " "; } + for(int j = 0; j < COL; j++) { + if(m_board[i][j].m_symbol != '.') { std::cout << m_board[i][j].m_symbol << " "; } + else { std::cout << ". "; } } std::cout << "\r\n"; } @@ -26,6 +30,6 @@ 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] = buffer[i]; + m_board[buffer[i]->m_row][buffer[i]->m_col].m_symbol = buffer[i]->m_symbol; } }