starting an upgrade to move everything to just use ptrs and simplify the code a little
This commit is contained in:
+11
-5
@@ -4,25 +4,31 @@
|
||||
#include <vector>
|
||||
#include "globals.hpp"
|
||||
|
||||
|
||||
struct Point {
|
||||
int m_row;
|
||||
int m_col;
|
||||
Point() {}
|
||||
Point(int x, int y) : m_row(x), m_col(y) {}
|
||||
char m_symbol;
|
||||
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(Point &point);
|
||||
};
|
||||
|
||||
void get_new_pos(Point* (*board)[COL], Point** segments);
|
||||
|
||||
struct Head {
|
||||
Head() : m_direction(2) {};
|
||||
Point m_head {5, 5};
|
||||
Point m_head {5, 5, '*'};
|
||||
Point m_previous_head;
|
||||
int m_direction;
|
||||
void get_new_pos();
|
||||
};
|
||||
|
||||
struct Tail {
|
||||
Point m_tail_pos;
|
||||
Point m_tail_pos {0, 0, '@'};
|
||||
Point m_previous_tail_pos;
|
||||
};
|
||||
|
||||
@@ -35,7 +41,7 @@ struct Body {
|
||||
|
||||
struct Fruit {
|
||||
Fruit();
|
||||
Point m_point;
|
||||
Point m_point {0, 0, 'o'};
|
||||
void get_new_pos(char (&board)[ROW][COL], Head &head);
|
||||
};
|
||||
|
||||
|
||||
+3
-2
@@ -5,8 +5,9 @@
|
||||
#include "element.hpp"
|
||||
|
||||
struct Window {
|
||||
char m_board[ROW][COL] = {};
|
||||
void update_display(Fruit &fruit, Head &head, Body &body);
|
||||
Point* m_board[ROW][COL] = {};
|
||||
void draw_display();
|
||||
void update_display(Point* segments[]);
|
||||
};
|
||||
|
||||
#endif // WINDOW_HPP
|
||||
|
||||
+22
-13
@@ -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 <cstdlib>
|
||||
#include <ctime>
|
||||
@@ -29,20 +30,28 @@ void Fruit::get_new_pos(char (&board)[ROW][COL], Head &head) {
|
||||
}
|
||||
}
|
||||
|
||||
void Head::get_new_pos() {
|
||||
if (m_direction == -1) {
|
||||
if (m_head.m_col <= 0) m_head.m_col = COL - 1;
|
||||
else m_head.m_col--;
|
||||
} else if (m_direction == 1) {
|
||||
if (m_head.m_col >= COL - 1) m_head.m_col = 0;
|
||||
else m_head.m_col++;
|
||||
} else if (m_direction == -2) {
|
||||
if (m_head.m_row <= 0) m_head.m_row = ROW - 1;
|
||||
else m_head.m_row--;
|
||||
} else if (m_direction == 2) {
|
||||
if (m_head.m_row >= ROW - 1) m_head.m_row = 0;
|
||||
else m_head.m_row++;
|
||||
void get_new_pos(Point* (*board)[COL], Point** segments) {
|
||||
Point new_point;
|
||||
new_point.m_row = segments[0]->m_row;
|
||||
new_point.m_col = segments[0]->m_col;
|
||||
|
||||
if (segments[0]->m_direction == -1) {
|
||||
if (new_point.m_col <= 0) new_point.m_col = COL - 1;
|
||||
else new_point.m_col--;
|
||||
} else if (segments[0]->m_direction == 1) {
|
||||
if (new_point.m_col >= COL - 1) new_point.m_col = 0;
|
||||
else new_point.m_col++;
|
||||
} else if (segments[0]->m_direction == -2) {
|
||||
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) {
|
||||
|
||||
+26
-15
@@ -11,28 +11,39 @@ int main () {
|
||||
Input input;
|
||||
Fruit fruit;
|
||||
Body body;
|
||||
Head head;
|
||||
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(
|
||||
&Input::get_input, &input,
|
||||
std::ref(head.m_direction),
|
||||
std::ref(segments[0]->m_direction),
|
||||
std::ref(running));
|
||||
|
||||
while (running) {
|
||||
body.check_new_tail();
|
||||
if(body.m_body.size() >= (ROW * COL)) {
|
||||
std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||
running = false;
|
||||
} else if(body.check_collision(head)) {
|
||||
std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||
running = false;
|
||||
get_new_pos(window.m_board, segments);
|
||||
// body.check_new_tail();
|
||||
// if(body.m_body.size() >= (ROW * COL)) {
|
||||
// std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||
// running = false;
|
||||
// } else if(body.check_collision(head)) {
|
||||
// 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();
|
||||
|
||||
+11
-13
@@ -3,24 +3,22 @@
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
void Window::update_display(Fruit &fruit, Head &head, Body &body) {
|
||||
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] = '@';
|
||||
}
|
||||
|
||||
void Window::draw_display() {
|
||||
std::cout << "\033[H" << std::flush; // Clear screen
|
||||
for(int i = 0; i < ROW; i++) {
|
||||
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::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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user