All pastes #460941 Raw Edit

pig game die class

public java v1 · immutable
#460941 ·published 2007-04-27 12:09 UTC
rendered paste body
//import Die.java;import cs1.Keyboard;public class Pig {	public static void main (String [] args) {		Die die1 = new Die();		Die die2 = new Die();		int PlayerPoints = 0;		int CompPoints = 0;		int RoundPoints = 0;		int roll1 = 0;		int roll2 = 0;		String PlayerRollAgain = "y";		String CompRollAgain = "y";		String PlayGame = "y";		System.out.println("Welcome to Pig, project 4.2 from the textbook");		do{		do		{						roll1 = die1.roll();			roll2 = die2.roll();			System.out.println("You rolled a " + roll1 + 			" and a " + roll2);			if (roll1 == 1 || roll2 == 1)			{				System.out.print("Oh no! You rolled a one! Control " +				"now goes to the computer\n\n");				RoundPoints = 0;				PlayerRollAgain = "n";			}			else			{				RoundPoints = RoundPoints + roll1 + roll2;				System.out.println("Your total so far for this round is: " + RoundPoints + 				"\nYour total from all previous rounds is: " + PlayerPoints + 				"\nThe computer's score is: " + CompPoints + 				"\nWould you like to roll again?");				PlayerRollAgain = Keyboard.readString();			}		}		while (PlayerRollAgain.equalsIgnoreCase("y"));		if (!PlayerRollAgain.equalsIgnoreCase("y"))		{			PlayerPoints = PlayerPoints + RoundPoints;			RoundPoints = 0;			CompRollAgain = "y";		}		do 		{			roll1 = die1.roll();			roll2 = die2.roll();			if (roll1 == 1 || roll2 == 1)			{				System.out.println("The computer has rolled a one!" + 				" You have control of the dice.\n\n");				RoundPoints = 0;				PlayerRollAgain = "y";				CompRollAgain = "n";			}			else			{				RoundPoints = RoundPoints + roll1 + roll2;				System.out.println("The computer rolled a " + roll1 + 				" and a " + roll2);			}		}		while (RoundPoints < 20 && PlayerPoints < 100 && CompRollAgain.equalsIgnoreCase("y"));		if (RoundPoints >= 20)		{			System.out.println("The computer has given up control of the dice. It is your turn");			CompRollAgain = "n";			CompPoints = CompPoints + RoundPoints;			RoundPoints = 0;			PlayerRollAgain = "y";		}		if (CompPoints >= 100)		{			System.out.println("The computer has won. Nice try, thank you " +			"for playing.");			PlayerRollAgain = "n";			CompRollAgain = "n";			PlayGame = "n";		}		else if (PlayerPoints >= 100)		{			System.out.println("Congratulations, you won! Thank you for playing");			PlayerRollAgain = "n";			CompRollAgain = "n";			PlayGame = "n";		}	}	while (PlayGame.equalsIgnoreCase("y"));	}}		**********************************************//*******************************************************************//  Die.java       Stolen from the textbook by: Ryan Giglio////  Represents one die (singular of dice) with faces showing values//  between 1 and the number of faces on the die.//*******************************************************************public class Die{	private final int MIN_FACES = 4;		private int numFaces;  // number of sides on the die	private int faceValue; // current value showing on the die		//---------------------------------------------------------------	//  Defaults to a six-sided die, initially showing 1	//---------------------------------------------------------------	public Die()	{		numFaces = 6;		faceValue = 1;	}	//---------------------------------------------------------------	//  Explicitly sets the size of the die.  Defaults to a size of	//  six if the parameter is invalid.  Initial face 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 = (int) (Math.random() * numFaces) + 1;		return faceValue;	}		//---------------------------------------------------------------	//  Returns the current die value.	//---------------------------------------------------------------	public int getFaceValue()	{		return faceValue;	}}