Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HELP! for loop and switch

807600Nov 20 2007 — edited Nov 24 2007
This took almost two hours to put together and has really been stressing me out. Any help is most appreciated.

Ok. Here we go. Please stay with me on this one guys i know its long, but i really need the help.

I have created an AI program that plays a Battle Ship game completely on its own. Information is being returned to a game engine from my program as the game progresses. Part of this code is a switch statement set to random that will pick randomly from four cases. Each case uses a "break;" to exit the loop, then it is checked to make sure its inside the parameters, and then is returned to the game engine.

The problem is: the same case will be returned multiple times because of the random util. I can change the program to remember the previous case selection so it wont choose the same case again, but that is beyond my knowledge ( although I am up for learning this).What I need to do is take the four cases that are being randomly chosen in the switch statement and just execute them sequentially one at a time. Each case needs to be within an already determined parameter or another case needs to be chosen. If it is in the parameters then it is returned to the game engine where it then either renders the loop containing this problem false, so that it repeats again, or true so it moves on to the rest of the code.

Remember these two examples are the same program just trying to fix the same problem two different ways.


Here is the working switch statement. I will still use this if someone can show me how to remember what case has been chosen before, check the new case against the previous chosen, then continue to the while statement.
  }
          else if (found_opp_again == false) {  // condition that needs to become true by executing the following
            Random myRandom = new Random(); 

            do {
                switch(myRandom.nextInt(4)){
                    case 1: nextFire.r = x.r+1;   // If the code is left like this i need some way to remember     
                            nextFire.c = x.c;      //the previous choices so they will not be chosen again.    
                            break;
                    case 2: nextFire.r = x.r-1;
                            nextFire.c = x.c;
                            break;
                    case 3: nextFire.r = x.r;
                            nextFire.c = x.c+1;
                            break;
                    case 4: nextFire.r = x.r;
                            nextFire.c = x.c-1;
                            break;
                }
            }while(!location_is_in_bounds(nextFire)); // checks to make sure shot is with in grid
            
                return nextFire; 
        }//end found_opp_again condition
        
        else {  
Ok this is of the for loop solution i tried. I wanted each if statement to be an integer so i could increment it allowing the for loop to continue through the four if statements. THIS IS NOT WORKING. I am trying to check for the right parameters with a do...while loop. Each if statement needs to change nextFire, then check to make sure nextFire is in the grid, and return it to game engine. If the found_opp_again loop is still false a previously chosen if statement can not be chosen again. hence the incrementation of j but its not working. j will not seem to increment.
}
        else if (found_opp_again == false) {  
 
            do{                                                
                for ( int j = 1; j <= 4; j += 1 ){ // I hoped this would increment j each time leading to the next if statement
                    if (j == 1){               //  when the project is ran is the loop gets stuck at if (j==1) 
                    nextFire.r = x.r+1;         // and repeats returning the nextFire coordinates for that first if statement. 
                    nextFire.c = x.c;
                
                }
                    if (j == 2){
                    nextFire.r = x.r;
                    nextFire.c = x.c-1;
             
                }
                    if (j == 3){
                    nextFire.r = x.r;
                    nextFire.c = x.c+1;
                
                }
                    if (j == 4){
                    nextFire.r = x.r-1;
                    nextFire.c = x.c;
                
                }     
            } // end for loop   
            
        }while(!location_is_in_bounds(nextFire)); // checks if shot is in grid
            return nextFire; // returns shot location to game engine
            
        } //end condition found_opp_again
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 22 2007
Added on Nov 20 2007
7 comments
101 views