fix(snake.cpp .hpp): first step to extracting tail functions and head movement to just head and tail separately

This commit is contained in:
DarthMooMancer
2025-08-23 00:33:48 -04:00
parent bb1040fff5
commit 667d7219bf
5 changed files with 72 additions and 94 deletions
+1 -1
View File
@@ -2,6 +2,7 @@
#define POINT_HPP #define POINT_HPP
#include <array> #include <array>
#include <string>
#include "globals.hpp" #include "globals.hpp"
struct Point { struct Point {
@@ -12,7 +13,6 @@ 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;
void get_new_point(int direction);
}; };
#endif // POINT_HPP #endif // POINT_HPP
+8 -6
View File
@@ -23,19 +23,21 @@
* separate the snake and tail from each other * separate the snake and tail from each other
* */ * */
struct Snake { struct Head {
Snake(int default_direction, int row, int col); Head(int default_direction, int row, int col);
Point m_head; Point m_head;
std::vector<Point> m_tails; std::vector<Point> m_tails;
// 1: Down, 2: Up, 3: Left, 4: Right // -1: Left, 1: Right, 2: Down, -2: Up
int m_direction; int m_direction;
// Adds a new tail to m_tails at { row, col } // Adds a new tail to m_tails at { row, col }
void create_tail(int row, int col); void create_tail(int row, int col);
void get_new_head_point(int tail_index, int direction); void get_new_head_point(int tail_index, int direction);
void get_new_point(int direction);
}; };
// struct Tail { struct Tail {
// Point m_tail_pos;
// }
};
#endif #endif
+15 -26
View File
@@ -10,7 +10,7 @@
#include "input.hpp" #include "input.hpp"
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
Snake snake { 1, 0, 0 }; Head head { 2, 0, 0 };
Window window; Window window;
Input input; Input input;
Fruit fruit; Fruit fruit;
@@ -18,45 +18,34 @@ int main (int argc, char *argv[]) {
std::thread input_thread(&Input::get_input, &input); std::thread input_thread(&Input::get_input, &input);
bool running = true; bool running = true;
int count = 0; int count = 0;
/* NOTE
* if 3 or 4 then "-"
* if 1 or 2 then "|"
* if "corner" then "~"
*/
while (running) { while (running) {
switch (input.m_current_key) { switch (input.m_current_key) {
case 's':
snake.m_direction = 1;
break;
case 'w':
snake.m_direction = 2;
break;
case 'a': case 'a':
snake.m_direction = 3; head.m_direction = -1;
break; break;
case 'd': case 'd':
snake.m_direction = 4; head.m_direction = 1;
break;
case 's':
head.m_direction = 2;
break;
case 'w':
head.m_direction = -2;
break; break;
case 'q': case 'q':
running = false; running = false;
break; break;
} }
fruit.reset(window.m_board, snake.m_head.m_row, snake.m_head.m_col, window.m_score, count); fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score, count);
/* TODO window.m_board[head.m_head.m_row][head.m_head.m_col] = '*';
* update tail positions window.m_board[fruit.m_row][fruit.m_col] = 'X';
* draw positions
* clear first tail
* repeat
* */
window.m_board[snake.m_head.m_row][snake.m_head.m_col] = '*';
window.m_board[fruit.m_row][fruit.m_col] = '*';
window.draw_score(); window.draw_score();
window.update_loop(FPS); window.update_loop(FPS);
snake.m_head.clear(window.m_board); head.m_head.clear(window.m_board);
snake.m_head.get_new_point(snake.m_direction); head.get_new_point(head.m_direction);
} }
input_thread.join(); input_thread.join();
-23
View File
@@ -8,26 +8,3 @@ std::string Point::to_string() const {
void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const { void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
arr[m_row][m_col] = '.'; arr[m_row][m_col] = '.';
} }
void Point::get_new_point(int direction) {
if (direction == 1) {
if (m_row >= ROW - 1) m_row = 0;
else m_row++;
} else if (direction == 2) {
if (m_row <= 0) m_row = ROW - 1;
else m_row--;
} else if (direction == 3) {
if (m_col <= 0) m_col = COL - 1;
else m_col--;
} else if (direction == 4) {
if (m_col >= COL - 1) m_col = 0;
else m_col++;
} else {
return;
}
}
/* Create save_point to take old_direction, new_direction, row and col
* Once a tail hits the { row, col }, change old_direction to new_direction
* Tail has own point to manage, game has array of points to manage tail and mantain coords with direction
* */
+42 -32
View File
@@ -1,42 +1,52 @@
#include "snake.hpp" #include "snake.hpp"
Snake::Snake(int default_direction, int row, int col) { Head::Head(int default_direction, int row, int col) {
m_direction = default_direction; m_direction = default_direction;
m_head = Point { row, col }; m_head = Point { row, col };
} }
void Snake::create_tail(int row, int col) { void Head::get_new_point(int direction) {
// if(row >= ROW - 1) { if (direction == -1) {
// row = 0; if (m_head.m_col <= 0) m_head.m_col = COL - 1;
// m_tails.push_back(Point {row, col}); else m_head.m_col--;
// } } else if (direction == 1) {
// if(col >= COL - 1) { if (m_head.m_col >= COL - 1) m_head.m_col = 0;
// col = 0; else m_head.m_col++;
// m_tails.push_back(Point {row, col}); } else if (direction == -2) {
// } if (m_head.m_row <= 0) m_head.m_row = ROW - 1;
m_tails.push_back(Point {row, col}); else m_head.m_row--;
}
void Snake::get_new_head_point(int tail_index, int direction) {
if (direction == 1) {
if (m_tails.at(tail_index).m_row >= ROW - 1) m_tails.at(tail_index).m_row = 0;
else ++m_tails.at(tail_index).m_row;
} else if (direction == 2) { } else if (direction == 2) {
if (m_tails.at(tail_index).m_row <= 0) m_tails.at(tail_index).m_row = ROW - 1; if (m_head.m_row >= ROW - 1) m_head.m_row = 0;
else --m_tails.at(tail_index).m_row; else m_head.m_row++;
} else if (direction == 3) { } else {
if (m_tails.at(tail_index).m_col <= 0) m_tails.at(tail_index).m_col = COL - 1; return;
else --m_tails.at(tail_index).m_row;
} else if (direction == 4) {
if (m_tails.at(tail_index).m_col >= COL - 1) m_tails.at(tail_index).m_col = 0;
else ++m_tails.at(tail_index).m_row;
} }
} }
/* snake head for instance is (3, 0) // void Snake::create_tail(int row, int col) {
* the direct body to the head when going down would be (snake_head_x, snake_head_y - 1) // // if(row >= ROW - 1) {
* the next and so on would be (previous_body_segment_x - 1, previous_body_segment_y - 1) // // row = 0;
* so save the head and body positions as previous and then do the math until the segment hits a point at direction changes. // // m_tails.push_back(Point {row, col});
* so if I change direction at (0, 0) from north to west then at (0, 0) there is a postion change saved and once a body hits that // // }
* switch direction // // if(col >= COL - 1) {
*/ // // col = 0;
// // m_tails.push_back(Point {row, col});
// // }
// m_tails.push_back(Point {row, col});
// }
//
// void Snake::get_new_tail_point(int tail_index, int direction) {
// if (direction == 1) {
// if (m_tails.at(tail_index).m_col >= COL - 1) m_tails.at(tail_index).m_col = 0;
// else ++m_tails.at(tail_index).m_row;
// } else if (direction == -1) {
// if (m_tails.at(tail_index).m_col <= 0) m_tails.at(tail_index).m_col = COL - 1;
// else --m_tails.at(tail_index).m_row;
// } else if (direction == -2) {
// if (m_tails.at(tail_index).m_row <= 0) m_tails.at(tail_index).m_row = ROW - 1;
// else --m_tails.at(tail_index).m_row;
// } else if (direction == 2) {
// if (m_tails.at(tail_index).m_row >= ROW - 1) m_tails.at(tail_index).m_row = 0;
// else ++m_tails.at(tail_index).m_row;
// }
// }