cleaning up
This commit is contained in:
@@ -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 () {
|
||||
SnakeGame engine;
|
||||
Engine engine;
|
||||
engine.run();
|
||||
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