changes from m_row & m_col to x & y

This commit is contained in:
DarthMooMancer
2025-11-03 13:09:37 -05:00
parent ab16f18137
commit 2c38da4b79
4 changed files with 35 additions and 35 deletions
+4 -4
View File
@@ -11,12 +11,12 @@ enum Direction {
}; };
struct Point { struct Point {
int m_row, m_col; int x, y;
char m_symbol; char m_symbol;
enum Direction m_dir; enum Direction m_dir;
Point() : m_row(0), m_col(0), m_symbol('@') {} Point() : x(0), y(0), m_symbol('@') {}
Point(int x, int y, char s) : m_row(x), m_col(y), m_symbol(s) {} Point(int x, int y, char s) : x(x), y(y), m_symbol(s) {}
void set_new_point(int row, int col); void set_new_point(int x, int y);
}; };
int get_score(); int get_score();
+18 -18
View File
@@ -1,8 +1,8 @@
#include "element.hpp" #include "element.hpp"
#include "globals.hpp" #include "globals.hpp"
#include <cstring>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <ranges>
int _score = 0; int _score = 0;
int _snake_size = 3; int _snake_size = 3;
@@ -11,44 +11,42 @@ int get_score() {
return _score; return _score;
} }
void Point::set_new_point(int row, int col) { void Point::set_new_point(int _x, int _y) {
m_row = row; y = _y;
m_col = col; x = _x;
} }
void determine_index_zero_pos(Point** buffer) { void determine_index_zero_pos(Point** buffer) {
auto& head = buffer[HEAD]; auto& head = buffer[HEAD];
buffer[PREVIOUS_HEAD]->set_new_point(head->m_row, head->m_col); buffer[PREVIOUS_HEAD]->set_new_point(head->x, head->y);
switch(head->m_dir) { switch(head->m_dir) {
case LEFT: head->m_col = (head->m_col + COL - 1) % COL; break; case LEFT: head->x = (head->x + COL - 1) % COL; break;
case RIGHT: head->m_col = (head->m_col + 1) % COL; break; case RIGHT: head->x = (head->x + 1) % COL; break;
case UP: head->m_row = (head->m_row + ROW - 1) % ROW; break; case UP: head->y = (head->y + ROW - 1) % ROW; break;
case DOWN: head->m_row = (head->m_row + 1) % ROW; break; case DOWN: head->y = (head->y + 1) % ROW; break;
} }
} }
void move_tail_indicies(Point** buffer) { void move_tail_indicies(Point** buffer) {
if(_snake_size <= 3) return; if(_snake_size <= 3) return;
int new_row = buffer[PREVIOUS_HEAD]->m_row;
int new_col = buffer[PREVIOUS_HEAD]->m_col;
Point* last_point = buffer[_snake_size - 1]; Point* last_point = buffer[_snake_size - 1];
memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*)); std::memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*));
buffer[3] = last_point; buffer[3] = last_point;
if(buffer[3] != nullptr) buffer[3]->set_new_point(new_row, new_col); if(buffer[3] != nullptr) buffer[3]->set_new_point(buffer[PREVIOUS_HEAD]->x, buffer[PREVIOUS_HEAD]->y);
} }
void on_fruit_eaten(Point (*board)[COL], Point** buffer) { void on_fruit_eaten(Point (*board)[COL], Point** buffer) {
int row, col; int row, col;
if(buffer[FRUIT]->m_row == buffer[HEAD]->m_row && buffer[FRUIT]->m_col == buffer[HEAD]->m_col) { if(buffer[FRUIT]->y == buffer[HEAD]->y && buffer[FRUIT]->x == buffer[HEAD]->x) {
do { do {
row = (rand() % ROW); row = (rand() % ROW);
col = (rand() % COL); col = (rand() % COL);
} while (board[row][col].m_symbol != '.'); } while (board[row][col].m_symbol != '.');
buffer[FRUIT]->set_new_point(row, col); buffer[FRUIT]->set_new_point(row, col);
memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*)); std::memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*));
buffer[3] = new Point { ROW, COL, '@' }; buffer[3] = new Point { ROW, COL, '@' };
_score++; _score++;
_snake_size++; _snake_size++;
@@ -56,9 +54,11 @@ void on_fruit_eaten(Point (*board)[COL], Point** buffer) {
} }
bool segment_overlap(Point** buffer) { bool segment_overlap(Point** buffer) {
for(int i = 0; i < _snake_size; i++) { for(int i : std::ranges::views::iota(3, _snake_size)) {
if(buffer[i + 3] == nullptr) continue; if(buffer[i] != nullptr &&
if(buffer[i + 3]->m_row == buffer[HEAD]->m_row && buffer[i + 3]->m_col == buffer[HEAD]->m_col) { buffer[i]->y == buffer[HEAD]->y &&
buffer[i]->x == buffer[HEAD]->x)
{
return true; return true;
} }
} }
+4 -5
View File
@@ -11,9 +11,9 @@ int main () {
bool running = true; bool running = true;
Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail
buffer[HEAD] = new Point { ROW / 2, COL / 2, '*' }; // Head buffer[HEAD] = new Point { COL / 2, ROW / 2, '*' };
buffer[PREVIOUS_HEAD] = new Point { buffer[HEAD]->m_row, buffer[HEAD]->m_col, '.' }; buffer[PREVIOUS_HEAD] = new Point { buffer[HEAD]->x, buffer[HEAD]->y, '.' };
buffer[FRUIT] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit buffer[FRUIT] = new Point { (rand() % COL), (rand() % ROW), 'o' };
std::thread input_thread([&] { get_input(buffer[HEAD]->m_dir, running ); } ); std::thread input_thread([&] { get_input(buffer[HEAD]->m_dir, running ); } );
@@ -25,8 +25,7 @@ int main () {
if(buffer[MAX_LEN - 1] != nullptr) { if(buffer[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(buffer)) {
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;
} }
+8 -7
View File
@@ -1,20 +1,21 @@
#include "window.hpp" #include "window.hpp"
#include <algorithm>
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <ranges> #include <ranges>
#include <thread> #include <thread>
void Window::clear_display() { void Window::clear_display() {
for(auto [i, j] : std::ranges::views::cartesian_product(std::ranges::views::iota(0, ROW), std::ranges::views::iota(0, COL))) { for(int i : std::ranges::views::iota(0, ROW)) {
for(int j : std::ranges::views::iota(0, COL)) {
m_board[i][j].m_symbol = '.'; m_board[i][j].m_symbol = '.';
} }
} }
}
void Window::draw_display(int milliseconds) { 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 = 0; i < ROW; i++) { for(int i : std::ranges::views::iota(0, ROW)) {
for(int j = 0; j < COL; j++) { 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(m_board[i][j].m_symbol != '.') { std::cout << m_board[i][j].m_symbol << " "; }
else { std::cout << ". "; } else { std::cout << ". "; }
} }
@@ -24,8 +25,8 @@ void Window::draw_display(int milliseconds) {
} }
void Window::update_display(Point** buffer) { void Window::update_display(Point** buffer) {
for(int i = 0; i < ROW * COL; i++) { for(int i : std::ranges::views::iota(0, ROW * COL)) {
if(buffer[i] == nullptr || i == 1 || buffer[i]->m_row > ROW - 1 || buffer[i]->m_col > COL - 1) continue; if(buffer[i] == nullptr || i == 1 || buffer[i]->y > ROW - 1 || buffer[i]->x > COL - 1) continue;
m_board[buffer[i]->m_row][buffer[i]->m_col].m_symbol = buffer[i]->m_symbol; m_board[buffer[i]->y][buffer[i]->x].m_symbol = buffer[i]->m_symbol;
} }
} }