shorthand names without being to short for clarity
This commit is contained in:
+9
-9
@@ -10,19 +10,19 @@ enum Direction {
|
||||
RIGHT = 1
|
||||
};
|
||||
|
||||
struct Point {
|
||||
struct Vector2i {
|
||||
int x, y;
|
||||
char m_symbol;
|
||||
char id;
|
||||
enum Direction m_dir;
|
||||
Point() : x(0), y(0), m_symbol('@') {}
|
||||
Point(int x, int y, char s) : x(x), y(y), m_symbol(s) {}
|
||||
void set_new_point(int x, int y);
|
||||
Vector2i() : x(0), y(0), id('@') {}
|
||||
Vector2i(int x, int y, char s) : x(x), y(y), id(s) {}
|
||||
void set(int x, int y);
|
||||
};
|
||||
|
||||
int get_score();
|
||||
void determine_index_zero_pos(Point** buffer);
|
||||
void move_tail_indicies(Point** buffer);
|
||||
void on_fruit_eaten(Point (*board)[COL], Point** buffer);
|
||||
bool segment_overlap(Point** buffer);
|
||||
void determine_index_zero_pos(Vector2i** buf);
|
||||
void move_tail_indicies(Vector2i** buf);
|
||||
void on_fruit_eaten(Vector2i (*board)[COL], Vector2i** buf);
|
||||
bool segment_overlap(Vector2i** buf);
|
||||
|
||||
#endif // ELEMENT_HPP
|
||||
|
||||
+2
-2
@@ -5,10 +5,10 @@
|
||||
#include "element.hpp"
|
||||
|
||||
struct Window {
|
||||
Point m_board[ROW][COL] = {};
|
||||
Vector2i board[ROW][COL] = {};
|
||||
void clear_display();
|
||||
void draw_display(int milliseconds);
|
||||
void update_display(Point** buffer);
|
||||
void update_display(Vector2i** buf);
|
||||
};
|
||||
|
||||
#endif // WINDOW_HPP
|
||||
|
||||
+25
-28
@@ -7,18 +7,16 @@
|
||||
int _score = 0;
|
||||
int _snake_size = 3;
|
||||
|
||||
int get_score() {
|
||||
return _score;
|
||||
}
|
||||
int get_score() { return _score; }
|
||||
|
||||
void Point::set_new_point(int _x, int _y) {
|
||||
void Vector2i::set(int _x, int _y) {
|
||||
y = _y;
|
||||
x = _x;
|
||||
}
|
||||
|
||||
void determine_index_zero_pos(Point** buffer) {
|
||||
auto& head = buffer[HEAD];
|
||||
buffer[PREVIOUS_HEAD]->set_new_point(head->x, head->y);
|
||||
void determine_index_zero_pos(Vector2i** buf) {
|
||||
auto& head = buf[HEAD];
|
||||
buf[PREVIOUS_HEAD]->set(head->x, head->y);
|
||||
|
||||
switch(head->m_dir) {
|
||||
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;
|
||||
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;
|
||||
if(buffer[3] != nullptr) buffer[3]->set_new_point(buffer[PREVIOUS_HEAD]->x, buffer[PREVIOUS_HEAD]->y);
|
||||
buf[3] = last_point;
|
||||
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;
|
||||
if(buffer[FRUIT]->y == buffer[HEAD]->y && buffer[FRUIT]->x == buffer[HEAD]->x) {
|
||||
do {
|
||||
row = (rand() % ROW);
|
||||
col = (rand() % COL);
|
||||
} while (board[row][col].m_symbol != '.');
|
||||
buffer[FRUIT]->set_new_point(row, col);
|
||||
std::memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*));
|
||||
buffer[3] = new Point { ROW, COL, '@' };
|
||||
_score++;
|
||||
_snake_size++;
|
||||
}
|
||||
if(buf[FRUIT]->y != buf[HEAD]->y || buf[FRUIT]->x != buf[HEAD]->x) return;
|
||||
do {
|
||||
row = (rand() % ROW);
|
||||
col = (rand() % COL);
|
||||
} while (board[row][col].id != '.');
|
||||
buf[FRUIT]->set(row, col);
|
||||
std::memmove(buf + 4, buf + 3, (_snake_size - 3) * sizeof(Vector2i*));
|
||||
buf[3] = new Vector2i { ROW, COL, '@' };
|
||||
_score++;
|
||||
_snake_size++;
|
||||
}
|
||||
|
||||
bool segment_overlap(Point** buffer) {
|
||||
bool segment_overlap(Vector2i** buf) {
|
||||
for(int i : std::ranges::views::iota(3, _snake_size)) {
|
||||
if(buffer[i] != nullptr &&
|
||||
buffer[i]->y == buffer[HEAD]->y &&
|
||||
buffer[i]->x == buffer[HEAD]->x)
|
||||
if(buf[i] &&
|
||||
buf[i]->y == buf[HEAD]->y &&
|
||||
buf[i]->x == buf[HEAD]->x)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
+15
-15
@@ -7,35 +7,35 @@
|
||||
|
||||
int main () {
|
||||
std::srand(std::time(0));
|
||||
Window window;
|
||||
Window win;
|
||||
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, '*' };
|
||||
buffer[PREVIOUS_HEAD] = new Point { buffer[HEAD]->x, buffer[HEAD]->y, '.' };
|
||||
buffer[FRUIT] = new Point { (rand() % COL), (rand() % ROW), 'o' };
|
||||
buf[HEAD] = new Vector2i { COL / 2, ROW / 2, '*' };
|
||||
buf[PREVIOUS_HEAD] = new Vector2i { buf[HEAD]->x, buf[HEAD]->y, '.' };
|
||||
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) {
|
||||
on_fruit_eaten(window.m_board, buffer);
|
||||
determine_index_zero_pos(buffer);
|
||||
move_tail_indicies(buffer);
|
||||
on_fruit_eaten(win.board, buf);
|
||||
determine_index_zero_pos(buf);
|
||||
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;
|
||||
running = false;
|
||||
} if(segment_overlap(buffer)) {
|
||||
} if(segment_overlap(buf)) {
|
||||
std::cout << "You collided with yourself and died! Score: " << get_score() << std::endl;
|
||||
running = false;
|
||||
}
|
||||
|
||||
window.clear_display();
|
||||
window.update_display(buffer);
|
||||
window.draw_display(200);
|
||||
win.clear_display();
|
||||
win.update_display(buf);
|
||||
win.draw_display(200);
|
||||
}
|
||||
|
||||
input_thread.join();
|
||||
for(auto& p : buffer) delete p;
|
||||
for(auto& p : buf) delete p;
|
||||
return 0;
|
||||
}
|
||||
|
||||
+6
-5
@@ -1,4 +1,5 @@
|
||||
#include "window.hpp"
|
||||
#include "element.hpp"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <ranges>
|
||||
@@ -7,7 +8,7 @@
|
||||
void Window::clear_display() {
|
||||
for(int i : std::ranges::views::iota(0, ROW)) {
|
||||
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
|
||||
for(int i : std::ranges::views::iota(0, ROW)) {
|
||||
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 << ". "; }
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
void Window::update_display(Point** buffer) {
|
||||
void Window::update_display(Vector2i** buf) {
|
||||
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;
|
||||
m_board[buffer[i]->y][buffer[i]->x].m_symbol = buffer[i]->m_symbol;
|
||||
if(buf[i] == nullptr || i == 1 || buf[i]->y > ROW - 1 || buf[i]->x > COL - 1) continue;
|
||||
board[buf[i]->y][buf[i]->x].id = buf[i]->id;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user