Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Do While loop repeat issue

    I have for the better part most of the program finished but my teacher at the last minute threw a curve ball and stated that we need the program to repeat if the user chooses to once they finish their game.

    Been at this for a few hours and I'm stuck. The loop doesn't repeat unless the tic tac toe game ends in a draw. I need the loop to repeat if the user chooses to once they win or after they lose to the computer.

    Rather than get angry, I decided to get another opinion. Looking for any information or ideas to point me in the right direction.


    Java Code:
            public static void main(String[] args)
    	{
    
     	Scanner input = new Scanner(System.in);
    
    do{
    
    	{
    	splashPortion(); //Splash page for tic tac toe game
    	pressEnter(); // Code for entering onto game board
    	}
    
    
    		topHeader(); //Game board letters and setup
    		String prompt = "Please enter your first move: ";
    		String humanMove = "";
    		String computerMove = "";
    		boolean gameIsWon = false;
    
    		for (int i = 0; i <=8; i++)
    		{
    			// Human player
    
    			humanMove = getMove(prompt);
    			updateBoard(humanMove, 1);
    			displayBoard();
    			if (isGameWon())
    			{
    				System.out.println("Player 1 Wins!!!");
    				gameIsWon = true;
    				return;
    			}
    
    			// Computer player
    			if (i < 8)
    			{
    				computerMove = getComputerMove();
    				System.out.println("I will play at " + computerMove);
    				updateBoard(computerMove, 2);
    				displayBoard();
    				if (isGameWon())
    				{
    					System.out.println("Computer Wins!!!");
    					gameIsWon = true;
    					return;
    				}
    				prompt = "Please enter your next move: ";
    				i++;
    			}
    		}
    		if (!gameIsWon)
    			System.out.println("It's a draw!");
    
    			System.out.println("Want to play again, type 'yes' or type 'no' to quit");
    		}while(input.next().equalsIgnoreCase("yes"));
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Do While loop repeat issue

    If the human wins or loses, the code returns from the main method; don't do that; while I'm at it, simplify the logic by introducing another method that plays a single game:

    Java Code:
    Scanner input = new Scanner(System.in);
    do {
       playGame(); // this was originally in you main( ... ) method
       System.out.println("Want to play again, type 'yes' or type 'no' to quit");
    }while(input.next().equalsIgnoreCase("yes"));
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Do While loop repeat issue

    I don't quite follow. You want me to introduce a new method called "playGame" into the main method??? Is there any way you could give me a better example of this? I am only a few months into my java class.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Do While loop repeat issue

    What Jos is saying is to simply have a method which plays a single game and determines the winner. Then put the call to that method
    in a loop where the prompting for another game is done. Using his example for a simple coin toss.

    Java Code:
    static Scanner input = new Scanner(System.in);
       public static void main(String[] args) {
          do {
             playGame(); // this was originally in you main( ... ) method
             System.out.println("Want to play again, type 'yes' or type 'no' to quit");
          }while(input.next().equalsIgnoreCase("yes"));
       }
       
       
       public static void playGame() {
          char[] coin = {'h', 't'};
          System.out.print("Call it, heads or tails (h or t)? " );
          char c = input.next().toLowerCase().charAt(0);
          int i = (int)(Math.random()*2);
          if (c == coin[i]) {
             System.out.println("You win!!");
          } else {
             System.out.println("You lose!!");
          }
       }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Do While loop repeat issue

    Oh i understand. It took me a sec to get it. I was confused at first, but im gonna try it right now. Thank you for clarifying that.

  6. #6
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Do While loop repeat issue

    Ok I inserted the loop code in to my program and its works. The only problem im having now is after i start a new game its not actually a new game it's a continuation of the previous game which is no good. Trying different things but its giving me the same result. Any thoughts?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Do While loop repeat issue

    All the state within your method should be reset since they are local variables to that method. Perhaps the board is saving the old moves and you are not resetting it. This is just a guess since I don't know how you are populating the moves on the board (or how that code is written).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Do While loop repeat issue

    Jim, can you check out the rest of my code??? If so,i could I email it to you because my professor has a tendency to check these boards to make sure we're not getting help from anyone? Please, i would be really grateful....

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Do While loop repeat issue

    Well, I can't do that for the reasons you just stated. We don't spoon feed solutions here but we do help people try to understand what is going on. Any help I provide will have to be via the normal avenues. Of course, I don't understand not being able to get "help." Assuming it is homework, providing help on homework is a pretty common thing as long as the actual work is being done by the student. You may want to discuss this with your professor.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Do While loop repeat issue

    I think you forgot some code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Do While loop repeat issue

    A couple of things.

    1. You prompt for "yes" but check for "y"

    2. You need to reset your board as I suggested earlier. Some of your variables are lingering on to their previous state.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Join Date
    Oct 2016
    Posts
    27
    Rep Power
    0

    Default Re: Do While loop repeat issue

    OK, i could change the "Y".. but how do i reset the board???? I don't get that.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Do While loop repeat issue

    You need to use some print statements to see what values you used in the previous game are not reset when you start a new one. This is one of the basic tasks of writing code. And using print statements at various locations is the most common way of debugging a program.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Do While loop repeat issue

    Looks like someone deleted their code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. While loop repeat
    By Nb4bi9 in forum New To Java
    Replies: 3
    Last Post: 12-11-2012, 01:16 AM
  2. Replies: 18
    Last Post: 04-03-2012, 10:49 AM
  3. repeat if statement in loop
    By eng_hyzoom in forum New To Java
    Replies: 5
    Last Post: 03-05-2012, 01:34 PM
  4. Storing A Repeat Loop In An Array
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 07-22-2011, 06:57 AM
  5. Repeat while answer yes - do while loop
    By AJ2009 in forum New To Java
    Replies: 1
    Last Post: 01-10-2009, 07:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •