-1

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.

  • there is no need to "reset of flush", the switch does not cache or store anything - you must only change the variable `directionToBush` - as long as its value is the same, the same case will be executed – user85421 Jan 17 '19 at 19:16
  • There are no `break` statements within some `case` blocks. Is this intended? If an exception is not thrown or there is no `return` statement, you can expect execution to flow from one `case` block into the next block even if that block was non-matching for your case. – scottb Jan 17 '19 at 19:32
  • It seems like your `while` is just an infinite loop, with no body. There is no `do` statement to make it a `do-while`. – Snowy_1803 Jan 17 '19 at 20:07
  • is the variable static? That the only reason that I can think of a variable holding its value between function calls. – caleb baker Jan 17 '19 at 21:25

2 Answers2

0
switch (directionToBush) {

directionToBush is 0?

where is it changed? Not in your snippet! If it is not changed it stays 0 in all loop iterations.

PdM
  • 74
  • 1
  • 7
  • I made a loop to search every direction and store the direction and distance of the fox and bushes: – BravoFour Jan 18 '19 at 02:08
  • for (int i = Model.MIN_DIRECTION; i <= Model.MAX_DIRECTION; i++) { if (look(i) == Model.FOX) { haveSeenFox = true; directionToFox = i; distanceToFox = distance(i); } if (look(i) == Model.BUSH) { haveSeenBush = true; directionToBush = i; distanceToBush = distance(i); } } – BravoFour Jan 18 '19 at 02:10
0

It would help if you can show where the switch case variable 'directionToBush' is being initialized and how it is being changed in the switch case. As long as you keep changing this variable basing on the cases, you should be fine. There no need to reset anything. Good luck!

L Dorado
  • 51
  • 3
  • I made a loop to search every direction and store the direction and distance of the fox and bushes: – BravoFour Jan 18 '19 at 03:09
  • for (int i = Model.MIN_DIRECTION; i <= Model.MAX_DIRECTION; i++) { if (look(i) == Model.FOX) { haveSeenFox = true; directionToFox = i; distanceToFox = distance(i); } if (look(i) == Model.BUSH) { haveSeenBush = true; directionToBush = i; distanceToBush = distance(i); } } – BravoFour Jan 18 '19 at 03:09