rework(snake): added tail get_new_point for managing tail movement

This commit is contained in:
DarthMooMancer committed 2025-05-22 12:23:06 -04:00
1 parent ff942631aa
commit a128048a8f
12 files changed
+38 -15

No files matched your search

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1
View File
@@ -1 +1,2 @@
build/ build/
.cache/
+4 -2
View File
@@ -1,7 +1,9 @@
#ifndef GLOBALS_HPP #ifndef GLOBALS_HPP
#define GLOBALS_HPP #define GLOBALS_HPP
constexpr unsigned int ROW = 10; #include <cstdint>
constexpr unsigned int COL = 10;
constexpr uint16_t ROW = 10;
constexpr uint16_t COL = 10;
#endif // GLOBALS_HPP #endif // GLOBALS_HPP
+1 -1
View File
@@ -12,7 +12,7 @@ struct Point {
std::string to_string() const; std::string to_string() const;
void clear(std::array<std::array<char, COL>, ROW> &arr) const; void clear(std::array<std::array<char, COL>, ROW> &arr) const;
std::pair<int, int> get_point(int direction, int row, int col); std::pair<int, int> get_new_point(int direction, int row, int col);
}; };
#endif // POINT_HPP #endif // POINT_HPP
+1
View File
@@ -7,6 +7,7 @@
struct Snake { struct Snake {
Snake(int default_direction); Snake(int default_direction);
void create_tail(int row, int col); void create_tail(int row, int col);
std::pair<int, int> get_new_point(int tail_index, int direction, int row, int col);
void update_tails_count(); void update_tails_count();
std::vector<Point> m_tails; std::vector<Point> m_tails;
+11 -10
View File
@@ -2,6 +2,7 @@
#include <thread> #include <thread>
#include <cstdlib> #include <cstdlib>
#include <unistd.h> #include <unistd.h>
#include <vector>
#include "point.hpp" #include "point.hpp"
#include "fruit.hpp" #include "fruit.hpp"
#include "snake.hpp" #include "snake.hpp"
@@ -9,28 +10,28 @@
#include "globals.hpp" #include "globals.hpp"
#include "input.hpp" #include "input.hpp"
int main() { int main (int argc, char *argv[]) {
Snake snake {1}; Snake snake {1};
Window window; Window window;
Input input; Input input;
Fruit fruit; Fruit fruit;
int x = 5; int x = 5;
int y = 5; for(int i = 0; i < x; i++) {
for(int i = 0; i <= x; i++) { snake.create_tail(i, 0);
for(int a = 0; a <= y; a++) {
snake.create_tail(i, a);
}
} }
int row = 0, col = 0;
int count = 0;
while(count < 2) {
count++;
for(int i = 0; i < snake.m_tails.size(); i++) { for(int i = 0; i < snake.m_tails.size(); i++) {
std::cout << &snake.m_tails.at(i) << std::endl; std::tie(row, col) = snake.get_new_point(i, 1, row, col);
std::cout << snake.m_tails.at(i).to_string() << std::endl;
}
} }
std::cout << snake.m_tails.size() << std::endl;
// int score = 0; // int score = 0;
// int row = 0, col = 0;
// int fps = 5; // int fps = 5;
// bool running = true; // bool running = true;
// //
+2 -2
View File
@@ -9,10 +9,10 @@ void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
arr[m_row][m_col] = '.'; arr[m_row][m_col] = '.';
} }
std::pair<int, int> Point::get_point(int direction, int row, int col) { std::pair<int, int> Point::get_new_point(int direction, int row, int col) {
switch (direction) { switch (direction) {
case 1: case 1:
row ++; row++;
if (row >= ROW) row = 0; if (row >= ROW) row = 0;
break; break;
case 2: case 2:
+18
View File
@@ -9,6 +9,24 @@ void Snake::create_tail(int row, int col) {
m_tails.push_back(Point {row, col}); m_tails.push_back(Point {row, col});
} }
std::pair<int, int> 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) /* 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 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) * the next and so on would be (previous_body_segment_x - 1, previous_body_segment_y - 1)