diff --git a/include/element.hpp b/include/element.hpp index f5b81bd..08b0911 100644 --- a/include/element.hpp +++ b/include/element.hpp @@ -3,20 +3,28 @@ #include "globals.hpp" +enum Direction { + NONE = 0, + UP = -2, + DOWN = 2, + LEFT = -1, + RIGHT = 1 +}; + struct Point { int m_row; int m_col; char m_symbol; - int m_direction; + enum Direction m_dir; Point() : m_row(-1), m_col(-1), m_symbol('@') {} Point(int x, int y, char s) : m_row(x), m_col(y), m_symbol(s) {} - Point(int x, int y, char s, int d) : m_row(x), m_col(y), m_symbol(s), m_direction(d) {} - void to_string(); void set_new_point(int row, int col); }; +int get_score(); void determine_index_zero_pos(Point* (*board)[COL], Point** segments); void move_tail_indicies(Point** segments); void on_fruit_eaten(Point* (*board)[COL], Point** segments); +bool segment_overlap(Point** segments); #endif // ELEMENT_HPP diff --git a/include/globals.hpp b/include/globals.hpp index fee3fb3..829ae86 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -1,5 +1,5 @@ #ifndef GLOBALS_HPP #define GLOBALS_HPP -#define ROW 10 -#define COL 10 +#define ROW 11 +#define COL 11 #endif // GLOBALS_HPP diff --git a/include/input.hpp b/include/input.hpp index 213dab1..da39ba5 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -1,10 +1,11 @@ #ifndef INPUT_HPP #define INPUT_HPP +#include "element.hpp" struct Input { - int m_current_key = 0; + enum Direction m_current_key = NONE; int m_getch = 0; - void get_input(int &d, bool &r); + void get_input(enum Direction &dir, bool &r); }; #endif // INPUT_HPP diff --git a/src/element.cpp b/src/element.cpp index 0a6ce88..9114fba 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -1,6 +1,11 @@ #include "element.hpp" #include #include +int _score = 0; + +int get_score() { + return _score; +} void Point::set_new_point(int row, int col) { m_row = row; @@ -16,16 +21,16 @@ void determine_index_zero_pos(Point* (*board)[COL], Point** segments) { segments[1]->set_new_point(segments[0]->m_row, segments[0]->m_col); } - if (segments[0]->m_direction == -1) { + if (segments[0]->m_dir == LEFT) { if (new_point.m_col <= 0) new_point.m_col = COL - 1; else new_point.m_col--; - } else if (segments[0]->m_direction == 1) { + } else if (segments[0]->m_dir == RIGHT) { if (new_point.m_col >= COL - 1) new_point.m_col = 0; else new_point.m_col++; - } else if (segments[0]->m_direction == -2) { + } else if (segments[0]->m_dir == UP) { if (new_point.m_row <= 0) new_point.m_row = ROW - 1; else new_point.m_row--; - } else if (segments[0]->m_direction == 2) { + } else if (segments[0]->m_dir == DOWN) { if (new_point.m_row >= ROW - 1) new_point.m_row = 0; else new_point.m_row++; } @@ -50,13 +55,13 @@ void move_tail_indicies(Point** segments) { } } -void _append_tail(Point *(*board)[10], Point** segments) { +void _append_tail(Point *(*board)[COL], Point** segments) { for(int i = (ROW * COL) - 1; i >= 3; i--) { segments[i] = segments[i - 1]; } Point* old_tail = segments[4]; if(old_tail != nullptr) { - segments[3] = new Point { old_tail->m_row, old_tail->m_col, '@', -1 }; + segments[3] = new Point { old_tail->m_row, old_tail->m_col, '@' }; } else { segments[3] = new Point {}; } @@ -71,5 +76,16 @@ void on_fruit_eaten(Point* (*board)[COL], Point** segments) { } while (board[row][col] != nullptr); segments[2]->set_new_point(row, col); _append_tail(board, segments); + _score++; } } + +bool segment_overlap(Point** segments) { + for(int i = (ROW * COL) - 1; i > 3; i--) { + if(segments[i] == nullptr) continue; + if(segments[i]->m_row == segments[0]->m_row && segments[i]->m_col == segments[0]->m_col) { + return true; + } + } + return false; +} diff --git a/src/input.cpp b/src/input.cpp index cfea5c7..e9a27d2 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,7 +1,7 @@ #include "input.hpp" #include -void Input::get_input(int &direction, bool &running) { +void Input::get_input(enum Direction &dir, bool &running) { while(running) { initscr(); cbreak(); @@ -11,16 +11,16 @@ void Input::get_input(int &direction, bool &running) { endwin(); switch (m_getch) { case 65: // Up arrow - if(m_current_key != 2) direction = m_current_key = -2; + if(m_current_key != DOWN) dir = m_current_key = UP; break; case 66: // Down arrow - if(m_current_key != -2) direction = m_current_key = 2; + if(m_current_key != UP) dir = m_current_key = DOWN; break; case 67: // Right arrow - if(m_current_key != -1) direction = m_current_key = 1; + if(m_current_key != LEFT) dir = m_current_key = RIGHT; break; case 68: // Left arrow - if(m_current_key != 1) direction = m_current_key = -1; + if(m_current_key != RIGHT) dir = m_current_key = LEFT; break; case 113: running = false; diff --git a/src/main.cpp b/src/main.cpp index b7238a6..de8c0d2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,26 +11,29 @@ int main () { Input input; bool running = true; - Point* segments[ROW * COL] = {}; - segments[0] = new Point { 5, 5, '*', 1 }; // Head - segments[2] = new Point { (rand() % ROW), (rand() % COL), 'o', 0 }; // Fruit + Point* segments[(ROW * COL) + 3] = {}; // 2 for head, 1 for fruit, rest for tail + segments[0] = new Point { 5, 5, '*' }; // Head + segments[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit std::thread input_thread( &Input::get_input, &input, - std::ref(segments[0]->m_direction), + std::ref(segments[0]->m_dir), std::ref(running)); while (running) { on_fruit_eaten(window.m_board, segments); determine_index_zero_pos(window.m_board, segments); move_tail_indicies(segments); - // if(body.m_body.size() >= (ROW * COL)) { - // std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl; - // running = false; - // } else if(body.check_collision(head)) { - // std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl; - // running = false; - // } + + for(int i = (ROW * COL) - 1; i > 3; i--) { + if(segments[i] == nullptr) break; + std::cout << "You win! Score: " << get_score() << std::endl; + running = false; + } + if(segment_overlap(segments)) { + std::cout << "You died! Score: " << get_score() << std::endl; + running = false; + } window.clear_display(); window.update_display(segments); window.draw_display(200); diff --git a/src/window.cpp b/src/window.cpp index 741dc71..3f5c181 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -9,7 +9,6 @@ void Window::clear_display() { m_board[i][j] = nullptr; } } - } void Window::draw_display(int milliseconds) {