starting an upgrade to move everything to just use ptrs and simplify the code a little

This commit is contained in:
DarthMooMancer
2025-09-24 14:25:03 -04:00
parent 626d7261d4
commit 0d47de8a7a
5 changed files with 73 additions and 48 deletions
+11 -5
View File
@@ -4,25 +4,31 @@
#include <vector> #include <vector>
#include "globals.hpp" #include "globals.hpp"
struct Point { struct Point {
int m_row; int m_row;
int m_col; int m_col;
Point() {} char m_symbol;
Point(int x, int y) : m_row(x), m_col(y) {} int m_direction;
Point() : m_row(0), m_col(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, int d) : m_row(x), m_col(y), m_symbol(s), m_direction(d) {}
void set_new_point(int row, int col); void set_new_point(int row, int col);
void set_new_point(Point &point); void set_new_point(Point &point);
}; };
void get_new_pos(Point* (*board)[COL], Point** segments);
struct Head { struct Head {
Head() : m_direction(2) {}; Head() : m_direction(2) {};
Point m_head {5, 5}; Point m_head {5, 5, '*'};
Point m_previous_head; Point m_previous_head;
int m_direction; int m_direction;
void get_new_pos(); void get_new_pos();
}; };
struct Tail { struct Tail {
Point m_tail_pos; Point m_tail_pos {0, 0, '@'};
Point m_previous_tail_pos; Point m_previous_tail_pos;
}; };
@@ -35,7 +41,7 @@ struct Body {
struct Fruit { struct Fruit {
Fruit(); Fruit();
Point m_point; Point m_point {0, 0, 'o'};
void get_new_pos(char (&board)[ROW][COL], Head &head); void get_new_pos(char (&board)[ROW][COL], Head &head);
}; };
+3 -2
View File
@@ -5,8 +5,9 @@
#include "element.hpp" #include "element.hpp"
struct Window { struct Window {
char m_board[ROW][COL] = {}; Point* m_board[ROW][COL] = {};
void update_display(Fruit &fruit, Head &head, Body &body); void draw_display();
void update_display(Point* segments[]);
}; };
#endif // WINDOW_HPP #endif // WINDOW_HPP
+22 -13
View File
@@ -1,3 +1,4 @@
// Used ptr to take a copy of the head, move head move tail at end of array to previous head position from copy and make the old tail a nullptr
#include "element.hpp" #include "element.hpp"
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
@@ -29,20 +30,28 @@ void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head) {
} }
} }
void Head::get_new_pos() { void get_new_pos(Point* (*board)[COL], Point** segments) {
if (m_direction == -1) { Point new_point;
if (m_head.m_col <= 0) m_head.m_col = COL - 1; new_point.m_row = segments[0]->m_row;
else m_head.m_col--; new_point.m_col = segments[0]->m_col;
} else if (m_direction == 1) {
if (m_head.m_col >= COL - 1) m_head.m_col = 0; if (segments[0]->m_direction == -1) {
else m_head.m_col++; if (new_point.m_col <= 0) new_point.m_col = COL - 1;
} else if (m_direction == -2) { else new_point.m_col--;
if (m_head.m_row <= 0) m_head.m_row = ROW - 1; } else if (segments[0]->m_direction == 1) {
else m_head.m_row--; if (new_point.m_col >= COL - 1) new_point.m_col = 0;
} else if (m_direction == 2) { else new_point.m_col++;
if (m_head.m_row >= ROW - 1) m_head.m_row = 0; } else if (segments[0]->m_direction == -2) {
else m_head.m_row++; if (new_point.m_row <= 0) new_point.m_row = ROW - 1;
else new_point.m_row--;
} else if (segments[0]->m_direction == 2) {
if (new_point.m_row >= ROW - 1) new_point.m_row = 0;
else new_point.m_row++;
} }
board[segments[0]->m_row][segments[0]->m_col] = nullptr;
segments[0]->m_row = new_point.m_row;
segments[0]->m_col = new_point.m_col;
board[segments[0]->m_row][segments[0]->m_col] = segments[0];
} }
bool Body::check_collision(Head &head) { bool Body::check_collision(Head &head) {
+26 -15
View File
@@ -11,28 +11,39 @@ int main () {
Input input; Input input;
Fruit fruit; Fruit fruit;
Body body; Body body;
Head head;
bool running = true; bool running = true;
Point* segments[ROW * COL] = {};
segments[0] = new Point { 5, 5, '*', 1 };
// segments[1] = new Point { 6, 5, '@', 0 };
std::thread input_thread( std::thread input_thread(
&Input::get_input, &input, &Input::get_input, &input,
std::ref(head.m_direction), std::ref(segments[0]->m_direction),
std::ref(running)); std::ref(running));
while (running) { while (running) {
body.check_new_tail(); get_new_pos(window.m_board, segments);
if(body.m_body.size() >= (ROW * COL)) { // body.check_new_tail();
std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl; // if(body.m_body.size() >= (ROW * COL)) {
running = false; // std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
} else if(body.check_collision(head)) { // running = false;
std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl; // } else if(body.check_collision(head)) {
running = false; // std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
// running = false;
// }
// head.get_new_pos();
// // fruit.get_new_pos(window.m_board, head);
// body.get_new_pos(head);
//
// head.m_previous_head.set_new_point(head.m_head);
window.draw_display();
window.update_display(segments);
}
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) {
delete window.m_board[i][j];
} }
head.get_new_pos();
fruit.get_new_pos(window.m_board, head);
body.get_new_pos(head);
head.m_previous_head.set_new_point(head.m_head);
window.update_display(fruit, head, body);
} }
input_thread.join(); input_thread.join();
+11 -13
View File
@@ -3,24 +3,22 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
void Window::update_display(Fruit &fruit, Head &head, Body &body) { void Window::draw_display() {
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] = 'o';
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] = '@';
}
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 = 0; i < ROW; i++) {
for(int j = 0; j < ROW; j++) { for(int j = 0; j < ROW; j++) {
std::cout << m_board[i][j] << " "; if(m_board[i][j] == nullptr) { std::cout << ". "; }
if(m_board[i][j] != nullptr) { std::cout << m_board[i][j]->m_symbol << " "; }
} }
std::cout << "\r\n"; std::cout << "\r\n";
} }
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps
} }
void Window::update_display(Point* segments[]) {
for(int i = 0; i < ROW * COL; i++) {
if(segments[i] != nullptr) {
m_board[segments[i]->m_row][segments[i]->m_col] = segments[i];
}
}
}