diff --git a/include/element.hpp b/include/element.hpp index c8e140d..11e52bb 100644 --- a/include/element.hpp +++ b/include/element.hpp @@ -22,7 +22,7 @@ struct Point { }; int get_score(); -void determine_index_zero_pos(Point* (*board)[COL], Point** buffer); +void determine_index_zero_pos(Point** buffer); void move_tail_indicies(Point** buffer); void on_fruit_eaten(Point* (*board)[COL], Point** buffer); bool segment_overlap(Point** buffer); diff --git a/src/element.cpp b/src/element.cpp index 60fcae3..e927b46 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -1,8 +1,10 @@ #include "element.hpp" #include +#include #include int _score = 0; +int _snake_size = 3; int get_score() { return _score; @@ -13,9 +15,7 @@ void Point::set_new_point(int row, int col) { m_col = col; } -void determine_index_zero_pos(Point* (*board)[COL], Point** buffer) { - Point new_point = { buffer[0]->m_row, buffer[0]->m_col, '.' }; - +void determine_index_zero_pos(Point** buffer) { if(buffer[1] == nullptr) { buffer[1] = new Point { buffer[0]->m_row, buffer[0]->m_col, '.' }; } else { @@ -23,49 +23,40 @@ void determine_index_zero_pos(Point* (*board)[COL], Point** buffer) { } if (buffer[0]->m_dir == LEFT) { - if (new_point.m_col <= 0) new_point.m_col = COL - 1; - else new_point.m_col--; - } else if (buffer[0]->m_dir == RIGHT) { - if (new_point.m_col >= COL - 1) new_point.m_col = 0; - else new_point.m_col++; - } else if (buffer[0]->m_dir == UP) { - if (new_point.m_row <= 0) new_point.m_row = ROW - 1; - else new_point.m_row--; - } else if (buffer[0]->m_dir == DOWN) { - if (new_point.m_row >= ROW - 1) new_point.m_row = 0; - else new_point.m_row++; + if (buffer[0]->m_col <= 0) buffer[0]->m_col = COL - 1; + else buffer[0]->m_col--; + } + else if (buffer[0]->m_dir == RIGHT) { + if (buffer[0]->m_col >= COL - 1) buffer[0]->m_col = 0; + else buffer[0]->m_col++; + } + else if (buffer[0]->m_dir == UP) { + if (buffer[0]->m_row <= 0) buffer[0]->m_row = ROW - 1; + else buffer[0]->m_row--; + } + else if (buffer[0]->m_dir == DOWN) { + if (buffer[0]->m_row >= ROW - 1) buffer[0]->m_row = 0; + else buffer[0]->m_row++; } - buffer[0]->set_new_point(new_point.m_row, new_point.m_col); } void move_tail_indicies(Point** buffer) { - Point* previous_head = buffer[1]; + if(_snake_size <= 3) return; int new_row = buffer[1]->m_row; int new_col = buffer[1]->m_col; - Point* last_point = nullptr; - for(int i = (ROW * COL) - 1; i > 3; i--) { - if(buffer[i] == nullptr) { - last_point = buffer[i - 1]; - continue; - } - buffer[i] = buffer[i - 1]; - } + Point* last_point = buffer[_snake_size - 1]; + + memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*)); + buffer[3] = last_point; if(buffer[3] != nullptr) { buffer[3]->set_new_point(new_row, new_col); } } -void _append_tail(Point *(*board)[COL], Point** buffer) { - for(int i = (ROW * COL) - 1; i >= 3; i--) { - buffer[i] = buffer[i - 1]; - } - Point* old_tail = buffer[4]; - if(old_tail != nullptr) { - buffer[3] = new Point { old_tail->m_row, old_tail->m_col, '@' }; - } else { - buffer[3] = new Point {}; - } +void _append_tail(Point** buffer) { + memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*)); + buffer[3] = new Point { ROW, COL, '@' }; } void on_fruit_eaten(Point* (*board)[COL], Point** buffer) { @@ -76,15 +67,16 @@ void on_fruit_eaten(Point* (*board)[COL], Point** buffer) { col = (rand() % COL); } while (board[row][col] != nullptr); buffer[2]->set_new_point(row, col); - _append_tail(board, buffer); + _append_tail(buffer); _score++; + _snake_size++; } } bool segment_overlap(Point** buffer) { - for(int i = (ROW * COL) - 1; i > 3; i--) { - if(buffer[i] == nullptr) continue; - if(buffer[i]->m_row == buffer[0]->m_row && buffer[i]->m_col == buffer[0]->m_col) { + for(int i = 0; i < _snake_size; i++) { + if(buffer[i + 3] == nullptr) continue; + if(buffer[i + 3]->m_row == buffer[0]->m_row && buffer[i + 3]->m_col == buffer[0]->m_col) { return true; } } diff --git a/src/main.cpp b/src/main.cpp index b53e26a..e623a19 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,9 +9,10 @@ int main () { std::srand(std::time(0)); Window window; Input input; + const int MAX_LEN = (ROW * COL) + 3; bool running = true; - Point* buffer[(ROW * COL) + 3] = {}; // 2 for head, 1 for fruit, rest for tail + Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail buffer[0] = new Point { ROW / 2, COL / 2, '*' }; // Head buffer[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit @@ -19,16 +20,16 @@ int main () { while (running) { on_fruit_eaten(window.m_board, buffer); - determine_index_zero_pos(window.m_board, buffer); + determine_index_zero_pos(buffer); move_tail_indicies(buffer); - for(int i = (ROW * COL) - 1; i > 3; i--) { + for(int i = MAX_LEN - 1; i > 3; i--) { if(buffer[i] == nullptr) break; - std::cout << "You win! Score: " << get_score() << std::endl; + std::cout << "You filled the whole board and won! Score: " << get_score() << std::endl; running = false; } if(segment_overlap(buffer)) { - std::cout << "You died! Score: " << get_score() << std::endl; + std::cout << "You collided with yourself and died! Score: " << get_score() << std::endl; running = false; } window.clear_display(); @@ -37,8 +38,7 @@ int main () { } input_thread.join(); - for(int i = ((ROW * COL) + 3) - 1; i > 0; i--) { - if(buffer[i] == nullptr) continue; + for(int i = 0; i < MAX_LEN - 1; i++) { delete buffer[i]; } return 0; diff --git a/src/window.cpp b/src/window.cpp index 5b22737..64ac54d 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -24,11 +24,8 @@ void Window::draw_display(int milliseconds) { } void Window::update_display(Point** buffer) { - m_board[buffer[0]->m_row][buffer[0]->m_col] = buffer[0]; - m_board[buffer[2]->m_row][buffer[2]->m_col] = buffer[2]; - for(int i = 3; i < ROW * COL; i++) { - if(buffer[i] != nullptr) { - m_board[buffer[i]->m_row][buffer[i]->m_col] = buffer[i]; - } + for(int i = 0; i < ROW * COL; i++) { + if(buffer[i] == nullptr || i == 1 || buffer[i]->m_row > ROW - 1 || buffer[i]->m_col > COL - 1) continue; + m_board[buffer[i]->m_row][buffer[i]->m_col] = buffer[i]; } }