Pig (dice game)

Pig game using two die.

Page 1 of 1

14 Replies - 20315 Views - Last Post: 01 March 2009 - 03:36 PM Rate Topic: -----

#1 blueskyjunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-February 09

Pig (dice game)

Posted 27 February 2009 - 09:48 PM

Hey everyone! I'm new here and new to Java in general. I'm enrolled in my first computer science class (ap comp sci a), and I love it! My teacher assigned us to create the pig dice game for a homework assignment. You guys probably know what it is from all the other noobs like me online asking about it, but if you don't here it is: http://boardgames.ab...ig/piggame.html

The version I'm trying to make is similar (without the nice interface and graphics :P) except with two die. If you roll a one on either die you lose all the points you've accumulated during that round, and if you roll two 1s then you lose all your points. In either of these cases the die go to the other player and it's their turn.

The problems I'm encountering are:
1)I can't seem to set up a round system to where a player loses all the points they accumulated for all the rolls that round.
2)Sometimes when the human player rolls a 1 and the dice goes to the computer, it breaks out of the computer rolling loop early (before it scores 20 points or before it rolls a 1).

Here is my code to my Pig class. I wrote most of it without help; however, I got the ideas for some of the structure (loops and flags) from java.sun.com forums.

import javax.swing.JOptionPane;

public class Pig
{

	String Play;
	boolean playerTurn = true;
	final int WIN = 100;
	final int COMPMAX = 20;
	int play, playerScore = 0, compScore = 0, compEnd, points;
	int die1Roll, die2Roll;
	
	Die die1 = new Die();
	Die die2 = new Die();
	
//-------------------------------------------------------------------------------------------------
//Main method creates object of class and calls the play method to initiate gameplay.
//------------------------------------------------------------------------------------------------- 

	public static void main (String[] args)
	{

		JOptionPane.showMessageDialog(null, "Welcome to M@'s Pig game.");
	
		Pig pig = new Pig();
		
		pig.play();
		
	}
	
//---------------------------------------------------------------------------------
//Loops game and controls who has dice.
//---------------------------------------------------------------------------------   
	
	public void play()
	{
	
		while(playerTurn)
		{
			
			Play = JOptionPane.showInputDialog("Please enter 1 to roll the die or 2 to let the computer roll the die.");

			play = Integer.parseInt (Play);
		
			if (play == 1)
			{

				playerTurn = true;
		
			}
		
			else
			{
			
				playerTurn = false;
			
			}
			
			if (playerTurn == false)
			{
			 
				computerRoll();   
			
			}
			
			else
			{

				playerRoll();
			
			}
			
		}
			
	}   
	
//---------------------------------------------------------------------------------
//Rolls for the computer and gives corresponding points.
//---------------------------------------------------------------------------------

public void computerRoll()
{
	
				while (playerTurn == false)
				{
				
					die1Roll = die1.roll(); 
					die2Roll = die2.roll();
					
					JOptionPane.showMessageDialog(null, "Computer rolled: " + die1Roll + "  +  " + die2Roll);
					
					compScore = points(die1Roll, die2Roll, compScore);
					
					if (die1Roll == 1 || die2Roll == 1)
					{
					
						playerTurn = true;
					
					}
					
					JOptionPane.showMessageDialog(null, "Player Score: " + playerScore + "	" + "Computer Score: " + compScore);
					
					if (compEnd >= COMPMAX)
					{
					
						playerTurn = true;
						
						compEnd = 0;
					
					}
				
				}
				
				if (compScore >= WIN)
				{
				
					win();
				
				}
				
}

//---------------------------------------------------------------------------------
//Rolls for the player and gives corresponding points.
//---------------------------------------------------------------------------------

public void playerRoll()
{
	
				die1Roll = die1.roll(); 
				die2Roll = die2.roll();
				
				JOptionPane.showMessageDialog(null, "You rolled: " + die1Roll + "  +  " + die2Roll);
				
				playerScore = points(die1Roll, die2Roll, playerScore);
				
				JOptionPane.showMessageDialog(null, "Player Score: " + playerScore + "	" + "Computer Score: " + compScore);
				
				if (die1Roll == 1 || die2Roll == 1)
				{

					playerTurn = false;
					computerRoll();

				}
				
				if (playerScore >= WIN)
				{
				
					win();
				
				}
			
}
	
//------------------------------------------------------------------------------------
//Calculates points for players and checks to see they roll 1 or double 1s.
//------------------------------------------------------------------------------------	

