Switch-case runs properly the first time but continues using the same initial choice over and over again. I need it to reset before this block of code runs so it chooses a different (or same) case when needed.
This is for a school project where a fox chases a rabbit. I must code the rabbit. I have the switch-case developed so as to use it's proximity to a bush as the argument that is switched on. This determines what the rabbit will do. It works properly the first time but continues with the same case after it moves and the previous case runs again on the rabbits next turn. After some research I found that I should wrap it up in a do-while statment with some flags to exit the loop and I did but the same problem continues.
if (noMansLand == false) {
needDirection = true;
do{
switch (directionToBush) {
//N-------------------------------------------------------
case 0:
if (look(7) == Model.BUSH) {
if (distance(7) == 1) {
currentDirection = 6;
if (look(6) == Model.BUSH) {
if (distance(6) == 1) {
currentDirection = 5;
}
}
}
} else {
//WANTED DIRECTION
currentDirection = 7;
}
//FOX ESCAPE PLAN
//code...
needDirection = false;
return currentDirection;
//E-------------------------------------------------------
case 2:
//similar code...
//S--------------------------------------------------------
case 4:
//similar code...
//W--------------------------------------------------------
case 6:
//similar code...
}while(needDirection == true);
}
There is ALOT more code and will happily show whole thing for help. The game works on an grid interface where the fox and rabbit take turns "looking" and moving by one grid in 1 of 6 directions (I only show 4 cardinal in my snip of the switch code). I have my rabbit look for the nearest bush to get fox to chase it around until max turns elapses. The rabbit gets to closest bush fine and upon reaching 1 distance from bush turns "noMansLand" to false and initiates switch-case block. The first turn of this works exactly as written and rabbit moves in correct direction but on the rabbits next turn it continues with the same case as if the bush is in the same direction it was last turn before moving. I'm assuming it is storing the old direction with the switch case but I do not know how to reset or flush this data.