From 56c500a93d1d659d4b4483fce06f7f858ae4e5a8 Mon Sep 17 00:00:00 2001
From: DenisD3D
Date: Mon, 1 Apr 2024 18:49:29 +0200
Subject: [PATCH] Fix infinite loop when no more room for apple
---
src/jeu.cpp | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/src/jeu.cpp b/src/jeu.cpp
index dc48df6..c8a4aaf 100644
--- a/src/jeu.cpp
+++ b/src/jeu.cpp
@@ -50,10 +50,15 @@ bool Jeu::init() {
dirSnake = map.getInitDirection();
- std::uniform_int_distribution<> distr(0, map.getWidth() - 1);
+ std::uniform_int_distribution distr(0, map.getWidth() - 1);
+ int attempts = 0;
do {
applePos.x = distr(gen);
applePos.y = distr(gen);
+ attempts++;
+ if (attempts > 2 * map.getWidth() * map.getHeight()) {
+ return false;
+ }
} while (!posValide(applePos, APPLE_SPAWN));
return true;
@@ -82,9 +87,14 @@ void Jeu::evolue() {
if (posTest == applePos) {
// The snake eats the apple, place a new apple
std::uniform_int_distribution<> distr(0, map.getWidth() - 1);
+ int attempts = 0;
do {
applePos.x = distr(gen);
applePos.y = distr(gen);
+ attempts++;
+ if (attempts > 2 * map.getWidth() * map.getHeight()) {
+ break; // TODO: lose / win condition ?
+ }
} while (!posValide(applePos, APPLE_SPAWN));
// Don't remove the last element of the snake to make it grow
} else {
@@ -93,7 +103,7 @@ void Jeu::evolue() {
}
} else {
// Game over
- snake.clear();
+ snake.clear(); // TODO: lose condition
}
}