From 23bc7bb4f33ebad3cbe43d9e88c716b2cc5422c8 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 8 Jan 2016 14:54:08 +0100 Subject: [PATCH] Evitato un freeze quando un nemico era bloccato da tutte e quattro le parti. --- main.cpp | 85 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/main.cpp b/main.cpp index 404adbc..ceca6e9 100644 --- a/main.cpp +++ b/main.cpp @@ -290,49 +290,52 @@ class Enemy : public Entity //Il giocatore non è vicino else { - //Muoviti in una direzione casuale - bool moving = true; - while(moving) + if(map[x-1][y] == EMPTY || map[x+1][y] == EMPTY || map[x][y-1] == EMPTY || map[x][y+1] == EMPTY) { - int direction = rand() % 4; - switch(direction) + //Muoviti in una direzione casuale + bool moving = true; + while(moving) { - case 0: //Sinistra - if(map[x-1][y] == EMPTY) - { - map[x][y] = EMPTY; - map[x-1][y] = ENEMY; - x--; - moving = false; - } - break; - case 1: //Destra - if(map[x+1][y] == EMPTY) - { - map[x][y] = EMPTY; - map[x+1][y] = ENEMY; - x++; - moving = false; - } - break; - case 2: //Su - if(map[x][y-1] == EMPTY) - { - map[x][y] = EMPTY; - map[x][y-1] = ENEMY; - y--; - moving = false; - } - break; - case 3: //Giù - if(map[x][y+1] == EMPTY) - { - map[x][y] = EMPTY; - map[x][y+1] = ENEMY; - y++; - moving = false; - } - break; + int direction = rand() % 4; + switch(direction) + { + case 0: //Sinistra + if(map[x-1][y] == EMPTY) + { + map[x][y] = EMPTY; + map[x-1][y] = ENEMY; + x--; + moving = false; + } + break; + case 1: //Destra + if(map[x+1][y] == EMPTY) + { + map[x][y] = EMPTY; + map[x+1][y] = ENEMY; + x++; + moving = false; + } + break; + case 2: //Su + if(map[x][y-1] == EMPTY) + { + map[x][y] = EMPTY; + map[x][y-1] = ENEMY; + y--; + moving = false; + } + break; + case 3: //Giù + if(map[x][y+1] == EMPTY) + { + map[x][y] = EMPTY; + map[x][y+1] = ENEMY; + y++; + moving = false; + } + break; + } } } }