diff --git a/include/fruit.hpp b/include/fruit.hpp index 64aa5a6..7604313 100644 --- a/include/fruit.hpp +++ b/include/fruit.hpp @@ -7,9 +7,10 @@ struct Fruit { Fruit(); int unsigned m_row; int unsigned m_col; + int unsigned m_total_eaten; Point m_point; void create_point(); - void reset(std::array, ROW> &arr, int row, int col, int &score, int &count); + void reset(std::array, ROW> &arr, int row, int col, int &score); }; #endif // FRUIT_HPP diff --git a/include/input.hpp b/include/input.hpp index 11d6a88..8846585 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -3,6 +3,7 @@ struct Input { char m_current_key = ' '; + char m_getch = ' '; void get_input(int &d, bool &r); }; diff --git a/include/snake.hpp b/include/snake.hpp index de4aff1..be3d70a 100644 --- a/include/snake.hpp +++ b/include/snake.hpp @@ -4,30 +4,12 @@ #include #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 { Head(int default_direction, int row, int col); Point m_head; + Point m_previous_head; void get_new_point(int direction); + void get_previous_point(); }; struct Tail { @@ -35,8 +17,7 @@ struct Tail { Point m_tail_pos; Point m_previous_tail_pos; std::vector m_move_history; - // void set_point(Point &d); - void get_new_point(); + void get_previous_point(); }; struct Body { @@ -44,6 +25,7 @@ struct Body { void clear(std::array, ROW> &arr); void draw(std::array, ROW> &arr); void move(Head &head); + void get_new_points(Head &head); }; #endif diff --git a/include/window.hpp b/include/window.hpp index 139c710..708ef3b 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -2,18 +2,22 @@ #define WINDOW_HPP #include #include "globals.hpp" +#include "fruit.hpp" +#include "snake.hpp" struct Window { - std::array, ROW> m_board; - int m_score; - // Initializes board with . for each value - Window(); + Window(); + std::array, ROW> m_board; + int m_score; + int m_reset; + int m_previous_reset; - void draw_score(); - void draw_window(); - void clear_window(); - void tick(double frames_per_second); - void update_loop(int fps); + void draw_score(); + void draw_window(); + void clear_window(); + void tick(double frames_per_second); + void update_score(Fruit &fruit, Body &body); + void update_loop(int fps); }; #endif // WINDOW_HPP diff --git a/src/fruit.cpp b/src/fruit.cpp index ec1a1d1..9e218fc 100644 --- a/src/fruit.cpp +++ b/src/fruit.cpp @@ -6,6 +6,7 @@ Fruit::Fruit() { std::srand(std::time(0)); create_point(); + m_total_eaten = 0; } void Fruit::create_point() { @@ -14,11 +15,10 @@ void Fruit::create_point() { m_col = m_point.m_col; } -void Fruit::reset(std::array, ROW> &arr, int row, int col, int &score, int &count) { +void Fruit::reset(std::array, ROW> &arr, int row, int col, int &score) { if(m_row == row && m_col == col) { m_point.clear(arr); create_point(); score++; - count++; } } diff --git a/src/input.cpp b/src/input.cpp index 1cd20ad..c7b2fc0 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -8,30 +8,25 @@ void Input::get_input(int &d, bool &r) { cbreak(); noecho(); - m_current_key = getch(); + m_getch = getch(); endwin(); - switch (m_current_key) { - case 'D': - case 'a': - d = -1; - break; - case 'C': - case 'd': - d = 1; - break; - case 'B': - case 's': - d = 2; - break; - case 'A': - case 'w': - d = -2; - break; - case 'q': - r = false; - running = false; - break; - + // TODO + // m_current_key should be the m_getch and then as long as the opposite doesnt equal m_current_key + if(m_getch == 'a' || m_getch == 'D') { + d = -1; + m_current_key = -1; + } else if(m_getch == 'd' || m_getch == 'C') { + d = 1; + m_current_key = 1; + } else if(m_getch == 's' || m_getch == 'B') { + d = 2; + m_current_key = m_getch; + } else if(m_getch == 'w' || m_getch == 'A') { + d = -2; + m_current_key = m_getch; + } else if(m_getch == 'q') { + r = false; + running = false; } } } diff --git a/src/main.cpp b/src/main.cpp index 1d87685..c0e6638 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,7 @@ #include -#include #include #include #include -#include #include "point.hpp" #include "fruit.hpp" #include "snake.hpp" @@ -18,37 +16,21 @@ int main (int argc, char *argv[]) { Body body; Head head { 2, 0, 0 }; bool running = true; - std::thread input_thread(&Input::get_input, &input, std::ref(head.m_head.m_direction), std::ref(running)); - int count = 0; + std::thread input_thread( + &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) { - for (int i = 0; i < body.m_body.size(); i++) { - if(i == 0) { - 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); - } else { - 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.update_score(fruit, body); + body.get_new_points(head); + head.get_previous_point(); + fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score); window.m_board[head.m_head.m_row][head.m_head.m_col] = '*'; window.m_board[fruit.m_row][fruit.m_col] = 'X'; body.draw(window.m_board); 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); body.clear(window.m_board); head.m_head.clear(window.m_board); diff --git a/src/snake.cpp b/src/snake.cpp index f809e90..626d19e 100644 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -3,6 +3,7 @@ Head::Head(int default_direction, int row, int col) { 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) { @@ -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) { 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_col = m_previous_tail_pos.m_col; 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(); + } +} diff --git a/src/window.cpp b/src/window.cpp index 863dfe2..93fb511 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -8,6 +8,8 @@ Window::Window() { row.fill('.'); } m_score = 0; + m_reset = 0; + m_previous_reset = 0; } void Window::draw_score() { @@ -23,6 +25,15 @@ void Window::draw_window() { 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() { std::cout<<"\033[H" << std::flush; }