refactor: first step to final product by removing redundant code
This commit is contained in:
+2
-6
@@ -5,12 +5,8 @@
|
|||||||
|
|
||||||
struct Fruit {
|
struct Fruit {
|
||||||
Fruit();
|
Fruit();
|
||||||
int unsigned m_row;
|
|
||||||
int unsigned m_col;
|
|
||||||
int unsigned m_total_eaten;
|
|
||||||
Point m_point;
|
Point m_point;
|
||||||
|
void reset(std::array<std::array<char, COL>, ROW> &board, int row, int col, int &score);
|
||||||
void create_point();
|
|
||||||
void reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FRUIT_HPP
|
#endif // FRUIT_HPP
|
||||||
|
|||||||
+2
-2
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
constexpr uint16_t ROW = 10;
|
constexpr uint16_t ROW = 11;
|
||||||
constexpr uint16_t COL = 10;
|
constexpr uint16_t COL = 11;
|
||||||
constexpr uint16_t FPS = 5;
|
constexpr uint16_t FPS = 5;
|
||||||
|
|
||||||
#endif // GLOBALS_HPP
|
#endif // GLOBALS_HPP
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "point.hpp"
|
#include "point.hpp"
|
||||||
|
|
||||||
struct Head {
|
struct Head {
|
||||||
|
Head();
|
||||||
Head(int default_direction, int row, int col);
|
Head(int default_direction, int row, int col);
|
||||||
Point m_head;
|
Point m_head;
|
||||||
Point m_previous_head;
|
Point m_previous_head;
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ struct Window {
|
|||||||
int m_previous_reset;
|
int m_previous_reset;
|
||||||
|
|
||||||
void draw_window();
|
void draw_window();
|
||||||
void clear_window();
|
|
||||||
void tick(double frames_per_second);
|
void tick(double frames_per_second);
|
||||||
void kill(bool &running);
|
void kill(bool &running);
|
||||||
void update_score(Fruit &fruit, Body &body);
|
void update_score(Fruit &fruit, Body &body);
|
||||||
|
|||||||
+7
-12
@@ -5,20 +5,15 @@
|
|||||||
|
|
||||||
Fruit::Fruit() {
|
Fruit::Fruit() {
|
||||||
std::srand(std::time(0));
|
std::srand(std::time(0));
|
||||||
create_point();
|
m_point.m_row = (rand() % 9);
|
||||||
m_total_eaten = 0;
|
m_point.m_col = (rand() % 9);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Fruit::create_point() {
|
void Fruit::reset(std::array<std::array<char, COL>, ROW> &board, int row, int col, int &score) {
|
||||||
m_point = Point {rand() % 9, rand() % 9};
|
if(m_point.m_row == row && m_point.m_col == col) {
|
||||||
m_row = m_point.m_row;
|
m_point.clear(board);
|
||||||
m_col = m_point.m_col;
|
m_point.m_row = (rand() % 9);
|
||||||
}
|
m_point.m_col = (rand() % 9);
|
||||||
|
|
||||||
void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score) {
|
|
||||||
if(m_row == row && m_col == col) {
|
|
||||||
m_point.clear(arr);
|
|
||||||
create_point();
|
|
||||||
score++;
|
score++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -6,12 +6,12 @@
|
|||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "input.hpp"
|
#include "input.hpp"
|
||||||
|
|
||||||
int main (int argc, char *argv[]) {
|
int main () {
|
||||||
Window window;
|
Window window;
|
||||||
Input input;
|
Input input;
|
||||||
Fruit fruit;
|
Fruit fruit;
|
||||||
Body body;
|
Body body;
|
||||||
Head head { 2, 0, 0 };
|
Head head;
|
||||||
bool running = true;
|
bool running = true;
|
||||||
bool collision = false;
|
bool collision = false;
|
||||||
std::thread input_thread(
|
std::thread input_thread(
|
||||||
@@ -19,6 +19,7 @@ int main (int argc, char *argv[]) {
|
|||||||
std::ref(head.m_head.m_direction),
|
std::ref(head.m_head.m_direction),
|
||||||
std::ref(collision),
|
std::ref(collision),
|
||||||
std::ref(running));
|
std::ref(running));
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
body.collision(head, collision);
|
body.collision(head, collision);
|
||||||
window.update_score(fruit, body);
|
window.update_score(fruit, body);
|
||||||
@@ -29,7 +30,7 @@ int main (int argc, char *argv[]) {
|
|||||||
|
|
||||||
fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score);
|
fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score);
|
||||||
window.m_board[head.m_head.m_row][head.m_head.m_col] = '*';
|
window.m_board[head.m_head.m_row][head.m_head.m_col] = '*';
|
||||||
window.m_board[fruit.m_row][fruit.m_col] = 'X';
|
window.m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
|
||||||
body.draw(window.m_board);
|
body.draw(window.m_board);
|
||||||
window.update_display(FPS);
|
window.update_display(FPS);
|
||||||
body.clear(window.m_board);
|
body.clear(window.m_board);
|
||||||
|
|||||||
+7
-1
@@ -1,5 +1,9 @@
|
|||||||
#include "snake.hpp"
|
#include "snake.hpp"
|
||||||
#include <iostream>
|
|
||||||
|
Head::Head() {
|
||||||
|
m_head = Point { 5, 5, 2 };
|
||||||
|
m_previous_head.set_new_point(m_previous_head.m_row = 0, m_previous_head.m_col = 0, m_previous_head.m_direction = 2);
|
||||||
|
}
|
||||||
|
|
||||||
Head::Head(int default_direction, int row, int col) {
|
Head::Head(int default_direction, int row, int col) {
|
||||||
m_head = Point { row, col, default_direction };
|
m_head = Point { row, col, default_direction };
|
||||||
@@ -58,8 +62,10 @@ void Body::clear(std::array<std::array<char, COL>, ROW> &arr) {
|
|||||||
|
|
||||||
void Body::draw(std::array<std::array<char, COL>, ROW> &arr) {
|
void Body::draw(std::array<std::array<char, COL>, ROW> &arr) {
|
||||||
for(int i = 0; i < m_body.size(); i++) {
|
for(int i = 0; i < m_body.size(); i++) {
|
||||||
|
if(i < m_body.size()) {
|
||||||
arr[m_body.at(i).m_tail_pos.m_row][m_body.at(i).m_tail_pos.m_col] = '0';
|
arr[m_body.at(i).m_tail_pos.m_row][m_body.at(i).m_tail_pos.m_col] = '0';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Body::move(Head &head) {
|
void Body::move(Head &head) {
|
||||||
|
|||||||
+3
-12
@@ -33,17 +33,8 @@ void Window::draw_window() {
|
|||||||
std::cout << std::flush;
|
std::cout << std::flush;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::clear_window() {
|
|
||||||
std::cout<<"\033[H" << std::flush;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::tick(double frames_per_second) {
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds((int) (1000 / frames_per_second)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::update_score(Fruit &fruit, Body &body) {
|
void Window::update_score(Fruit &fruit, Body &body) {
|
||||||
if(m_score % 1 == 0 && m_score >= 1) {
|
if(m_score >= 1) {
|
||||||
fruit.m_total_eaten++;
|
|
||||||
m_score = 0;
|
m_score = 0;
|
||||||
body.m_body.push_back(Tail{2, 0, 0});
|
body.m_body.push_back(Tail{2, 0, 0});
|
||||||
}
|
}
|
||||||
@@ -54,7 +45,7 @@ void Window::kill(bool &running) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Window::update_display(int fps) {
|
void Window::update_display(int fps) {
|
||||||
clear_window();
|
std::cout<<"\033[H" << std::flush;
|
||||||
draw_window();
|
draw_window();
|
||||||
tick(fps);
|
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user