refactor: removing some deprecated functions and removed array for c style array

This commit is contained in:
DarthMooMancer
2025-09-12 20:31:42 -04:00
parent cfa0e52705
commit 4bab69a3a8
9 changed files with 33 additions and 58 deletions
+7 -1
View File
@@ -2,12 +2,18 @@
#include <cstdlib>
#include <ctime>
void Point::set_new_point(int row, int col, int dir) {
m_row = row;
m_col = col;
m_direction = dir;
}
Fruit::Fruit() {
std::srand(std::time(0));
m_point.set_new_point((rand() % 9), (rand() % 9), 0);
}
void Fruit::get_new_pos(std::array<std::array<char, COL>, ROW> &board, Head &head, int &score) {
void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head, int &score) {
if(m_point.m_row == head.m_head.m_row && m_point.m_col == head.m_head.m_col) {
m_point.set_new_point((rand() % 9), (rand() % 9), 0);
score++;
-11
View File
@@ -1,11 +0,0 @@
#include "point.hpp"
void Point::clear(std::array<std::array<char, COL>, ROW> &board) const {
board[m_row][m_col] = '.';
}
void Point::set_new_point(int row, int col, int dir) {
m_row = row;
m_col = col;
m_direction = dir;
}
+9 -16
View File
@@ -3,31 +3,24 @@
#include <chrono>
#include <thread>
Window::Window() {
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
}
void Window::update_display(Fruit &fruit, Head &head, Body &body) {
// Update symbols of positions
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < ROW; j++) {
m_board[i][j] = '.';
}
}
m_board[head.m_head.m_row][head.m_head.m_col] = '*';
m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
for(int i = 0; i < body.m_body.size(); i++) {
m_board[body.m_body.at(i).m_tail_pos.m_row][body.m_body.at(i).m_tail_pos.m_col] = '0';
m_board[body.m_body.at(i).m_tail_pos.m_row][body.m_body.at(i).m_tail_pos.m_col] = '@';
}
std::cout << "\033[H" << std::flush; // Clear screen
// Draw Screen
for(std::array<char, COL> &row : m_board) {
for(char &col : row) {
std::cout << col << " ";
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < ROW; j++) {
std::cout << m_board[i][j] << " ";
}
std::cout << "\r\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps
// Clear old spaces
for(int i = 0; i < body.m_body.size(); i++) {
body.m_body.at(i).m_tail_pos.clear(m_board);
}
head.m_head.clear(m_board);
fruit.m_point.clear(m_board);
}