6 Replies - 536 Views - Last Post: 11 June 2013 - 03:52 AM Rate Topic: -----

#1 Icedrake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-June 13

Java "Pig" Game, booleans within while loops not working corre

Posted 10 June 2013 - 06:31 PM

Hello everyone. The issue that I'm having with this program assignment for my computer science class named Pig is that the booleans that are within my while loops aren't working correctly. What's supposed to happen is that within the while loop for the player's turn, and the else part of the if statement, the loop of the player should stop when the boolean for playerturn is set to false. However, this never happens and the loop for the player's turn keeps on going and going, no matter if I type in yes or no for the keepplaying string. I will post the code for both the Die class used by this program (this is pre-provided, so no problems here), and the code I currently have for this Pig game. I'm absolutely stumped as to what's going wrong here.

Die class used by the Pig game:
/*
 * Die.java
 * 
 * Represents one die (singular of dice) with faces showing values between 1
 * and the number of faces on the die. 
 */
import java.util.Random;

public class Die
{
    private final int MIN_FACES = 4;    
    private static Random generator = new Random();
    private int numFaces; // number of sides on the die
    private int faceValue; // current value showing on the die    
    /*
     * Defaults to a six-sided die. Initial face value is 1.
     */
    public Die()
    {
        numFaces = 6;
        faceValue = 1;
    }
    /*
     * Explicitly sets the zie of the die. Defaults to a size of six
     * if the parameter is invalid. Initial face value is 1.
     */
    public Die (int faces)
    {
        if (faces < MIN_FACES)
        {
            numFaces = 6;
        }
        else
        {
            numFaces = faces;
        }
        faceValue = 1;
    }    
    /*
     * Rolls the die and returns the result.
     */
    public int roll ()
    {
        faceValue = generator.nextInt(numFaces) + 1;
        return faceValue;
    }
    /*
     * Returns the current die value.
     */
    public int getFaceValue()
    {
        return faceValue;
    }
}


The code I have for the Pig game:
import java.util.Scanner;

public class Pig
{
    public static void main(String[] args)
    {      
        final int ROUNDPOINTS = 20;
        final int WINPOINTS = 100;
        
        int die1side;
        int die2side;
        int playersum=0;
        int compsum=0;
        int playercurrent=0;
        int compcurrent=0;
        
        boolean playerturn = true;
        
        String another = "y";
        String keepplaying = "y";
        
        Scanner scan = new Scanner (System.in);
        
        Die die1 = new Die();
        Die die2 = new Die();
        
        System.out.println("Welcome to the Pig game. Press y to begin.");
        another = scan.nextLine();
        
        while (another.equalsIgnoreCase("y"))
        {
            if (playersum>=100)
            {
                System.out.println("Congratulations, you win!");
                System.out.println("Play again (y/n)?");
                another = scan.nextLine();
            }
            else if (compsum>=100)
            {
                System.out.println("You lose, the computer is victorious.");
                System.out.println("Play again (y/n)?");
                another = scan.nextLine();
            }
            else
            {
            }           
            while (playerturn=true)
            {
                die1side = die1.roll();
                die2side = die2.roll();
                
                if ((die1side == 1) || (die2side == 1))
                {
                    System.out.println("You rolled two 1s. All points earned so far in the game lost.");
                    playersum = 0;
                    playerturn=false;                
                }
                else if ((die1side == 1) || (die2side == 1))
                {
                    System.out.println("You rolled a 1. All points for this round lost.");
                    playercurrent = 0;
                    playerturn=false; 
                }
                else
                {
                    playercurrent = playercurrent + die1side + die2side;
                    System.out.println("You rolled a "+die1side+" and a "+die2side+", and your current score is "+playercurrent+"");
                    System.out.println("Would you like to roll again (y/n)?");
                    keepplaying = scan.nextLine();
                    if (keepplaying.equalsIgnoreCase("y"))
                    {
                        playerturn=true;
                    }
                    else
                    {
                        playerturn=false;
                    }
                }
                
                playersum = playersum + playercurrent;
            }            
            while (playerturn=false)
            {
                while (compcurrent < ROUNDPOINTS)
                {              
                    die1side = die1.roll();
                    die2side = die2.roll();
                    System.out.println("The computer rolled a "+die1side+" and a "+die2side+"");
                    compcurrent = compcurrent + die1side + die2side;
                }
                compsum = compsum + compcurrent;
                playerturn = true;                                
            }           
        }
    }                     
}    



