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:
Code Java:
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!
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()
Re: Boolean Flag Not Working to Move Between While Loops?
Re: Boolean Flag Not Working to Move Between While Loops?
Please mark solved as soon as your problem solved.
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!
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
Re: Boolean Flag Not Working to Move Between While Loops?
Thank you!! Sorry about that...