Class PigHelper
For this problem, you will write some helper methods which will be used to play the game pig.
Playing Pig
Pig is a dice game where the goal is to score 100 points. Players take turns repeatedly rolling a die to accumulate points (their turn total). If they ever roll a 1, their turn total is 0, and it becomes the other player's turn. If they choose to stop rolling before rolling a 1, they add their turn total to their score and it is the other player's turn.An example round
The score starts at 0-0. Player one rolls: 5 4 6 4 3 1 so receives no points. Player two rolls: 3 2 5 3 2 2 4 and then holds adding the total, 21, to their score. The score is now 0-21.
The Pig GUI
- Create project for the Pig Game.
- Download PigSwing.java and PigHelper.java from the assignment. PigSwing contains the main program to run. PigHelper is where you will put your code
- Run PigSwing. It should display a window with working buttons.
- Write the body of the roll method. Test the use of the roll button. You should be able to accumulate points by rolling and holding. The computer will get 0 every turn.
- Write the body of isGameOver. Test to make sure the game ends when you get 100 points.
- Write the cpuRollAgain method. Start by returning turnTotal < 20 (a simple strategy).
- Write cpuTurn as described in its documentation.
- Implement the strategy described in cpuRollAgain method.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancpuRollAgain(int myScore, int yourScore, int myTurnTotal) Determine if the cpu should roll again based on its score, your score, and its turn total.static StringcpuTurn(int myScore, int yourScore) Run the entire turn for the computer player, adding each dice roll to a String as you go.static booleanisGameOver(int score) The game is over when someone's score is 100 or more.static introll()Use Math.random to produce a roll of a six-sided die.
-
Method Details
-
roll
public static int roll()Use Math.random to produce a roll of a six-sided die. This method is called any time the die needs to be rolled.- Returns:
- a random value between 1 and 6.
-
isGameOver
public static boolean isGameOver(int score) The game is over when someone's score is 100 or more. This method is called any time we need to check if the game is over.- Parameters:
score- The score to be checked.- Returns:
- true if the game is over, false otherwise.
-
cpuRollAgain
public static boolean cpuRollAgain(int myScore, int yourScore, int myTurnTotal) Determine if the cpu should roll again based on its score, your score, and its turn total. One simple method, although sub-optimal, is for the computer to roll if it hasn't gotten enough points this round, or it's score is lower than the player's, or the player is close to winning. You can decide what "enough points" are and what it means for a player to be close to winning.This method is called to determine if the computer player should roll or hold.
- Parameters:
myScore- the computer's score.yourScore- the player's score.myTurnTotal- the computer's current turn total- Returns:
- true if the Computer should roll again and false otherwise.
-
cpuTurn
Run the entire turn for the computer player, adding each dice roll to a String as you go. At the end concatenate an equal sign and the turn total.2 3 4 =9 indicates the computer rolled a 2 then a 3 then a 4 and then decided to hold with a turn total of 9.
6 4 2 1 =0 indicates the computer rolled a 6, 4, 2, and finally a 1 which ended its turn with no points.
Repeatedly roll the dice until one of the following conditions hold
- 1 is rolled, or
- the computer can hold and win the game, or
- the computer chooses to hold (as defined by the cpuRollAgain method).
Update the turn total as you go. Concatenate each roll onto the result String.
If the loop ends from a dice roll of 1, set turn total to 0.
This method should call the other methods you wrote whenever possible.
This method is called to simulate the computer player's turn and provide the resulting turn total.
- Parameters:
myScore- the computer's score.yourScore- the player's score.- Returns:
- the String as described above. Be sure it ends with = followed by the turn total.
-