Is This A Good Question/Topic? 0
  • +

Replies To: Java "Pig" Game, booleans within while loops not working corre

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java "Pig" Game, booleans within while loops not working corre

Posted 10 June 2013 - 07:40 PM

A lot of horror in your code. But for sure

while (playerturn=true)

this assigns true to playerturn and enter the while loop forever as you'll assignr "true" to playerturn at each iteration so simply write

while(true)

the others who read you code will have less problem figuring out what you are trymg to do :)
Was This Post Helpful? 0
  • +
  • -

#3 Icedrake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-June 13

Re: Java "Pig" Game, booleans within while loops not working corre

Posted 10 June 2013 - 07:51 PM

Dang, I didn't realize I was that bad at Java... Maybe I should rethink this whole computer science thing...

Anyways, I changed the while (playerturn=true) to while (true), but now I'm getting an unreachable statement error at the while (playerturn=false) area (which I thought would be fixed if I changed it to while (false), but that didn't work). I assume I'm getting this error because changing while (playerturn=true) to just while(true) makes having the while (playerturn=false) statement redundant, but I'm not sure how to have that computer player's code section if I remove that... Maybe I should restart from the beginning and just have different methods for the computer and the player.

Thanks for the reply as well!
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java "Pig" Game, booleans within while loops not working corre

Posted 10 June 2013 - 07:57 PM

This is anoher coding inepsty.. or wahtever the Englisg word for "totaly non semse is"

while (playerturn=false) 


the while loop will be executed if playerturn == true
but you set it to false at the beginning of the the loop
so the inside of this while) will never been executed
so th whole while() is completly useless :)
Was This Post Helpful? 0
  • +
  • -

#5 Icedrake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-June 13

Re: Java "Pig" Game, booleans within while loops not working corre

Posted 10 June 2013 - 08:03 PM

Yeah, I completely messed up with all of the while loops and everything... I will restart this tomorrow as it's 11 PM currently, and I will post back the new code I come up with. Hopefully it's not as bad as this.
Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Java "Pig" Game, booleans within while loops not working corre

Posted 10 June 2013 - 08:15 PM

pbl is trying to enlighten you to the difference between the assignment(=) and equality(==) operators. Assignment will assign a value to your variable. In a while loop that might as well be 'while(true)' because regardless of what the current value of the variable, you are changing the value you want to test and creating a 'true' situation before the loop even executes.

You want to be using the equality operator(==) to test for equality between your boolean and a boolean value (true/false).

Changing to while(true) won't change anything, never mind fixing anything. :)
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Java "Pig" Game, booleans within while loops not working corre

Posted 11 June 2013 - 03:52 AM

First, stop this.

View PostIcedrake, on 10 June 2013 - 10:51 PM, said:

Maybe I should rethink this whole computer science thing...


You need a high degree of frustration tolerance to program. So maybe this is true. But not for the code. You're learning! You also need to be willing to see your mistakes so you can fix them, which means you have to be tolerant of others pointing them out. ;)

Computers are too stupid to figure out what you're thinking. However, they can tell you exactly what they're thinking. When in doubt, dump out more information about what's going in.

If your condition is already a boolean variable, just test it with while (foo) { and while (!foo) {. Less error prone and easier to read.

Note, you do not need die1 and die2 in your code. Just one instance will do. Think about how you're using your one instance of Scanner for many things.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1