refactor: removing some deprecated functions and removed array for c style array
This commit is contained in:
@@ -10,8 +10,6 @@ set(SOURCES
|
|||||||
src/element.cpp
|
src/element.cpp
|
||||||
src/input.cpp
|
src/input.cpp
|
||||||
src/window.cpp
|
src/window.cpp
|
||||||
src/point.cpp
|
|
||||||
|
|
||||||
)
|
)
|
||||||
add_executable(snake ${SOURCES})
|
add_executable(snake ${SOURCES})
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
build/compile_commands.json
|
|
||||||
+13
-2
@@ -2,7 +2,18 @@
|
|||||||
#define ELEMENT_HPP
|
#define ELEMENT_HPP
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "point.hpp"
|
#include "globals.hpp"
|
||||||
|
|
||||||
|
struct Point {
|
||||||
|
int m_row;
|
||||||
|
int m_col;
|
||||||
|
int m_direction;
|
||||||
|
Point() {}
|
||||||
|
Point(int x, int y) : m_row(x), m_col(y) {}
|
||||||
|
Point(int x, int y, int dir) : m_row(x), m_col(y), m_direction(dir) {}
|
||||||
|
|
||||||
|
void set_new_point(int row, int col, int dir);
|
||||||
|
};
|
||||||
|
|
||||||
struct Head {
|
struct Head {
|
||||||
Head();
|
Head();
|
||||||
@@ -29,7 +40,7 @@ struct Body {
|
|||||||
struct Fruit {
|
struct Fruit {
|
||||||
Fruit();
|
Fruit();
|
||||||
Point m_point;
|
Point m_point;
|
||||||
void get_new_pos(std::array<std::array<char, COL>, ROW> &board, Head &head, int &score);
|
void get_new_pos(char (&board)[ROW][COL], Head &head, int &score);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ELEMENT_HPP
|
#endif // ELEMENT_HPP
|
||||||
|
|||||||
+2
-3
@@ -1,7 +1,6 @@
|
|||||||
#ifndef GLOBALS_HPP
|
#ifndef GLOBALS_HPP
|
||||||
#define GLOBALS_HPP
|
#define GLOBALS_HPP
|
||||||
|
#define ROW 10
|
||||||
constexpr unsigned int ROW = 11;
|
#define COL 10
|
||||||
constexpr unsigned int COL = 11;
|
|
||||||
|
|
||||||
#endif // GLOBALS_HPP
|
#endif // GLOBALS_HPP
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
#ifndef POINT_HPP
|
|
||||||
#define POINT_HPP
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include "globals.hpp"
|
|
||||||
|
|
||||||
struct Point {
|
|
||||||
int m_row;
|
|
||||||
int m_col;
|
|
||||||
int m_direction;
|
|
||||||
Point() {}
|
|
||||||
Point(int x, int y) : m_row(x), m_col(y) {}
|
|
||||||
Point(int x, int y, int dir) : m_row(x), m_col(y), m_direction(dir) {}
|
|
||||||
|
|
||||||
void clear(std::array<std::array<char, COL>, ROW> &board) const;
|
|
||||||
void set_new_point(int row, int col, int dir);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // POINT_HPP
|
|
||||||
+2
-3
@@ -1,12 +1,11 @@
|
|||||||
#ifndef WINDOW_HPP
|
#ifndef WINDOW_HPP
|
||||||
#define WINDOW_HPP
|
#define WINDOW_HPP
|
||||||
#include <array>
|
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "element.hpp"
|
#include "element.hpp"
|
||||||
|
|
||||||
struct Window {
|
struct Window {
|
||||||
Window();
|
char m_board[ROW][COL] = {};
|
||||||
std::array<std::array<char, COL>, ROW> m_board;
|
|
||||||
void update_display(Fruit &fruit, Head &head, Body &body);
|
void update_display(Fruit &fruit, Head &head, Body &body);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+7
-1
@@ -2,12 +2,18 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
|
void Point::set_new_point(int row, int col, int dir) {
|
||||||
|
m_row = row;
|
||||||
|
m_col = col;
|
||||||
|
m_direction = dir;
|
||||||
|
}
|
||||||
|
|
||||||
Fruit::Fruit() {
|
Fruit::Fruit() {
|
||||||
std::srand(std::time(0));
|
std::srand(std::time(0));
|
||||||
m_point.set_new_point((rand() % 9), (rand() % 9), 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) {
|
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);
|
m_point.set_new_point((rand() % 9), (rand() % 9), 0);
|
||||||
score++;
|
score++;
|
||||||
|
|||||||
@@ -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
@@ -3,31 +3,24 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
Window::Window() {
|
|
||||||
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::update_display(Fruit &fruit, Head &head, Body &body) {
|
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[head.m_head.m_row][head.m_head.m_col] = '*';
|
||||||
m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
|
m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
|
||||||
for(int i = 0; i < body.m_body.size(); i++) {
|
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
|
std::cout << "\033[H" << std::flush; // Clear screen
|
||||||
// Draw Screen
|
for(int i = 0; i < ROW; i++) {
|
||||||
for(std::array<char, COL> &row : m_board) {
|
for(int j = 0; j < ROW; j++) {
|
||||||
for(char &col : row) {
|
std::cout << m_board[i][j] << " ";
|
||||||
std::cout << col << " ";
|
|
||||||
}
|
}
|
||||||
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
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user