The version I'm trying to make is similar (without the nice interface and graphics
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

New Topic/Question
Reply




MultiQuote





|