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
+18 -18
View File
@@ -1,8 +1,8 @@
#include "element.hpp"
#include "globals.hpp"
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <ranges>
int _score = 0;
int _snake_size = 3;
@@ -11,44 +11,42 @@ int get_score() {
return _score;
}
void Point::set_new_point(int row, int col) {
m_row = row;
m_col = col;
void Point::set_new_point(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->m_row, head->m_col);
buffer[PREVIOUS_HEAD]->set_new_point(head->x, head->y);
switch(head->m_dir) {
case LEFT: head->m_col = (head->m_col + COL - 1) % COL; break;
case RIGHT: head->m_col = (head->m_col + 1) % COL; break;
case UP: head->m_row = (head->m_row + ROW - 1) % ROW; break;
case DOWN: head->m_row = (head->m_row + 1) % ROW; break;
case LEFT: head->x = (head->x + COL - 1) % COL; break;
case RIGHT: head->x = (head->x + 1) % COL; break;
case UP: head->y = (head->y + ROW - 1) % ROW; break;
case DOWN: head->y = (head->y + 1) % ROW; break;
}
}
void move_tail_indicies(Point** buffer) {
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];
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;
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) {
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 {
row = (rand() % ROW);
col = (rand() % COL);
} while (board[row][col].m_symbol != '.');
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, '@' };
_score++;
_snake_size++;
@@ -56,9 +54,11 @@ void on_fruit_eaten(Point (*board)[COL], Point** buffer) {
}
bool segment_overlap(Point** buffer) {
for(int i = 0; i < _snake_size; i++) {
if(buffer[i + 3] == nullptr) continue;
if(buffer[i + 3]->m_row == buffer[HEAD]->m_row && buffer[i + 3]->m_col == buffer[HEAD]->m_col) {
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)
{
return true;
}
}
+4 -5
View File
@@ -11,9 +11,9 @@ int main () {
bool running = true;
Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail
buffer[HEAD] = new Point { ROW / 2, COL / 2, '*' }; // Head
buffer[PREVIOUS_HEAD] = new Point { buffer[HEAD]->m_row, buffer[HEAD]->m_col, '.' };
buffer[FRUIT] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit
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' };
std::thread input_thread([&] { get_input(buffer[HEAD]->m_dir, running ); } );
@@ -25,8 +25,7 @@ int main () {
if(buffer[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(buffer)) {
std::cout << "You collided with yourself and died! Score: " << get_score() << std::endl;
running = false;
}
+9 -8
View File
@@ -1,20 +1,21 @@
#include "window.hpp"
#include <algorithm>
#include <iostream>
#include <chrono>
#include <ranges>
#include <thread>
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))) {
m_board[i][j].m_symbol = '.';
for(int i : std::ranges::views::iota(0, ROW)) {
for(int j : std::ranges::views::iota(0, COL)) {
m_board[i][j].m_symbol = '.';
}
}
}
void Window::draw_display(int milliseconds) {
std::cout << "\033[H" << std::flush; // Clear screen
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) {
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 << " "; }
else { std::cout << ". "; }
}
@@ -24,8 +25,8 @@ void Window::draw_display(int milliseconds) {
}
void Window::update_display(Point** buffer) {
for(int i = 0; i < ROW * COL; i++) {
if(buffer[i] == nullptr || i == 1 || buffer[i]->m_row > ROW - 1 || buffer[i]->m_col > COL - 1) continue;
m_board[buffer[i]->m_row][buffer[i]->m_col].m_symbol = buffer[i]->m_symbol;
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;
}
}