49 lines
960 B
C++
49 lines
960 B
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <deque>
|
|
#include <array>
|
|
#include <ncurses.h>
|
|
|
|
constexpr int ROW = 15;
|
|
constexpr int COL = 21;
|
|
|
|
enum class Direction {
|
|
UP, DOWN, LEFT, RIGHT
|
|
};
|
|
|
|
struct vec2 {
|
|
int x, y;
|
|
vec2(int x, int y) : x(x), y(y) {}
|
|
void set(int _x, int _y) { x = _x; y = _y; }
|
|
auto operator <=> (const vec2&) const = default;
|
|
};
|
|
|
|
struct Window {
|
|
std::array<std::array<char, COL>, ROW> view;
|
|
void draw_display();
|
|
void update_display(const vec2 fruit, std::deque<vec2> &buf);
|
|
};
|
|
|
|
class Engine {
|
|
public:
|
|
Engine();
|
|
~Engine() { endwin(); }
|
|
void run();
|
|
|
|
private:
|
|
Window win;
|
|
std::deque<vec2> tail;
|
|
std::string status;
|
|
vec2 head { COL / 2, ROW / 2 };
|
|
vec2 fruit { rand() % COL, rand() % ROW };
|
|
vec2 prev_head { head.x, head.y };
|
|
Direction dir = Direction::RIGHT;
|
|
unsigned int m_score = 0;
|
|
bool process_input();
|
|
void determine_index_zero_pos();
|
|
void move_tail_indicies();
|
|
void add_tail_and_respawn_fruit();
|
|
bool exit_summary();
|
|
};
|