shorthand names without being to short for clarity

This commit is contained in:
DarthMooMancer
2025-11-03 14:02:27 -05:00
parent 2c38da4b79
commit 1af33ba5a4
5 changed files with 57 additions and 59 deletions
+9 -9
View File
@@ -10,19 +10,19 @@ enum Direction {
RIGHT = 1 RIGHT = 1
}; };
struct Point { struct Vector2i {
int x, y; int x, y;
char m_symbol; char id;
enum Direction m_dir; enum Direction m_dir;
Point() : x(0), y(0), m_symbol('@') {} Vector2i() : x(0), y(0), id('@') {}
Point(int x, int y, char s) : x(x), y(y), m_symbol(s) {} Vector2i(int x, int y, char s) : x(x), y(y), id(s) {}
void set_new_point(int x, int y); void set(int x, int y);
}; };
int get_score(); int get_score();
void determine_index_zero_pos(Point** buffer); void determine_index_zero_pos(Vector2i** buf);
void move_tail_indicies(Point** buffer); void move_tail_indicies(Vector2i** buf);
void on_fruit_eaten(Point (*board)[COL], Point** buffer); void on_fruit_eaten(Vector2i (*board)[COL], Vector2i** buf);
bool segment_overlap(Point** buffer); bool segment_overlap(Vector2i** buf);
#endif // ELEMENT_HPP #endif // ELEMENT_HPP
+2 -2
View File
@@ -5,10 +5,10 @@
#include "element.hpp" #include "element.hpp"
struct Window { struct Window {
Point m_board[ROW][COL] = {}; Vector2i board[ROW][COL] = {};
void clear_display(); void clear_display();
void draw_display(int milliseconds); void draw_display(int milliseconds);
void update_display(Point** buffer); void update_display(Vector2i** buf);
}; };
#endif // WINDOW_HPP #endif // WINDOW_HPP
+20 -23
View File
@@ -7,18 +7,16 @@
int _score = 0; int _score = 0;
int _snake_size = 3; int _snake_size = 3;
int get_score() { int get_score() { return _score; }
return _score;
}
void Point::set_new_point(int _x, int _y) { void Vector2i::set(int _x, int _y) {
y = _y; y = _y;
x = _x; x = _x;
} }
void determine_index_zero_pos(Point** buffer) { void determine_index_zero_pos(Vector2i** buf) {
auto& head = buffer[HEAD]; auto& head = buf[HEAD];
buffer[PREVIOUS_HEAD]->set_new_point(head->x, head->y); buf[PREVIOUS_HEAD]->set(head->x, head->y);
switch(head->m_dir) { switch(head->m_dir) {
case LEFT: head->x = (head->x + COL - 1) % COL; break; case LEFT: head->x = (head->x + COL - 1) % COL; break;
@@ -28,36 +26,35 @@ void determine_index_zero_pos(Point** buffer) {
} }
} }
void move_tail_indicies(Point** buffer) { void move_tail_indicies(Vector2i** buf) {
if(_snake_size <= 3) return; if(_snake_size <= 3) return;
Point* last_point = buffer[_snake_size - 1]; Vector2i* last_point = buf[_snake_size - 1];
std::memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*)); std::memmove(buf + 4, buf + 3, (_snake_size - 4) * sizeof(Vector2i*));
buffer[3] = last_point; buf[3] = last_point;
if(buffer[3] != nullptr) buffer[3]->set_new_point(buffer[PREVIOUS_HEAD]->x, buffer[PREVIOUS_HEAD]->y); if(buf[3] != nullptr) last_point->set(buf[PREVIOUS_HEAD]->x, buf[PREVIOUS_HEAD]->y);
} }
void on_fruit_eaten(Point (*board)[COL], Point** buffer) { void on_fruit_eaten(Vector2i (*board)[COL], Vector2i** buf) {
int row, col; int row, col;
if(buffer[FRUIT]->y == buffer[HEAD]->y && buffer[FRUIT]->x == buffer[HEAD]->x) { if(buf[FRUIT]->y != buf[HEAD]->y || buf[FRUIT]->x != buf[HEAD]->x) return;
do { do {
row = (rand() % ROW); row = (rand() % ROW);
col = (rand() % COL); col = (rand() % COL);
} while (board[row][col].m_symbol != '.'); } while (board[row][col].id != '.');
buffer[FRUIT]->set_new_point(row, col); buf[FRUIT]->set(row, col);
std::memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*)); std::memmove(buf + 4, buf + 3, (_snake_size - 3) * sizeof(Vector2i*));
buffer[3] = new Point { ROW, COL, '@' }; buf[3] = new Vector2i { ROW, COL, '@' };
_score++; _score++;
_snake_size++; _snake_size++;
} }
}
bool segment_overlap(Point** buffer) { bool segment_overlap(Vector2i** buf) {
for(int i : std::ranges::views::iota(3, _snake_size)) { for(int i : std::ranges::views::iota(3, _snake_size)) {
if(buffer[i] != nullptr && if(buf[i] &&
buffer[i]->y == buffer[HEAD]->y && buf[i]->y == buf[HEAD]->y &&
buffer[i]->x == buffer[HEAD]->x) buf[i]->x == buf[HEAD]->x)
{ {
return true; return true;
} }
+15 -15
View File
@@ -7,35 +7,35 @@
int main () { int main () {
std::srand(std::time(0)); std::srand(std::time(0));
Window window; Window win;
bool running = true; bool running = true;
Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail Vector2i* buf[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail
buffer[HEAD] = new Point { COL / 2, ROW / 2, '*' }; buf[HEAD] = new Vector2i { COL / 2, ROW / 2, '*' };
buffer[PREVIOUS_HEAD] = new Point { buffer[HEAD]->x, buffer[HEAD]->y, '.' }; buf[PREVIOUS_HEAD] = new Vector2i { buf[HEAD]->x, buf[HEAD]->y, '.' };
buffer[FRUIT] = new Point { (rand() % COL), (rand() % ROW), 'o' }; buf[FRUIT] = new Vector2i { (rand() % COL), (rand() % ROW), 'o' };
std::thread input_thread([&] { get_input(buffer[HEAD]->m_dir, running ); } ); std::thread input_thread([&] { get_input(buf[HEAD]->m_dir, running ); } );
while (running) { while (running) {
on_fruit_eaten(window.m_board, buffer); on_fruit_eaten(win.board, buf);
determine_index_zero_pos(buffer); determine_index_zero_pos(buf);
move_tail_indicies(buffer); move_tail_indicies(buf);
if(buffer[MAX_LEN - 1] != nullptr) { if(buf[MAX_LEN - 1] != nullptr) {
std::cout << "You filled the whole board and won! Score: " << get_score() << std::endl; std::cout << "You filled the whole board and won! Score: " << get_score() << std::endl;
running = false; running = false;
} if(segment_overlap(buffer)) { } if(segment_overlap(buf)) {
std::cout << "You collided with yourself and died! Score: " << get_score() << std::endl; std::cout << "You collided with yourself and died! Score: " << get_score() << std::endl;
running = false; running = false;
} }
window.clear_display(); win.clear_display();
window.update_display(buffer); win.update_display(buf);
window.draw_display(200); win.draw_display(200);
} }
input_thread.join(); input_thread.join();
for(auto& p : buffer) delete p; for(auto& p : buf) delete p;
return 0; return 0;
} }
+6 -5
View File
@@ -1,4 +1,5 @@
#include "window.hpp" #include "window.hpp"
#include "element.hpp"
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <ranges> #include <ranges>
@@ -7,7 +8,7 @@
void Window::clear_display() { void Window::clear_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)) {
m_board[i][j].m_symbol = '.'; board[i][j].id = '.';
} }
} }
} }
@@ -16,7 +17,7 @@ void Window::draw_display(int milliseconds) {
std::cout << "\033[H" << std::flush; // Clear screen std::cout << "\033[H" << std::flush; // Clear screen
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)) {
if(m_board[i][j].m_symbol != '.') { std::cout << m_board[i][j].m_symbol << " "; } if(board[i][j].id != '.') { std::cout << board[i][j].id << " "; }
else { std::cout << ". "; } else { std::cout << ". "; }
} }
std::cout << "\r\n"; std::cout << "\r\n";
@@ -24,9 +25,9 @@ void Window::draw_display(int milliseconds) {
std::this_thread::sleep_for(std::chrono::milliseconds((int) (milliseconds))); // 1000 / fps; 200ms = 5fps std::this_thread::sleep_for(std::chrono::milliseconds((int) (milliseconds))); // 1000 / fps; 200ms = 5fps
} }
void Window::update_display(Point** buffer) { void Window::update_display(Vector2i** buf) {
for(int i : std::ranges::views::iota(0, ROW * COL)) { for(int i : std::ranges::views::iota(0, ROW * COL)) {
if(buffer[i] == nullptr || i == 1 || buffer[i]->y > ROW - 1 || buffer[i]->x > COL - 1) continue; if(buf[i] == nullptr || i == 1 || buf[i]->y > ROW - 1 || buf[i]->x > COL - 1) continue;
m_board[buffer[i]->y][buffer[i]->x].m_symbol = buffer[i]->m_symbol; board[buf[i]->y][buf[i]->x].id = buf[i]->id;
} }
} }