diff --git a/CMakeLists.txt b/CMakeLists.txt index b737180..827b7ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,6 @@ set(SOURCES src/element.cpp src/input.cpp src/window.cpp - src/point.cpp - ) add_executable(snake ${SOURCES}) diff --git a/compile_commands.json b/compile_commands.json deleted file mode 120000 index 25eb4b2..0000000 --- a/compile_commands.json +++ /dev/null @@ -1 +0,0 @@ -build/compile_commands.json \ No newline at end of file diff --git a/include/element.hpp b/include/element.hpp index 7f727d8..5beda9a 100644 --- a/include/element.hpp +++ b/include/element.hpp @@ -2,7 +2,18 @@ #define ELEMENT_HPP #include -#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 { Head(); @@ -29,7 +40,7 @@ struct Body { struct Fruit { Fruit(); Point m_point; - void get_new_pos(std::array, ROW> &board, Head &head, int &score); + void get_new_pos(char (&board)[ROW][COL], Head &head, int &score); }; #endif // ELEMENT_HPP diff --git a/include/globals.hpp b/include/globals.hpp index 66b8581..c7984cb 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -1,7 +1,6 @@ #ifndef GLOBALS_HPP #define GLOBALS_HPP - -constexpr unsigned int ROW = 11; -constexpr unsigned int COL = 11; +#define ROW 10 +#define COL 10 #endif // GLOBALS_HPP diff --git a/include/point.hpp b/include/point.hpp deleted file mode 100644 index 50d8328..0000000 --- a/include/point.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef POINT_HPP -#define POINT_HPP - -#include -#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, ROW> &board) const; - void set_new_point(int row, int col, int dir); -}; - -#endif // POINT_HPP diff --git a/include/window.hpp b/include/window.hpp index 41b9457..deed272 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -1,12 +1,11 @@ #ifndef WINDOW_HPP #define WINDOW_HPP -#include + #include "globals.hpp" #include "element.hpp" struct Window { - Window(); - std::array, ROW> m_board; + char m_board[ROW][COL] = {}; void update_display(Fruit &fruit, Head &head, Body &body); }; diff --git a/src/element.cpp b/src/element.cpp index c18e90c..ba96e55 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -2,12 +2,18 @@ #include #include +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, 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++; diff --git a/src/point.cpp b/src/point.cpp deleted file mode 100644 index e364d0f..0000000 --- a/src/point.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "point.hpp" - -void Point::clear(std::array, 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; -} diff --git a/src/window.cpp b/src/window.cpp index 5b39dd7..b9dd3ea 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,31 +3,24 @@ #include #include -Window::Window() { - for(std::array &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 &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); }