cleaning up
This commit is contained in:
+1
-2
@@ -6,8 +6,7 @@ project(snake)
|
|||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/element.cpp
|
src/engine.cpp
|
||||||
src/window.cpp
|
|
||||||
)
|
)
|
||||||
add_executable(snake ${SOURCES})
|
add_executable(snake ${SOURCES})
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
#ifndef ELEMENT_HPP
|
#ifndef ELEMENT_HPP
|
||||||
#define ELEMENT_HPP
|
#define ELEMENT_HPP
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <memory>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <globals.hpp>
|
|
||||||
|
|
||||||
class Window;
|
constexpr int ROW = 11;
|
||||||
|
constexpr int COL = 11;
|
||||||
|
|
||||||
enum class Direction {
|
enum class Direction {
|
||||||
UP, DOWN, LEFT, RIGHT
|
UP, DOWN, LEFT, RIGHT
|
||||||
@@ -14,32 +12,35 @@ enum class Direction {
|
|||||||
|
|
||||||
struct vec2 {
|
struct vec2 {
|
||||||
int x, y;
|
int x, y;
|
||||||
vec2() : x(0), y(0) {}
|
|
||||||
vec2(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 vec2&) const = default;
|
auto operator <=> (const vec2&) const = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SnakeGame {
|
struct Window {
|
||||||
|
std::array<std::array<char, COL>, ROW> view;
|
||||||
|
void draw_display();
|
||||||
|
void update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Engine {
|
||||||
public:
|
public:
|
||||||
SnakeGame();
|
Engine();
|
||||||
~SnakeGame();
|
~Engine();
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Window> win;
|
Window win;
|
||||||
std::vector<vec2> buf;
|
std::vector<vec2> buf;
|
||||||
vec2 head { COL / 2, ROW / 2 };
|
vec2 head { COL / 2, ROW / 2 };
|
||||||
vec2 fruit { rand() % COL, rand() % ROW };
|
vec2 fruit { rand() % COL, rand() % ROW };
|
||||||
vec2 prev_head { head.x, head.y };
|
vec2 prev_head { head.x, head.y };
|
||||||
struct {
|
Direction dir = Direction::RIGHT;
|
||||||
Direction dir = Direction::RIGHT;
|
unsigned int m_score = 0;
|
||||||
uint8_t m_score = 0;
|
|
||||||
} state;
|
|
||||||
bool 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> view);
|
void add_tail_and_respawn_fruit();
|
||||||
bool exit_summary();
|
bool exit_summary();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
#ifndef GLOBALS_HPP
|
|
||||||
#define GLOBALS_HPP
|
|
||||||
constexpr int ROW = 11;
|
|
||||||
constexpr int COL = 11;
|
|
||||||
constexpr int MAX_LEN = (ROW * COL) + 3;
|
|
||||||
constexpr int HEAD = 0;
|
|
||||||
constexpr int PREVIOUS_HEAD = 1;
|
|
||||||
constexpr int FRUIT = 2;
|
|
||||||
#endif // GLOBALS_HPP
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#ifndef WINDOW_HPP
|
|
||||||
#define WINDOW_HPP
|
|
||||||
|
|
||||||
#include <globals.hpp>
|
|
||||||
#include <element.hpp>
|
|
||||||
#include <array>
|
|
||||||
|
|
||||||
struct Window {
|
|
||||||
std::array<std::array<char, COL>, ROW> view;
|
|
||||||
void draw_display();
|
|
||||||
void update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // WINDOW_HPP
|
|
||||||
@@ -1,92 +0,0 @@
|
|||||||
#include <element.hpp>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <ctime>
|
|
||||||
#include <ncurses.h>
|
|
||||||
#include <ranges>
|
|
||||||
#include <iostream>
|
|
||||||
#include <window.hpp>
|
|
||||||
#include <thread>
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
SnakeGame::SnakeGame() {
|
|
||||||
std::srand(std::time(0));
|
|
||||||
initscr();
|
|
||||||
cbreak();
|
|
||||||
noecho();
|
|
||||||
curs_set(0);
|
|
||||||
nodelay(stdscr, true);
|
|
||||||
win = std::make_unique<Window>();
|
|
||||||
}
|
|
||||||
|
|
||||||
SnakeGame::~SnakeGame() {
|
|
||||||
endwin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SnakeGame::run() {
|
|
||||||
while (exit_summary() && process_input()) {
|
|
||||||
add_tail_and_respawn_fruit(win->view);
|
|
||||||
determine_index_zero_pos();
|
|
||||||
move_tail_indicies();
|
|
||||||
|
|
||||||
win->update_display(head, fruit, buf);
|
|
||||||
win->draw_display();
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SnakeGame::process_input() {
|
|
||||||
int ch;
|
|
||||||
while((ch = getch()) != ERR) {
|
|
||||||
switch(ch) {
|
|
||||||
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(buf.size() >= MAX_LEN - 1) {
|
|
||||||
std::cout << "You filled the whole board and won! 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SnakeGame::determine_index_zero_pos() {
|
|
||||||
prev_head.set(head.x, head.y);
|
|
||||||
|
|
||||||
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;
|
|
||||||
case Direction::DOWN: head.y = (head.y + 1) % ROW; break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SnakeGame::move_tail_indicies() {
|
|
||||||
if(buf.empty()) return;
|
|
||||||
buf.pop_back();
|
|
||||||
buf.insert(buf.begin(), prev_head);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SnakeGame::add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> view) {
|
|
||||||
if(fruit != head) return;
|
|
||||||
int row, col;
|
|
||||||
do {
|
|
||||||
row = (rand() % ROW);
|
|
||||||
col = (rand() % COL);
|
|
||||||
} while (view[row][col] != '.');
|
|
||||||
fruit.set(row, col);
|
|
||||||
buf.insert(buf.begin(), vec2 { ROW, COL });
|
|
||||||
state.m_score++;
|
|
||||||
}
|
|
||||||
+106
@@ -0,0 +1,106 @@
|
|||||||
|
#include <engine.hpp>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ctime>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <ranges>
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
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, view[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf) {
|
||||||
|
for(auto& row : view) row.fill('.');
|
||||||
|
view[head.y][head.x] = '*';
|
||||||
|
view[fruit.y][fruit.x] = 'o';
|
||||||
|
|
||||||
|
for(const auto& segment : buf) {
|
||||||
|
if(segment.x < COL && segment.y < ROW) {
|
||||||
|
view[segment.y][segment.x] = '~';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Engine::Engine() {
|
||||||
|
std::srand(std::time(0));
|
||||||
|
initscr();
|
||||||
|
cbreak();
|
||||||
|
noecho();
|
||||||
|
curs_set(0);
|
||||||
|
nodelay(stdscr, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Engine::~Engine() {
|
||||||
|
endwin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::run() {
|
||||||
|
while (exit_summary() && process_input()) {
|
||||||
|
add_tail_and_respawn_fruit();
|
||||||
|
determine_index_zero_pos();
|
||||||
|
move_tail_indicies();
|
||||||
|
|
||||||
|
win.update_display(head, fruit, buf);
|
||||||
|
win.draw_display();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Engine::process_input() {
|
||||||
|
int ch;
|
||||||
|
while((ch = getch()) != ERR) {
|
||||||
|
if(ch == 'q') { return false; }
|
||||||
|
else if(ch == 'w') { if(dir != Direction::DOWN) dir = Direction::UP; }
|
||||||
|
else if(ch == 's') { if(dir != Direction::UP) dir = Direction::DOWN; }
|
||||||
|
else if(ch == 'd') { if(dir != Direction::LEFT) dir = Direction::RIGHT; }
|
||||||
|
else if(ch == 'a') { if(dir != Direction::RIGHT) dir = Direction::LEFT; }
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Engine::exit_summary() {
|
||||||
|
if(buf.size() >= ROW * COL) {
|
||||||
|
std::cout << "You filled the whole board and won! Score: " << m_score << "\n";
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(std::any_of(buf.begin(), buf.end(), [&](const vec2& segment) { return segment == head; })) {
|
||||||
|
std::cout << "You collided with yourself and died! Score: " << m_score << "\n";
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::determine_index_zero_pos() {
|
||||||
|
prev_head.set(head.x, head.y);
|
||||||
|
|
||||||
|
if(dir == Direction::LEFT) { head.x = (head.x + COL - 1) % COL; }
|
||||||
|
else if(dir == Direction::RIGHT) { head.x = (head.x + 1) % COL; }
|
||||||
|
else if(dir == Direction::UP) { head.y = (head.y + ROW - 1) % ROW; }
|
||||||
|
else if(dir == Direction::DOWN) { head.y = (head.y + 1) % ROW; }
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::move_tail_indicies() {
|
||||||
|
if(buf.empty()) return;
|
||||||
|
buf.pop_back();
|
||||||
|
buf.insert(buf.begin(), prev_head);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::add_tail_and_respawn_fruit() {
|
||||||
|
if(fruit != head) return;
|
||||||
|
vec2 new_pos { 0, 0 };
|
||||||
|
do { new_pos.set((rand() % ROW), (rand() % COL)); }
|
||||||
|
while (new_pos == head || std::any_of(buf.begin(), buf.end(), [&](const vec2& seg) { return seg == new_pos; }));
|
||||||
|
fruit = new_pos;
|
||||||
|
buf.insert(buf.begin(), vec2 { ROW, COL });
|
||||||
|
m_score++;
|
||||||
|
}
|
||||||
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
#include <element.hpp>
|
#include <engine.hpp>
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
SnakeGame engine;
|
Engine engine;
|
||||||
engine.run();
|
engine.run();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
#include <window.hpp>
|
|
||||||
#include <element.hpp>
|
|
||||||
#include <ncurses.h>
|
|
||||||
#include <ranges>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
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, view[i][j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
refresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &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) {
|
|
||||||
view[segments.y][segments.x] = '~';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user