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
#include <array>
#include <string>
#include "globals.hpp"
struct Point {
@@ -12,7 +13,6 @@ struct Point {
std::string to_string() const;
void clear(std::array<std::array<char, COL>, ROW> &arr) const;
void get_new_point(int direction);
};
#endif // POINT_HPP
+8 -6
View File
@@ -23,19 +23,21 @@
* separate the snake and tail from each other
* */
struct Snake {
Snake(int default_direction, int row, int col);
struct Head {
Head(int default_direction, int row, int col);
Point m_head;
std::vector<Point> m_tails;
// 1: Down, 2: Up, 3: Left, 4: Right
// -1: Left, 1: Right, 2: Down, -2: Up
int m_direction;
// Adds a new tail to m_tails at { row, col }
void create_tail(int row, int col);
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
+15 -26
View File
@@ -10,7 +10,7 @@
#include "input.hpp"
int main (int argc, char *argv[]) {
Snake snake { 1, 0, 0 };
Head head { 2, 0, 0 };
Window window;
Input input;
Fruit fruit;
@@ -18,45 +18,34 @@ int main (int argc, char *argv[]) {
std::thread input_thread(&Input::get_input, &input);
bool running = true;
int count = 0;
/* NOTE
* if 3 or 4 then "-"
* if 1 or 2 then "|"
* if "corner" then "~"
*/
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;
head.m_direction = -1;
break;
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;
case 'q':
running = false;
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
* update tail positions
* 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.m_board[head.m_head.m_row][head.m_head.m_col] = '*';
window.m_board[fruit.m_row][fruit.m_col] = 'X';
window.draw_score();
window.update_loop(FPS);
snake.m_head.clear(window.m_board);
snake.m_head.get_new_point(snake.m_direction);
head.m_head.clear(window.m_board);
head.get_new_point(head.m_direction);
}
input_thread.join();
+2 -25
View File
@@ -2,32 +2,9 @@
#include "point.hpp"
std::string Point::to_string() const {
return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + ")";
return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + ")";
}
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
* */
+46 -36
View File
@@ -1,42 +1,52 @@
#include "snake.hpp"
Snake::Snake(int default_direction, int row, int col) {
m_direction = default_direction;
m_head = Point { row, col };
Head::Head(int default_direction, int row, int col) {
m_direction = default_direction;
m_head = Point { row, col };
}
void Snake::create_tail(int row, int col) {
// if(row >= ROW - 1) {
// row = 0;
// m_tails.push_back(Point {row, col});
// }
// if(col >= COL - 1) {
// col = 0;
// m_tails.push_back(Point {row, col});
// }
m_tails.push_back(Point {row, col});
void Head::get_new_point(int direction) {
if (direction == -1) {
if (m_head.m_col <= 0) m_head.m_col = COL - 1;
else m_head.m_col--;
} else if (direction == 1) {
if (m_head.m_col >= COL - 1) m_head.m_col = 0;
else m_head.m_col++;
} else if (direction == -2) {
if (m_head.m_row <= 0) m_head.m_row = ROW - 1;
else m_head.m_row--;
} else if (direction == 2) {
if (m_head.m_row >= ROW - 1) m_head.m_row = 0;
else m_head.m_row++;
} else {
return;
}
}
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) {
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 == 3) {
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 == 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)
* 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)
* so save the head and body positions as previous and then do the math until the segment hits a point at direction changes.
* 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
*/
// void Snake::create_tail(int row, int col) {
// // if(row >= ROW - 1) {
// // row = 0;
// // m_tails.push_back(Point {row, col});
// // }
// // 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;
// }
// }