diff --git a/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx b/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx index d3d1fc7..c36907c 100644 Binary files a/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx and b/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx differ diff --git a/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx b/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx index 1384c2f..cd3f639 100644 Binary files a/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx and b/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx differ diff --git a/.cache/clangd/index/point.hpp.1A777AC8437A8313.idx b/.cache/clangd/index/point.hpp.1A777AC8437A8313.idx index c570bed..1cf8a3c 100644 Binary files a/.cache/clangd/index/point.hpp.1A777AC8437A8313.idx and b/.cache/clangd/index/point.hpp.1A777AC8437A8313.idx differ diff --git a/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx b/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx index 389ad64..73c4065 100644 Binary files a/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx and b/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx differ diff --git a/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx b/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx index c94449e..87db0d1 100644 Binary files a/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx and b/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx differ diff --git a/.gitignore b/.gitignore index 567609b..a4fb4fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +.cache/ diff --git a/include/globals.hpp b/include/globals.hpp index ae1c778..0963164 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -1,7 +1,9 @@ #ifndef GLOBALS_HPP #define GLOBALS_HPP -constexpr unsigned int ROW = 10; -constexpr unsigned int COL = 10; +#include + +constexpr uint16_t ROW = 10; +constexpr uint16_t COL = 10; #endif // GLOBALS_HPP diff --git a/include/point.hpp b/include/point.hpp index 8a1d436..c7d0689 100644 --- a/include/point.hpp +++ b/include/point.hpp @@ -12,7 +12,7 @@ struct Point { std::string to_string() const; void clear(std::array, ROW> &arr) const; - std::pair get_point(int direction, int row, int col); + std::pair get_new_point(int direction, int row, int col); }; #endif // POINT_HPP diff --git a/include/snake.hpp b/include/snake.hpp index dc8feda..d326b5c 100644 --- a/include/snake.hpp +++ b/include/snake.hpp @@ -7,6 +7,7 @@ struct Snake { Snake(int default_direction); void create_tail(int row, int col); + std::pair get_new_point(int tail_index, int direction, int row, int col); void update_tails_count(); std::vector m_tails; diff --git a/src/main.cpp b/src/main.cpp index a74fb9f..49cc149 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "point.hpp" #include "fruit.hpp" #include "snake.hpp" @@ -9,66 +10,66 @@ #include "globals.hpp" #include "input.hpp" -int main() { +int main (int argc, char *argv[]) { Snake snake {1}; Window window; Input input; Fruit fruit; int x = 5; - int y = 5; - for(int i = 0; i <= x; i++) { - for(int a = 0; a <= y; a++) { - snake.create_tail(i, a); + for(int i = 0; i < x; i++) { + snake.create_tail(i, 0); + } + + int row = 0, col = 0; + int count = 0; + while(count < 2) { + count++; + for(int i = 0; i < snake.m_tails.size(); i++) { + std::tie(row, col) = snake.get_new_point(i, 1, row, col); + std::cout << snake.m_tails.at(i).to_string() << std::endl; } } - for(int i = 0; i < snake.m_tails.size(); i++) { - std::cout << &snake.m_tails.at(i) << std::endl; - } - std::cout << snake.m_tails.size() << std::endl; - - - // int score = 0; - // int row = 0, col = 0; - // int fps = 5; - // bool running = true; - // - // Point previous {row,col}; // Creating initial snake head position - // fruit.create_point(); // Creating initial fruit object - // std::array, ROW> &board = window.m_board; - // std::thread input_thread(&Input::get_input, &input); - // while(running) { - // switch (input.m_current_key) { - // case 's': - // snake.m_direction = 1; - // break; - // case 'w': - // snake.m_direction = 2; - // break; - // case 'a': - // snake.m_direction = 3; - // break; - // case 'd': - // snake.m_direction = 4; - // break; - // case 'q': - // running = false; - // break; - // } - // - // fruit.reset(board, row, col, score); - // std::tie(row, col) = previous.get_point(snake.m_direction, row, col); - // - // board[row][col] = '*'; - // board[fruit.m_row][fruit.m_col] = '*'; - // std::cout << "Score: " << score << std::endl; - // previous.clear(board); // Clear old previous point - // previous = Point {row, col}; // Set new previous point - // window.update_loop(fps); // Update loop to clear board, tick and redisplay board - // } - // - // input_thread.join(); + // int score = 0; + // int fps = 5; + // bool running = true; + // + // Point previous {row,col}; // Creating initial snake head position + // fruit.create_point(); // Creating initial fruit object + // std::array, ROW> &board = window.m_board; + // std::thread input_thread(&Input::get_input, &input); + // while(running) { + // switch (input.m_current_key) { + // case 's': + // snake.m_direction = 1; + // break; + // case 'w': + // snake.m_direction = 2; + // break; + // case 'a': + // snake.m_direction = 3; + // break; + // case 'd': + // snake.m_direction = 4; + // break; + // case 'q': + // running = false; + // break; + // } + // + // fruit.reset(board, row, col, score); + // std::tie(row, col) = previous.get_point(snake.m_direction, row, col); + // + // board[row][col] = '*'; + // board[fruit.m_row][fruit.m_col] = '*'; + // std::cout << "Score: " << score << std::endl; + // previous.clear(board); // Clear old previous point + // previous = Point {row, col}; // Set new previous point + // window.update_loop(fps); // Update loop to clear board, tick and redisplay board + // } + // + // input_thread.join(); return 0; } diff --git a/src/point.cpp b/src/point.cpp index dc3113b..afc1fbc 100644 --- a/src/point.cpp +++ b/src/point.cpp @@ -9,10 +9,10 @@ void Point::clear(std::array, ROW> &arr) const { arr[m_row][m_col] = '.'; } -std::pair Point::get_point(int direction, int row, int col) { +std::pair Point::get_new_point(int direction, int row, int col) { switch (direction) { case 1: - row ++; + row++; if (row >= ROW) row = 0; break; case 2: diff --git a/src/snake.cpp b/src/snake.cpp index 4fc7ebc..f3e6c09 100644 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -9,6 +9,24 @@ void Snake::create_tail(int row, int col) { m_tails.push_back(Point {row, col}); } +std::pair Snake::get_new_point(int tail_index, int direction, int row, int col) { + m_tails.at(tail_index) = Point {row, col}; + if (direction == 1) { + if (row >= ROW - 1) row = 1; + else row++; + } else if (direction == 2) { + if (row <= 0) row = ROW - 1; + else row--; + } else if (direction == 3) { + if (col <= 0) col = COL - 1; + else col--; + } else if (direction == 4) { + if (col >= COL - 1) col = 1; + else col++; + } + return { row, col }; +} + /* snake head for instance is (3, 0) * the direct body to the head when going down would be (snake_head_x, snake_head_y - 1) * the next and so on would be (previous_body_segment_x - 1, previous_body_segment_y - 1)