using memmove to update snake

This commit is contained in:
DarthMooMancer
2025-10-09 11:51:21 -04:00
parent baf2e21358
commit 6c6605fcb5
4 changed files with 41 additions and 52 deletions
+7 -7
View File
@@ -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;