fix: (tail) fixed where tail index 0 is on top of the head

This commit is contained in:
DarthMooMancer
2025-08-28 20:14:35 -04:00
parent ac23c873c3
commit 157933c122
9 changed files with 86 additions and 84 deletions
+2 -1
View File
@@ -7,9 +7,10 @@ struct Fruit {
Fruit(); Fruit();
int unsigned m_row; int unsigned m_row;
int unsigned m_col; int unsigned m_col;
int unsigned m_total_eaten;
Point m_point; Point m_point;
void create_point(); void create_point();
void reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score, int &count); void reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score);
}; };
#endif // FRUIT_HPP #endif // FRUIT_HPP
+1
View File
@@ -3,6 +3,7 @@
struct Input { struct Input {
char m_current_key = ' '; char m_current_key = ' ';
char m_getch = ' ';
void get_input(int &d, bool &r); void get_input(int &d, bool &r);
}; };
+4 -22
View File
@@ -4,30 +4,12 @@
#include <vector> #include <vector>
#include "point.hpp" #include "point.hpp"
/* NOTE
* tail
* ARGS
* - direction
* - old point
* - new point
* - new direction (x and y of old point and what direction it needs to swtich to so once it gets to it changes)
* - symbol
* IMPLEMENTATION
* - predict the new point
* - save old point
* - find if previous tail has new direction
* - once tail hits point change direction
*/
/* Struct Tail
* separate the snake and tail from each other
* */
struct Head { struct Head {
Head(int default_direction, int row, int col); Head(int default_direction, int row, int col);
Point m_head; Point m_head;
Point m_previous_head;
void get_new_point(int direction); void get_new_point(int direction);
void get_previous_point();
}; };
struct Tail { struct Tail {
@@ -35,8 +17,7 @@ struct Tail {
Point m_tail_pos; Point m_tail_pos;
Point m_previous_tail_pos; Point m_previous_tail_pos;
std::vector<Point> m_move_history; std::vector<Point> m_move_history;
// void set_point(Point &d); void get_previous_point();
void get_new_point();
}; };
struct Body { struct Body {
@@ -44,6 +25,7 @@ struct Body {
void clear(std::array<std::array<char, COL>, ROW> &arr); void clear(std::array<std::array<char, COL>, ROW> &arr);
void draw(std::array<std::array<char, COL>, ROW> &arr); void draw(std::array<std::array<char, COL>, ROW> &arr);
void move(Head &head); void move(Head &head);
void get_new_points(Head &head);
}; };
#endif #endif
+6 -2
View File
@@ -2,17 +2,21 @@
#define WINDOW_HPP #define WINDOW_HPP
#include <array> #include <array>
#include "globals.hpp" #include "globals.hpp"
#include "fruit.hpp"
#include "snake.hpp"
struct Window { struct Window {
Window();
std::array<std::array<char, COL>, ROW> m_board; std::array<std::array<char, COL>, ROW> m_board;
int m_score; int m_score;
// Initializes board with . for each value int m_reset;
Window(); int m_previous_reset;
void draw_score(); void draw_score();
void draw_window(); void draw_window();
void clear_window(); void clear_window();
void tick(double frames_per_second); void tick(double frames_per_second);
void update_score(Fruit &fruit, Body &body);
void update_loop(int fps); void update_loop(int fps);
}; };
+2 -2
View File
@@ -6,6 +6,7 @@
Fruit::Fruit() { Fruit::Fruit() {
std::srand(std::time(0)); std::srand(std::time(0));
create_point(); create_point();
m_total_eaten = 0;
} }
void Fruit::create_point() { void Fruit::create_point() {
@@ -14,11 +15,10 @@ void Fruit::create_point() {
m_col = m_point.m_col; m_col = m_point.m_col;
} }
void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score, int &count) { void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score) {
if(m_row == row && m_col == col) { if(m_row == row && m_col == col) {
m_point.clear(arr); m_point.clear(arr);
create_point(); create_point();
score++; score++;
count++;
} }
} }
+12 -17
View File
@@ -8,30 +8,25 @@ void Input::get_input(int &d, bool &r) {
cbreak(); cbreak();
noecho(); noecho();
m_current_key = getch(); m_getch = getch();
endwin(); endwin();
switch (m_current_key) { // TODO
case 'D': // m_current_key should be the m_getch and then as long as the opposite doesnt equal m_current_key
case 'a': if(m_getch == 'a' || m_getch == 'D') {
d = -1; d = -1;
break; m_current_key = -1;
case 'C': } else if(m_getch == 'd' || m_getch == 'C') {
case 'd':
d = 1; d = 1;
break; m_current_key = 1;
case 'B': } else if(m_getch == 's' || m_getch == 'B') {
case 's':
d = 2; d = 2;
break; m_current_key = m_getch;
case 'A': } else if(m_getch == 'w' || m_getch == 'A') {
case 'w':
d = -2; d = -2;
break; m_current_key = m_getch;
case 'q': } else if(m_getch == 'q') {
r = false; r = false;
running = false; running = false;
break;
} }
} }
} }
+8 -26
View File
@@ -1,9 +1,7 @@
#include <iostream> #include <iostream>
#include <string>
#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"
@@ -18,37 +16,21 @@ int main (int argc, char *argv[]) {
Body body; Body body;
Head head { 2, 0, 0 }; Head head { 2, 0, 0 };
bool running = true; bool running = true;
std::thread input_thread(&Input::get_input, &input, std::ref(head.m_head.m_direction), std::ref(running)); std::thread input_thread(
int count = 0; &Input::get_input, &input,
std::ref(head.m_head.m_direction),
std::ref(running));
body.m_body.push_back(Tail{2, 0, 0});
body.m_body.push_back(Tail{2, 0, 0});
body.m_body.push_back(Tail{2, 0, 0});
body.m_body.push_back(Tail{2, 0, 0});
body.m_body.push_back(Tail{2, 0, 0});
body.m_body.push_back(Tail{2, 0, 0});
while (running) { while (running) {
for (int i = 0; i < body.m_body.size(); i++) { window.update_score(fruit, body);
if(i == 0) { body.get_new_points(head);
body.m_body.at(i).m_previous_tail_pos.set_new_point(head.m_head.m_row, head.m_head.m_col, head.m_head.m_direction); head.get_previous_point();
} else { fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score);
body.m_body.at(i).m_previous_tail_pos.set_new_point(body.m_body.at(i - 1).m_tail_pos.m_row, body.m_body.at(i - 1).m_tail_pos.m_col, body.m_body.at(i - 1).m_tail_pos.m_direction);
}
}
for (int i = 0; i < body.m_body.size(); i++) {
body.m_body.at(i).get_new_point();
}
fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score, count);
window.m_board[head.m_head.m_row][head.m_head.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.m_board[fruit.m_row][fruit.m_col] = 'X';
body.draw(window.m_board); body.draw(window.m_board);
window.draw_score(); window.draw_score();
// for(int i = 0; i < body.m_body.size(); i++) {
// std::cout << "Index: " + std::to_string(i) + " " + body.m_body.at(i).m_tail_pos.to_string() << std::endl;
// }
// std::cout << "Head: " + head.m_head.to_string() << std::endl;
window.update_loop(FPS); window.update_loop(FPS);
body.clear(window.m_board); body.clear(window.m_board);
head.m_head.clear(window.m_board); head.m_head.clear(window.m_board);
+27 -1
View File
@@ -3,6 +3,7 @@
Head::Head(int default_direction, int row, int col) { Head::Head(int default_direction, int row, int col) {
m_head = Point { row, col, default_direction }; m_head = Point { row, col, default_direction };
m_previous_head.set_new_point(m_previous_head.m_row = 0, m_previous_head.m_col = 0, m_previous_head.m_direction = 2);
} }
void Head::get_new_point(int direction) { void Head::get_new_point(int direction) {
@@ -23,11 +24,17 @@ void Head::get_new_point(int direction) {
} }
} }
void Head::get_previous_point() {
m_previous_head.m_row = m_head.m_row;
m_previous_head.m_col = m_head.m_col;
m_previous_head.m_direction = m_head.m_direction;
}
Tail::Tail(int follow_direction, int row, int col) { Tail::Tail(int follow_direction, int row, int col) {
m_tail_pos = Point { row, col, follow_direction }; m_tail_pos = Point { row, col, follow_direction };
} }
void Tail::get_new_point() { void Tail::get_previous_point() {
m_tail_pos.m_row = m_previous_tail_pos.m_row; m_tail_pos.m_row = m_previous_tail_pos.m_row;
m_tail_pos.m_col = m_previous_tail_pos.m_col; m_tail_pos.m_col = m_previous_tail_pos.m_col;
m_tail_pos.m_direction = m_previous_tail_pos.m_direction; m_tail_pos.m_direction = m_previous_tail_pos.m_direction;
@@ -52,3 +59,22 @@ void Body::move(Head &head) {
} }
} }
} }
void Body::get_new_points(Head &head) {
for (int i = 0; i < m_body.size(); i++) {
if(i == 0) {
m_body.at(i).m_previous_tail_pos.set_new_point(
head.m_previous_head.m_row,
head.m_previous_head.m_col,
head.m_previous_head.m_direction);
} else {
m_body.at(i).m_previous_tail_pos.set_new_point(
m_body.at(i - 1).m_tail_pos.m_row,
m_body.at(i - 1).m_tail_pos.m_col,
m_body.at(i - 1).m_tail_pos.m_direction);
}
}
for (int i = 0; i < m_body.size(); i++) {
m_body.at(i).get_previous_point();
}
}
+11
View File
@@ -8,6 +8,8 @@ Window::Window() {
row.fill('.'); row.fill('.');
} }
m_score = 0; m_score = 0;
m_reset = 0;
m_previous_reset = 0;
} }
void Window::draw_score() { void Window::draw_score() {
@@ -23,6 +25,15 @@ void Window::draw_window() {
std::cout << std::flush; std::cout << std::flush;
} }
void Window::update_score(Fruit &fruit, Body &body) {
if(m_score % 1 == 0 && m_score >= 1) {
fruit.m_total_eaten++;
m_score = 0;
body.m_body.push_back(Tail{2, 0, 0});
}
}
void Window::clear_window() { void Window::clear_window() {
std::cout<<"\033[H" << std::flush; std::cout<<"\033[H" << std::flush;
} }