Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Boolean Flag Not Working to Move Between While Loops?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Boolean Flag Not Working to Move Between While Loops?

    Hello, all. So, this is a project I was assigned in my computer science class. We have to design a dice game called Pig, to be played between a human player and the computer. The rules are as follows (I'm taking the wording out of my textbook): On each turn, the player rolls a pair of dice and adds up his or her points. Whoever reaches 100 points first, wins. If a player rolls a 1, he or she loses all points for that round and the dice go to the other player. If a player rolls two 1s in one turn, the player loses all points earned so far in he game and loses control of the dice. The player must voluntarily turn over the dice after each roll. So the player must decide to either roll again (be a pig) and risk losing points, or give up the dice, possibly letting the other player win. Set up the computer player so that it always gives up the dice after getting 20 or more points in a round.

    So, here is my code:
     
    import java.util.Scanner;
     
    public class pig_main {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		Scanner keyboard = new Scanner (System.in);
     
    		int pgame = 0; //player points for the game
    		int cgame = 0; //computer points for the game
    		int pround = 0; //player points for a single round
    		int cround = 0; //computer points for a single round
    		boolean flag = true; //when the flag is true, it is the player's turn
    							 //when the flag is false, it is the computer's turn
     
    		while (pgame < 100 && cgame < 100) { //play to 100
     
    			while (flag == true) { //player's turn
     
    				int pturn = play.takeTurn("You"); //roll dice, assess whether or not a 1 was rolled
    												  //if a 1 wasn't rolled: adds dice face values, returns total
    												  //if one 1 was rolled: announces consequences, returns 0
    												  //if two 1s were rolled: announces consequences, returns -1
     
    				if (pturn > 0) { //if player didn't roll a 1
     
    					System.out.println("You rolled a(n) " + pturn + "."); //announces total
    					pround += pturn; //computes total player points for the round
    					pgame += pturn; //computes player points for the game
    					System.out.println("You have " + pround + " points this turn and " + pgame + " total."); //announces player points
    					System.out.println("Computer has " + cgame + " points."); //announces computer points
    					System.out.print("Roll again? "); //prompt player to roll again or pass play to computer
    					String answer = keyboard.next(); //player answers "yes" or "no"
    					answer = answer.toLowerCase(); //convert answer to lower case
     
    					if (answer == "yes"){					
    						flag = true; //player goes again					
    					}
    					if (answer == "no") {
    						pround = 0; //resets player points for the round
    						flag = false; //in theory, passes play to computer
    					}
     
    				} else {					
    					if (pturn == 0) { //if player rolled one 1
    						pround = 0; //player loses all points for the round
    						flag = false; //play moves to computer
    					} else {						
    						if (pturn == -1) { //if player rolled two 1s
    							pround = 0; //player loses all points for the round
    							pgame = 0; //player loses all points for the game
    							flag = false; //play moves to computer
    						}
    					}
    				}
    			}
     
    			while (flag == false) { //computer's turn
     
    				int cturn = play.takeTurn("Computer"); //roll dice, assess whether or not a 1 was rolled
    				  									   //if a 1 wasn't rolled: adds dice face values, returns total
    				    								   //if one 1 was rolled: announces consequences, returns 0
    													   //if two 1s were rolled: announces consequences, returns -1
     
    				if (cturn > 0) { //if computer didn't roll a 1
     
    					System.out.println("Computer rolled a(n) " + cturn + "."); //announces total
    					cround += cturn; //computes total computer points for the round
    					cgame += cturn; //computes computer points for the game
    					System.out.println("Computer has " + cround + " points this turn and " + cgame + " total."); //announces computer points
    					System.out.println("You have " + pgame + " points."); //announces player points
     
    					if (cturn < 20) { //if total computer points for the turn haven't reached 20
    						System.out.println("Computer rolls again.\n");
    						flag = false; //computer goes again
    					} else { //if total computer points for the turn have reached 20
    						System.out.println("Computer has reahced 20 points. Play moves to you.\n");
    						flag = true; //play moves to player
    					}
     
    				} else { //if computer rolled one 1
    					if (cturn == 0) {
    						cround = 0; //computer loses all points for the round
    						flag = true; //play moves to player
    					} else {
    						if (cturn == -1) { //if computer rolled two 1s
    							cround = 0; //computer loses all points for the round
    							cgame = 0; //computer loses all points for the game
    							flag = true; //play moves to player
    						}
    					}
    				}		
    			}
    		}
     
    		if (pgame > cgame) { //if the player reached 100 points first
    		System.out.println("You won! Congratulations!");
    		} else { //if the computer reached 100 points first
    			System.out.println("Sorry, you lost. Better luck next time!");
    		}
    	}
    }

    My problem lies in that whenever I give an answer other then "yes" when prompted to roll again, the program proceeds to run the player's turn again rather than moving to the computer's turn. (Hence why my note says "in theory.") Any other time that I change the status of "flag," though, the program responds correctly. So basically, the human player cannot, right now, choose to pass the dice to the computer. Could anyone tell me why and/or how to fix it?

    Please let me know if I have not been clear or if you have any questions. Thank you!


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Boolean Flag Not Working to Move Between While Loops?

    When you compare answer to "yes" or "no" you should use the .equals() or .equalsIgnoreCase() method from the String class

    == checks the equality of the references. .equals() checks the equality of the contents of the String objects

    Here is a link I found googling: Java: ==, .equals()

  3. The Following User Says Thank You to DavidFongs For This Useful Post:

    verbicidalmaniac (January 19th, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Flag Not Working to Move Between While Loops?

    Thanks very much!

  5. #4
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Boolean Flag Not Working to Move Between While Loops?

    Please mark solved as soon as your problem solved.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Flag Not Working to Move Between While Loops?

    Dan,

    I know I should, and I've been trying, but I can't figure out how. Could you please tell me? Thank you!

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Boolean Flag Not Working to Move Between While Loops?

    Go to the top of this page, Thread Tools drop down menu -> Mark this thread as solved

  8. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Flag Not Working to Move Between While Loops?

    Thank you!! Sorry about that...

Similar Threads

  1. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  2. Passing in more than one flag
    By anon181 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 2nd, 2010, 06:37 AM
  3. what if flag?
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: November 8th, 2009, 12:44 AM
  4. Want to move my button away from center.
    By Fendaril in forum AWT / Java Swing
    Replies: 2
    Last Post: November 6th, 2009, 10:05 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM

Tags for this Thread