From 435f7d1a499d6e4dbe9a1c8b163ed9fe057da3fb Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Wed, 5 Nov 2025 09:32:05 -0500 Subject: [PATCH] wip --- include/element.hpp | 26 ++++++++++++------------ include/window.hpp | 8 ++++---- src/element.cpp | 48 +++++++++++++++++++-------------------------- src/main.cpp | 2 +- src/window.cpp | 16 +++++++-------- 5 files changed, 45 insertions(+), 55 deletions(-) diff --git a/include/element.hpp b/include/element.hpp index 915214c..8b21630 100644 --- a/include/element.hpp +++ b/include/element.hpp @@ -4,7 +4,7 @@ #include #include #include -#include "globals.hpp" +#include class Window; @@ -12,12 +12,12 @@ enum class Direction { UP, DOWN, LEFT, RIGHT }; -struct vec { +struct vec2 { int x, y; - vec() : x(0), y(0) {} - vec(int x, int y) : x(x), y(y) {} + vec2() : x(0), y(0) {} + vec2(int x, int y) : x(x), y(y) {} void set(int _x, int _y) { x = _x; y = _y; } - auto operator <=> (const vec&) const = default; + auto operator <=> (const vec2&) const = default; }; class SnakeGame { @@ -28,20 +28,18 @@ public: private: std::unique_ptr win; - std::vector buf; - vec head { COL / 2, ROW / 2 }; - vec fruit { rand() % COL, rand() % ROW }; - vec prev_head { head.x, head.y }; + std::vector buf; + vec2 head { COL / 2, ROW / 2 }; + vec2 fruit { rand() % COL, rand() % ROW }; + vec2 prev_head { head.x, head.y }; struct { - bool running = true; - Direction m_dir = Direction::RIGHT; + Direction dir = Direction::RIGHT; uint8_t m_score = 0; } state; - void process_input(); + bool process_input(); void determine_index_zero_pos(); void move_tail_indicies(); - void add_tail_and_respawn_fruit(std::array, ROW> board); - bool segment_overlap(); + void add_tail_and_respawn_fruit(std::array, ROW> view); bool exit_summary(); }; diff --git a/include/window.hpp b/include/window.hpp index d91a7a5..427fd92 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -1,14 +1,14 @@ #ifndef WINDOW_HPP #define WINDOW_HPP -#include "globals.hpp" -#include "element.hpp" +#include +#include #include struct Window { - std::array, ROW> board; + std::array, ROW> view; void draw_display(); - void update_display(vec &head, vec &fruit, std::vector &buf); + void update_display(vec2 &head, vec2 &fruit, std::vector &buf); }; #endif // WINDOW_HPP diff --git a/src/element.cpp b/src/element.cpp index 626bfa9..64a6c57 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -1,8 +1,6 @@ -#include "element.hpp" -#include "globals.hpp" +#include #include #include -#include #include #include #include @@ -25,9 +23,8 @@ SnakeGame::~SnakeGame() { } void SnakeGame::run() { - while (exit_summary()) { - process_input(); - add_tail_and_respawn_fruit(win->board); + while (exit_summary() && process_input()) { + add_tail_and_respawn_fruit(win->view); determine_index_zero_pos(); move_tail_indicies(); @@ -37,27 +34,30 @@ void SnakeGame::run() { } } -void SnakeGame::process_input() { +bool SnakeGame::process_input() { int ch; while((ch = getch()) != ERR) { switch(ch) { - case 'w': if(state.m_dir != Direction::DOWN) state.m_dir = Direction::UP; break; - case 's': if(state.m_dir != Direction::UP) state.m_dir = Direction::DOWN; break; - case 'd': if(state.m_dir != Direction::LEFT) state.m_dir = Direction::RIGHT; break; - case 'a': if(state.m_dir != Direction::RIGHT) state.m_dir = Direction::LEFT; break; - case 'q': state.running = false; break; + case 'w': if(state.dir != Direction::DOWN) state.dir = Direction::UP; break; + case 's': if(state.dir != Direction::UP) state.dir = Direction::DOWN; break; + case 'd': if(state.dir != Direction::LEFT) state.dir = Direction::RIGHT; break; + case 'a': if(state.dir != Direction::RIGHT) state.dir = Direction::LEFT; break; + case 'q': return false; } } + return true; } bool SnakeGame::exit_summary() { - if(!state.running) return false; if(buf.size() >= MAX_LEN - 1) { std::cout << "You filled the whole board and won! Score: " << state.m_score << "\n"; return false; - } if(segment_overlap()) { - std::cout << "You collided with yourself and died! Score: " << state.m_score << "\n"; - return false; + } + for(int i : std::ranges::views::iota(0, (int)buf.size())) { + if(buf[i] == head) { + std::cout << "You collided with yourself and died! Score: " << state.m_score << "\n"; + return false; + } } return true; } @@ -65,7 +65,7 @@ bool SnakeGame::exit_summary() { void SnakeGame::determine_index_zero_pos() { prev_head.set(head.x, head.y); - switch(state.m_dir) { + switch(state.dir) { case Direction::LEFT: head.x = (head.x + COL - 1) % COL; break; case Direction::RIGHT: head.x = (head.x + 1) % COL; break; case Direction::UP: head.y = (head.y + ROW - 1) % ROW; break; @@ -79,22 +79,14 @@ void SnakeGame::move_tail_indicies() { buf.insert(buf.begin(), prev_head); } -void SnakeGame::add_tail_and_respawn_fruit(std::array, ROW> board) { +void SnakeGame::add_tail_and_respawn_fruit(std::array, ROW> view) { if(fruit != head) return; int row, col; do { row = (rand() % ROW); col = (rand() % COL); - } while (board[row][col] != '.'); + } while (view[row][col] != '.'); fruit.set(row, col); - buf.insert(buf.begin(), vec { ROW, COL }); + buf.insert(buf.begin(), vec2 { ROW, COL }); state.m_score++; } - -bool SnakeGame::segment_overlap() { - if(buf.size() < 1) return false; - for(int i : std::ranges::views::iota(0, (int)buf.size())) { - if(buf[i] == head) return true; - } - return false; -} diff --git a/src/main.cpp b/src/main.cpp index 2385f80..e5dda5d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include "element.hpp" +#include int main () { SnakeGame engine; diff --git a/src/window.cpp b/src/window.cpp index 9ef5f2c..8a7fafd 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,5 +1,5 @@ -#include "window.hpp" -#include "element.hpp" +#include +#include #include #include #include @@ -7,21 +7,21 @@ void Window::draw_display() { for(int i : std::ranges::views::iota(0, ROW)) { for(int j : std::ranges::views::iota(0, COL)) { - mvaddch(i, j * 2, board[i][j]); + mvaddch(i, j * 2, view[i][j]); } } refresh(); } -void Window::update_display(vec &head, vec &fruit, std::vector &buf) { - for(auto& row : board) row.fill('.'); - board[head.y][head.x] = '*'; - board[fruit.y][fruit.x] = 'o'; +void Window::update_display(vec2 &head, vec2 &fruit, std::vector &buf) { + for(auto& row : view) row.fill('.'); + view[head.y][head.x] = '*'; + view[fruit.y][fruit.x] = 'o'; for(int i : std::ranges::views::iota(0, (int)buf.size())) { const auto &segments = buf.at(i); if(segments.x < COL && segments.y < ROW) { - board[segments.y][segments.x] = '~'; + view[segments.y][segments.x] = '~'; } } }