	public int points(int die1Roll, int die2Roll, int points)
	{
		
		if (die1Roll == 1 && die2Roll == 1)
		{
			
			JOptionPane.showMessageDialog(null, "Lost all points!");
			
			points = 0;
			
			compEnd = 0;
			
			return points;
			
		}
	
		else if (die1Roll == 1 || die2Roll == 1)
		{
			
			JOptionPane.showMessageDialog(null, "Received no points!");
			
			if (points >= die1Roll + die2Roll)
			{
			
				points -= die1Roll + die2Roll;
			
			}
			
			else
			{
			
				points = 0;
			
			}
			
			return points;
		
		}
		
		else
		{

			compEnd += die1Roll + die2Roll;
			
			points += die1Roll + die2Roll;
			
			return points;
		
		}
		
	}

//---------------------------------------------------------------------------------
//Tests whether or not player or computer won.
//---------------------------------------------------------------------------------		
		
	public void win()
	{

		if (playerScore >= 100)
		{
		
			JOptionPane.showMessageDialog(null, "Congratulations! You Win!");
			
			System.exit(0);
			
		}	
			
		if (compScore >= 100)
		{
		
			JOptionPane.showMessageDialog(null, "You lose!");
			
			System.exit(0);
			
		} 
		
	}

}





There shouldn't be any errors in my Die class, but if you need to see it for some reason just let me know.

Any help is greatly appreciated!

This post has been edited by blueskyjunkie: 28 February 2009 - 08:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Pig (dice game)

#2 blueskyjunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-February 09

Re: Pig (dice game)

Posted 28 February 2009 - 07:36 AM

Anyone?
Was This Post Helpful? 0
  • +
  • -

#3 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Pig (dice game)

Posted 28 February 2009 - 11:33 AM

Quote

1)I can't seem to set up a round system to where a player loses all the points they accumulated for all the rolls that round.


Just add a "round" variable which keeps track of the points for the current round, if they lose the round don't add any points to the total for that player, if they win then add the round points to the total points for the round for that player.

Quote

2)Sometimes when the human player rolls a 1 and the dice goes to the computer, it breaks out of the computer rolling loop early (before it scores 20 points or before it rolls a 1).

Do you reset all the values before the computer's loop? If not I will take a closer look at the code later when I get a chance.
Was This Post Helpful? 0
  • +
  • -

#4 blueskyjunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-February 09

Re: Pig (dice game)

Posted 01 March 2009 - 01:09 PM

View PostBigAnt, on 28 Feb, 2009 - 10:33 AM, said:

Quote

1)I can't seem to set up a round system to where a player loses all the points they accumulated for all the rolls that round.


Just add a "round" variable which keeps track of the points for the current round, if they lose the round don't add any points to the total for that player, if they win then add the round points to the total points for the round for that player.

Quote

2)Sometimes when the human player rolls a 1 and the dice goes to the computer, it breaks out of the computer rolling loop early (before it scores 20 points or before it rolls a 1).

Do you reset all the values before the computer's loop? If not I will take a closer look at the code later when I get a chance.


//---------------------------------------------------------------------------------
//Checks to see if the round should end or not.
//---------------------------------------------------------------------------------	

	public boolean roundCont(int die1Roll, int die2Roll)
	{
		
		if (die1Roll == 1 && die2Roll == 1)
		{
			
			return true;
			
		}
	
		else if (die1Roll == 1 || die2Roll == 1)
		{

			return true;
		
		}
		
		else
		{
			
			return false;
		
		}
		
	}



