From 667d7219bf8902511f4e14f7941b292562aa2b97 Mon Sep 17 00:00:00 2001 From: DarthMooMancer Date: Sat, 23 Aug 2025 00:33:48 -0400 Subject: [PATCH] fix(snake.cpp .hpp): first step to extracting tail functions and head movement to just head and tail separately --- include/point.hpp | 2 +- include/snake.hpp | 14 ++++---- src/main.cpp | 41 +++++++++--------------- src/point.cpp | 27 ++-------------- src/snake.cpp | 82 ++++++++++++++++++++++++++--------------------- 5 files changed, 72 insertions(+), 94 deletions(-) diff --git a/include/point.hpp b/include/point.hpp index 6730ec1..32267d0 100644 --- a/include/point.hpp +++ b/include/point.hpp @@ -2,6 +2,7 @@ #define POINT_HPP #include +#include #include "globals.hpp" struct Point { @@ -12,7 +13,6 @@ struct Point { std::string to_string() const; void clear(std::array, ROW> &arr) const; - void get_new_point(int direction); }; #endif // POINT_HPP diff --git a/include/snake.hpp b/include/snake.hpp index ea505f7..334b8fc 100644 --- a/include/snake.hpp +++ b/include/snake.hpp @@ -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 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 diff --git a/src/main.cpp b/src/main.cpp index ed537bb..8e7c19f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); diff --git a/src/point.cpp b/src/point.cpp index d2e1c61..fb9c7ce 100644 --- a/src/point.cpp +++ b/src/point.cpp @@ -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, 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 - * */ diff --git a/src/snake.cpp b/src/snake.cpp index d5b8aa1..0911e30 100644 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -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; +// } +// }