converting to c

This commit is contained in:
Andrew Haynes
2026-03-03 13:06:45 -05:00
parent 820c4b6dc4
commit 3d6a6142bd
9 changed files with 308 additions and 197 deletions
+62
View File
@@ -0,0 +1,62 @@
#ifndef ENGINE_HPP
#define ENGINE_HPP
#include <stddef.h>
#define ROW 15
#define COL 21
typedef enum
{
UP,
DOWN,
LEFT,
RIGHT
} Direction;
typedef struct
{
int x, y;
} vec2;
int
equals(vec2* a, vec2* b);
void
set(vec2* vec2, int _x, int _y);
typedef struct
{
unsigned char view[ROW][COL];
vec2 head;
vec2 fruit;
vec2 prev_head;
int m_score;
Direction dir;
vec2* tail[(ROW * COL) - 1];
int tail_size;
} Engine;
void
engine_init(Engine* engine);
void
engine_display(Engine* engine);
int
process_input(Direction* dir);
int
is_open(char* status, size_t len, Engine* engine);
void
determine_index_zero_pos(Engine* engine);
void
add_tail_and_respawn_fruit(Engine* engine);
void
move_tail_indicies(vec2** tail, int tail_size, vec2* prev_head,
unsigned char (*view)[COL]);
#endif
-48
View File
@@ -1,48 +0,0 @@
#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();
};