This commit is contained in:
DarthMooMancer
2025-11-05 09:32:05 -05:00
parent c5bb6e34b3
commit 435f7d1a49
5 changed files with 45 additions and 55 deletions
+12 -14
View File
@@ -4,7 +4,7 @@
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "globals.hpp" #include <globals.hpp>
class Window; class Window;
@@ -12,12 +12,12 @@ enum class Direction {
UP, DOWN, LEFT, RIGHT UP, DOWN, LEFT, RIGHT
}; };
struct vec { struct vec2 {
int x, y; int x, y;
vec() : x(0), y(0) {} vec2() : x(0), y(0) {}
vec(int x, int y) : x(x), y(y) {} vec2(int x, int y) : x(x), y(y) {}
void set(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 { class SnakeGame {
@@ -28,20 +28,18 @@ public:
private: private:
std::unique_ptr<Window> win; std::unique_ptr<Window> win;
std::vector<vec> buf; std::vector<vec2> buf;
vec head { COL / 2, ROW / 2 }; vec2 head { COL / 2, ROW / 2 };
vec fruit { rand() % COL, rand() % ROW }; vec2 fruit { rand() % COL, rand() % ROW };
vec prev_head { head.x, head.y }; vec2 prev_head { head.x, head.y };
struct { struct {
bool running = true; Direction dir = Direction::RIGHT;
Direction m_dir = Direction::RIGHT;
uint8_t m_score = 0; uint8_t m_score = 0;
} state; } state;
void process_input(); bool process_input();
void determine_index_zero_pos(); void determine_index_zero_pos();
void move_tail_indicies(); void move_tail_indicies();
void add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> board); void add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> view);
bool segment_overlap();
bool exit_summary(); bool exit_summary();
}; };
+4 -4
View File
@@ -1,14 +1,14 @@
#ifndef WINDOW_HPP #ifndef WINDOW_HPP
#define WINDOW_HPP #define WINDOW_HPP
#include "globals.hpp" #include <globals.hpp>
#include "element.hpp" #include <element.hpp>
#include <array> #include <array>
struct Window { struct Window {
std::array<std::array<char, COL>, ROW> board; std::array<std::array<char, COL>, ROW> view;
void draw_display(); void draw_display();
void update_display(vec &head, vec &fruit, std::vector<vec> &buf); void update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf);
}; };
#endif // WINDOW_HPP #endif // WINDOW_HPP
+20 -28
View File
@@ -1,8 +1,6 @@
#include "element.hpp" #include <element.hpp>
#include "globals.hpp"
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <memory>
#include <ncurses.h> #include <ncurses.h>
#include <ranges> #include <ranges>
#include <iostream> #include <iostream>
@@ -25,9 +23,8 @@ SnakeGame::~SnakeGame() {
} }
void SnakeGame::run() { void SnakeGame::run() {
while (exit_summary()) { while (exit_summary() && process_input()) {
process_input(); add_tail_and_respawn_fruit(win->view);
add_tail_and_respawn_fruit(win->board);
determine_index_zero_pos(); determine_index_zero_pos();
move_tail_indicies(); move_tail_indicies();
@@ -37,27 +34,30 @@ void SnakeGame::run() {
} }
} }
void SnakeGame::process_input() { bool SnakeGame::process_input() {
int ch; int ch;
while((ch = getch()) != ERR) { while((ch = getch()) != ERR) {
switch(ch) { switch(ch) {
case 'w': if(state.m_dir != Direction::DOWN) state.m_dir = Direction::UP; break; case 'w': if(state.dir != Direction::DOWN) state.dir = Direction::UP; break;
case 's': if(state.m_dir != Direction::UP) state.m_dir = Direction::DOWN; break; case 's': if(state.dir != Direction::UP) state.dir = Direction::DOWN; break;
case 'd': if(state.m_dir != Direction::LEFT) state.m_dir = Direction::RIGHT; break; case 'd': if(state.dir != Direction::LEFT) state.dir = Direction::RIGHT; break;
case 'a': if(state.m_dir != Direction::RIGHT) state.m_dir = Direction::LEFT; break; case 'a': if(state.dir != Direction::RIGHT) state.dir = Direction::LEFT; break;
case 'q': state.running = false; break; case 'q': return false;
} }
} }
return true;
} }
bool SnakeGame::exit_summary() { bool SnakeGame::exit_summary() {
if(!state.running) return false;
if(buf.size() >= MAX_LEN - 1) { if(buf.size() >= MAX_LEN - 1) {
std::cout << "You filled the whole board and won! Score: " << state.m_score << "\n"; std::cout << "You filled the whole board and won! Score: " << state.m_score << "\n";
return false; return false;
} if(segment_overlap()) { }
std::cout << "You collided with yourself and died! Score: " << state.m_score << "\n"; for(int i : std::ranges::views::iota(0, (int)buf.size())) {
return false; if(buf[i] == head) {
std::cout << "You collided with yourself and died! Score: " << state.m_score << "\n";
return false;
}
} }
return true; return true;
} }
@@ -65,7 +65,7 @@ bool SnakeGame::exit_summary() {
void SnakeGame::determine_index_zero_pos() { void SnakeGame::determine_index_zero_pos() {
prev_head.set(head.x, head.y); 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::LEFT: head.x = (head.x + COL - 1) % COL; break;
case Direction::RIGHT: head.x = (head.x + 1) % COL; break; case Direction::RIGHT: head.x = (head.x + 1) % COL; break;
case Direction::UP: head.y = (head.y + ROW - 1) % ROW; 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); buf.insert(buf.begin(), prev_head);
} }
void SnakeGame::add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> board) { void SnakeGame::add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> view) {
if(fruit != head) return; if(fruit != head) return;
int row, col; int row, col;
do { do {
row = (rand() % ROW); row = (rand() % ROW);
col = (rand() % COL); col = (rand() % COL);
} while (board[row][col] != '.'); } while (view[row][col] != '.');
fruit.set(row, col); fruit.set(row, col);
buf.insert(buf.begin(), vec { ROW, COL }); buf.insert(buf.begin(), vec2 { ROW, COL });
state.m_score++; 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;
}
+1 -1
View File
@@ -1,4 +1,4 @@
#include "element.hpp" #include <element.hpp>
int main () { int main () {
SnakeGame engine; SnakeGame engine;
+8 -8
View File
@@ -1,5 +1,5 @@
#include "window.hpp" #include <window.hpp>
#include "element.hpp" #include <element.hpp>
#include <ncurses.h> #include <ncurses.h>
#include <ranges> #include <ranges>
#include <vector> #include <vector>
@@ -7,21 +7,21 @@
void Window::draw_display() { void Window::draw_display() {
for(int i : std::ranges::views::iota(0, ROW)) { for(int i : std::ranges::views::iota(0, ROW)) {
for(int j : std::ranges::views::iota(0, COL)) { for(int j : std::ranges::views::iota(0, COL)) {
mvaddch(i, j * 2, board[i][j]); mvaddch(i, j * 2, view[i][j]);
} }
} }
refresh(); refresh();
} }
void Window::update_display(vec &head, vec &fruit, std::vector<vec> &buf) { void Window::update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf) {
for(auto& row : board) row.fill('.'); for(auto& row : view) row.fill('.');
board[head.y][head.x] = '*'; view[head.y][head.x] = '*';
board[fruit.y][fruit.x] = 'o'; view[fruit.y][fruit.x] = 'o';
for(int i : std::ranges::views::iota(0, (int)buf.size())) { for(int i : std::ranges::views::iota(0, (int)buf.size())) {
const auto &segments = buf.at(i); const auto &segments = buf.at(i);
if(segments.x < COL && segments.y < ROW) { if(segments.x < COL && segments.y < ROW) {
board[segments.y][segments.x] = '~'; view[segments.y][segments.x] = '~';
} }
} }
} }