using memmove to update snake
This commit is contained in:
+1
-1
@@ -22,7 +22,7 @@ struct Point {
|
|||||||
};
|
};
|
||||||
|
|
||||||
int get_score();
|
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 move_tail_indicies(Point** buffer);
|
||||||
void on_fruit_eaten(Point* (*board)[COL], Point** buffer);
|
void on_fruit_eaten(Point* (*board)[COL], Point** buffer);
|
||||||
bool segment_overlap(Point** buffer);
|
bool segment_overlap(Point** buffer);
|
||||||
|
|||||||
+30
-38
@@ -1,8 +1,10 @@
|
|||||||
#include "element.hpp"
|
#include "element.hpp"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
int _score = 0;
|
int _score = 0;
|
||||||
|
int _snake_size = 3;
|
||||||
|
|
||||||
int get_score() {
|
int get_score() {
|
||||||
return _score;
|
return _score;
|
||||||
@@ -13,9 +15,7 @@ void Point::set_new_point(int row, int col) {
|
|||||||
m_col = col;
|
m_col = col;
|
||||||
}
|
}
|
||||||
|
|
||||||
void determine_index_zero_pos(Point* (*board)[COL], Point** buffer) {
|
void determine_index_zero_pos(Point** buffer) {
|
||||||
Point new_point = { buffer[0]->m_row, buffer[0]->m_col, '.' };
|
|
||||||
|
|
||||||
if(buffer[1] == nullptr) {
|
if(buffer[1] == nullptr) {
|
||||||
buffer[1] = new Point { buffer[0]->m_row, buffer[0]->m_col, '.' };
|
buffer[1] = new Point { buffer[0]->m_row, buffer[0]->m_col, '.' };
|
||||||
} else {
|
} else {
|
||||||
@@ -23,49 +23,40 @@ void determine_index_zero_pos(Point* (*board)[COL], Point** buffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (buffer[0]->m_dir == LEFT) {
|
if (buffer[0]->m_dir == LEFT) {
|
||||||
if (new_point.m_col <= 0) new_point.m_col = COL - 1;
|
if (buffer[0]->m_col <= 0) buffer[0]->m_col = COL - 1;
|
||||||
else new_point.m_col--;
|
else buffer[0]->m_col--;
|
||||||
} else if (buffer[0]->m_dir == RIGHT) {
|
}
|
||||||
if (new_point.m_col >= COL - 1) new_point.m_col = 0;
|
else if (buffer[0]->m_dir == RIGHT) {
|
||||||
else new_point.m_col++;
|
if (buffer[0]->m_col >= COL - 1) buffer[0]->m_col = 0;
|
||||||
} else if (buffer[0]->m_dir == UP) {
|
else buffer[0]->m_col++;
|
||||||
if (new_point.m_row <= 0) new_point.m_row = ROW - 1;
|
}
|
||||||
else new_point.m_row--;
|
else if (buffer[0]->m_dir == UP) {
|
||||||
} else if (buffer[0]->m_dir == DOWN) {
|
if (buffer[0]->m_row <= 0) buffer[0]->m_row = ROW - 1;
|
||||||
if (new_point.m_row >= ROW - 1) new_point.m_row = 0;
|
else buffer[0]->m_row--;
|
||||||
else new_point.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) {
|
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_row = buffer[1]->m_row;
|
||||||
int new_col = buffer[1]->m_col;
|
int new_col = buffer[1]->m_col;
|
||||||
Point* last_point = nullptr;
|
Point* last_point = buffer[_snake_size - 1];
|
||||||
for(int i = (ROW * COL) - 1; i > 3; i--) {
|
|
||||||
if(buffer[i] == nullptr) {
|
memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*));
|
||||||
last_point = buffer[i - 1];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
buffer[i] = buffer[i - 1];
|
|
||||||
}
|
|
||||||
buffer[3] = last_point;
|
buffer[3] = last_point;
|
||||||
if(buffer[3] != nullptr) {
|
if(buffer[3] != nullptr) {
|
||||||
buffer[3]->set_new_point(new_row, new_col);
|
buffer[3]->set_new_point(new_row, new_col);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _append_tail(Point *(*board)[COL], Point** buffer) {
|
void _append_tail(Point** buffer) {
|
||||||
for(int i = (ROW * COL) - 1; i >= 3; i--) {
|
memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*));
|
||||||
buffer[i] = buffer[i - 1];
|
buffer[3] = new Point { ROW, COL, '@' };
|
||||||
}
|
|
||||||
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 on_fruit_eaten(Point* (*board)[COL], Point** buffer) {
|
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);
|
col = (rand() % COL);
|
||||||
} while (board[row][col] != nullptr);
|
} while (board[row][col] != nullptr);
|
||||||
buffer[2]->set_new_point(row, col);
|
buffer[2]->set_new_point(row, col);
|
||||||
_append_tail(board, buffer);
|
_append_tail(buffer);
|
||||||
_score++;
|
_score++;
|
||||||
|
_snake_size++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool segment_overlap(Point** buffer) {
|
bool segment_overlap(Point** buffer) {
|
||||||
for(int i = (ROW * COL) - 1; i > 3; i--) {
|
for(int i = 0; i < _snake_size; i++) {
|
||||||
if(buffer[i] == nullptr) continue;
|
if(buffer[i + 3] == nullptr) continue;
|
||||||
if(buffer[i]->m_row == buffer[0]->m_row && buffer[i]->m_col == buffer[0]->m_col) {
|
if(buffer[i + 3]->m_row == buffer[0]->m_row && buffer[i + 3]->m_col == buffer[0]->m_col) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-7
@@ -9,9 +9,10 @@ int main () {
|
|||||||
std::srand(std::time(0));
|
std::srand(std::time(0));
|
||||||
Window window;
|
Window window;
|
||||||
Input input;
|
Input input;
|
||||||
|
const int MAX_LEN = (ROW * COL) + 3;
|
||||||
bool running = true;
|
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[0] = new Point { ROW / 2, COL / 2, '*' }; // Head
|
||||||
buffer[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit
|
buffer[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit
|
||||||
|
|
||||||
@@ -19,16 +20,16 @@ int main () {
|
|||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
on_fruit_eaten(window.m_board, buffer);
|
on_fruit_eaten(window.m_board, buffer);
|
||||||
determine_index_zero_pos(window.m_board, buffer);
|
determine_index_zero_pos(buffer);
|
||||||
move_tail_indicies(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;
|
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;
|
running = false;
|
||||||
}
|
}
|
||||||
if(segment_overlap(buffer)) {
|
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;
|
running = false;
|
||||||
}
|
}
|
||||||
window.clear_display();
|
window.clear_display();
|
||||||
@@ -37,8 +38,7 @@ int main () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
input_thread.join();
|
input_thread.join();
|
||||||
for(int i = ((ROW * COL) + 3) - 1; i > 0; i--) {
|
for(int i = 0; i < MAX_LEN - 1; i++) {
|
||||||
if(buffer[i] == nullptr) continue;
|
|
||||||
delete buffer[i];
|
delete buffer[i];
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+2
-5
@@ -24,11 +24,8 @@ void Window::draw_display(int milliseconds) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Window::update_display(Point** buffer) {
|
void Window::update_display(Point** buffer) {
|
||||||
m_board[buffer[0]->m_row][buffer[0]->m_col] = buffer[0];
|
for(int i = 0; i < ROW * COL; i++) {
|
||||||
m_board[buffer[2]->m_row][buffer[2]->m_col] = buffer[2];
|
if(buffer[i] == nullptr || i == 1 || buffer[i]->m_row > ROW - 1 || buffer[i]->m_col > COL - 1) continue;
|
||||||
for(int i = 3; i < ROW * COL; i++) {
|
|
||||||
if(buffer[i] != nullptr) {
|
|
||||||
m_board[buffer[i]->m_row][buffer[i]->m_col] = buffer[i];
|
m_board[buffer[i]->m_row][buffer[i]->m_col] = buffer[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user