I made that method to see if the round should end or not and thus end the round score calculation, but when I try to implement it into my points method I get the error "No applicable overload for the method roundCont."

	public int points(int die1Roll, int die2Roll, int points)
	{
		do
		{
		
			roundScore += die1Roll + die2Roll;
		
		}
		
		while(roundCont());

This post has been edited by blueskyjunkie: 01 March 2009 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#5 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Pig (dice game)

Posted 01 March 2009 - 01:25 PM

Where do you define the method roundCont?
Was This Post Helpful? 0
  • +
  • -

#6 blueskyjunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-February 09

Re: Pig (dice game)

Posted 01 March 2009 - 01:29 PM

The roundEnd method is roundCont. I changed the after I copied it here, and forgot to change it on here.
Was This Post Helpful? 0
  • +
  • -

#7 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Pig (dice game)

Posted 01 March 2009 - 01:46 PM

Ok, then you have to pass in the parameters to the method.

You specify the method signature as such: public boolean roundCont(int die1Roll, int die2Roll)

But then try and call it like: roundCont()

You need to provide the correct parameters to the method.
Was This Post Helpful? 1
  • +
  • -

#8 blueskyjunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-February 09

Re: Pig (dice game)

Posted 01 March 2009 - 02:00 PM

Ok thanks a lot! I feel like such a newb, but I'm trying to get the hang of this. I got it to compile and run, but it still won't erase all points earned by a player during that round if they roll a 1 (only the ones for that roll). I've now created another problem where it is somehow adding the players points to each other even though I reset all the variables.

Here is my points method.

//---------------------------------------------------------------------------------
//Calculates points for players and checks to see they roll 1 or double 1s.
//---------------------------------------------------------------------------------	

	public int points(int die1Roll, int die2Roll, int points)
	{

		if(roundCont(die1Roll, die2Roll));
		{
		
			roundScore += die1Roll + die2Roll;
			
		}
		
		if (die1Roll == 1 && die2Roll == 1)
		{
			
			JOptionPane.showMessageDialog(null, "Lost all points!");
			
			points = 0;
			
			compEnd = 0;
			
			roundScore = 0;
			
			return points;
			
		}
	
		else if (die1Roll == 1 || die2Roll == 1)
		{
			
			JOptionPane.showMessageDialog(null, "Received no points!");

			
				roundScore = 0;

			
			return points;
		
		}
		
		else
		{

			compEnd += die1Roll + die2Roll;
			
			points = roundScore;
			
			return points;
		
		}
		
	}


This post has been edited by blueskyjunkie: 01 March 2009 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

#9 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Pig (dice game)

Posted 01 March 2009 - 02:19 PM

So points is for the current roll and roundscore is for the current round?

Repost all your current code you now have.
Was This Post Helpful? 0
  • +
  • -

#10 blueskyjunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-February 09

Re: Pig (dice game)

Posted 01 March 2009 - 02:31 PM

Pig Class

import javax.swing.JOptionPane;
import java.util.*;

public class Pig
{

	String Play;
	boolean playerTurn = true;
	final int WIN = 100;
	final int COMPMAX = 20;
	int play, playerScore = 0, compScore = 0, compEnd, points, roundScore;
	int die1Roll, die2Roll;
	
	Die die1 = new Die();
	Die die2 = new Die();
	
//---------------------------------------------------------------------------------
//Main method creates object of class and calls the play method to initiate gameplay.
//---------------------------------------------------------------------------------	

	public static void main (String[] args)
	{

		JOptionPane.showMessageDialog(null, "Welcome to M@'s Pig game.");
	
		Pig pig = new Pig();
		
		pig.play();
		
	}
	
//---------------------------------------------------------------------------------
//Loops game and controls who has dice.
//---------------------------------------------------------------------------------   
	
	public void play()
	{
	
		while(playerTurn)
		{
			
			Play = JOptionPane.showInputDialog("Please enter 1 to roll the die or 2 to let the computer roll the die.");

			play = Integer.parseInt (Play);
		
			if (play == 1)
			{

				playerTurn = true;
		
			}
		
			else
			{
			
				playerTurn = false;
			
			}
			
			if (playerTurn == false)
			{
			 
				computerRoll();   
			
			}
			
			else
			{

				playerRoll();
			
			}
			
		}
			
	}   
	
//---------------------------------------------------------------------------------
//Rolls for the computer and gives corresponding points.
//---------------------------------------------------------------------------------

public void computerRoll()
{
	
				while (playerTurn == false)
				{
				
					die1Roll = die1.roll(); 
					die2Roll = die2.roll();
					
					JOptionPane.showMessageDialog(null, "Computer rolled: " + die1Roll + "  +  " + die2Roll);
					
					compScore = points(die1Roll, die2Roll, compScore);
					
					if (die1Roll == 1 || die2Roll == 1)
					{
					
						playerTurn = true;
					
					}
					
					JOptionPane.showMessageDialog(null, "Player Score: " + playerScore + "	" + "Computer Score: " + compScore);
					
					if (compEnd >= COMPMAX)
					{
					
						playerTurn = true;
						
						compEnd = 0;
					
					}
				
				}
				
				if (compScore >= WIN)
				{
				
					win();
				
				}
				
}

//---------------------------------------------------------------------------------
//Rolls for the player and gives corresponding points.
//---------------------------------------------------------------------------------

public void playerRoll()
{
	
				die1Roll = die1.roll(); 
				die2Roll = die2.roll();
				
				JOptionPane.showMessageDialog(null, "You rolled: " + die1Roll + "  +  " + die2Roll);
				
				playerScore = points(die1Roll, die2Roll, playerScore);
				
				JOptionPane.showMessageDialog(null, "Player Score: " + playerScore + "	" + "Computer Score: " + compScore);
				
				if (die1Roll == 1 || die2Roll == 1)
				{

					playerTurn = false;
					computerRoll();

				}
				
				if (playerScore >= WIN)
				{
				
					win();
				
				}
			
}
	
//---------------------------------------------------------------------------------
//Calculates points for players and checks to see they roll 1 or double 1s.
//---------------------------------------------------------------------------------	

	public int points(int die1Roll, int die2Roll, int points)
	{

		if(roundCont(die1Roll, die2Roll));
		{
		
			roundScore += die1Roll + die2Roll;
			
		}
		
		if (die1Roll == 1 && die2Roll == 1)
		{
			
			JOptionPane.showMessageDialog(null, "Lost all points!");
			
			points = 0;
			
			compEnd = 0;
			
			roundScore = 0;
			
			return points;
			
		}
	
		else if (die1Roll == 1 || die2Roll == 1)
		{
			
			JOptionPane.showMessageDialog(null, "Received no points!");

			
				roundScore = 0;

			
			return points;
		
		}
		
		else
		{

			compEnd += die1Roll + die2Roll;
			
			points = roundScore;
			
			return points;
		
		}
		
	}
	
//---------------------------------------------------------------------------------
//Checks to see if the round should end or not.
//---------------------------------------------------------------------------------	

	public boolean roundCont(int die1Roll, int die2Roll)
	{
		
		if (die1Roll == 1 && die2Roll == 1)
		{
			
			return false;
			
		}
	
		else if (die1Roll == 1 || die2Roll == 1)
		{

			return false;
		
		}
		
		else
		{
			
			return true;
		
		}
		
	}

//---------------------------------------------------------------------------------
//Tests whether or not player or computer won.
//---------------------------------------------------------------------------------		
		
	public void win()
	{

		if (playerScore >= 100)
		{
		
			JOptionPane.showMessageDialog(null, "Congratulations! You Win!");
			
			System.exit(0);
			
		}	
			
		if (compScore >= 100)
		{
		
			JOptionPane.showMessageDialog(null, "You lose!");
			
			System.exit(0);
			
		} 
		
	}

}





Die Class

import java.util.Random;

public class Die
{

	private final int MIN_FACES = 4;
	
	private static Random generator = new Random();
	private int numFaces;
	private int faceValue;
	
	public Die ()
	{
	
		numFaces = 6;
		faceValue = 1;
	
	}
	
	public int roll ()
	{
	
		faceValue = generator.nextInt(numFaces) + 1;
		return faceValue;
	
	}
	
	public int getFaceValue ()
	{
	
		return faceValue;
	
	}

}



Thanks for all your help man. Please forgive me for being an epic failure :P. I'm working hard to get better.

Yes.

My playerScore/compScore variables represent the total scores.

My points variable kind of acts like a round score so now that I think about it I think the roundScore variable I just created is unnecessary.
Was This Post Helpful? 0
  • +
  • -

#11 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Pig (dice game)

Posted 01 March 2009 - 02:34 PM

First thing I notice is in the if statement:

Quote

if(roundCont(die1Roll, die2Roll));
{

roundScore += die1Roll + die2Roll;

}

Remove the semicolon after the if statement or else the if will never do anything and the next line will always execute.
Was This Post Helpful? 1
  • +
  • -

#12 blueskyjunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-February 09

Re: Pig (dice game)

Posted 01 March 2009 - 02:38 PM

View PostBigAnt, on 1 Mar, 2009 - 01:34 PM, said:

First thing I notice is in the if statement:

Quote

if(roundCont(die1Roll, die2Roll));
{

roundScore += die1Roll + die2Roll;

}

Remove the semicolon after the if statement or else the if will never do anything and the next line will always execute.


Ahhh thanks! I copied that line from the "while" part of my the do while loop I tried before the if statement and I guess I forgot to take off the semicolon.

Just ran it, and my round system still isn't working right though.
Was This Post Helpful? 0
  • +
  • -

#13 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Pig (dice game)

Posted 01 March 2009 - 02:40 PM

Also do you wish in the points method, to use the points parameter passed in, or the points field from the class?

Second, in order to make the player/computer lose their entire score on snake eyes, then you will have to have a way to determine if the points method is being called by the human or the computer rolls.
Was This Post Helpful? 0
  • +
  • -

#14 blueskyjunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-February 09

Re: Pig (dice game)

Posted 01 March 2009 - 02:55 PM

Oh yeah I guess I should use the points field so that I can reset it to 0 instead of being equal to the playerScore from all previous rounds. Will that allow me to use my points variable to keep track of the round score?

Could I use a boolean value to accomplish this? Something like setting it equal to true if playerRoll or false if computerRoll right before the points method is called?
Was This Post Helpful? 0
  • +
  • -

#15 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Pig (dice game)

Posted 01 March 2009 - 03:36 PM

Quote

Oh yeah I guess I should use the points field so that I can reset it to 0 instead of being equal to the playerScore from all previous rounds. Will that allow me to use my points variable to keep track of the round score?

If you are going to have points and roundScore both be for the current round then it would probably be better to only use one of these.

Quote

Could I use a boolean value to accomplish this? Something like setting it equal to true if playerRoll or false if computerRoll right before the points method is called?

Just pass in the boolean as an additional parameter, then you can check this boolean to see which score to erase on account of a snake eyes roll.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1