Requirements: 1. You are not allowed to use global variables. 2. Must declare th
ID: 3606338 • Letter: R
Question
Requirements:
1. You are not allowed to use global variables.
2. Must declare the following two constants
public static final int POINTS = 30;
public static final int FORFEIT_POINTS = 20;
3. You can implement your own solution but you are required to break down the problem into smaller pieces.
4. You must provide the output for two rounds of game minimum.
5. Since we are using the Random number generator your output will not be exactly like mine. However, all the prompts must be exactly as the provided output.
Problem: Write a program to play the pig game against the computer. At each turn, the current player will roll a pair of dice and accumulate points. The goal is to reach 100 or more points before your opponent does. (For testing purposes use 30 instead of 100 points) If, on any turn, the player rolls a 1, all the points accumulated for that round are forfeited and the control of the dice moves to the other player. If the player rolls two 1s in one turn, the player loses all the points accumulated thus far and the control moves to the other player. The player may voluntarily turn over the control of the dice after each roll. Therefore, player must decide to roll again (be a pig) and risk losing points, or relinquish control of the dice, possibly allowing the other player to win. The computer flips a coin to choose the first player. Note that there are two constants in this program, POINTS and FORFEIT_POINTS. The POINTS constant is the number of points necessary for winning the game, for testing purposes we will use 30 but it could be set to any value. The FORFEIT_POINTS constant is the number of points that the computer player needs to reach before forfeiting their turn and passing control back to the human player. After the computer player has a score greater than or equal to FORFEIT_POINTS then it will only roll one time each subsequent turn. If the computer has less than FORFEIT_POINTS then it will continue to roll until it achieves a score greater than or equal to FORFEIT_POINTS.
Here is the list of the tasks that needs to be done
1. Describe the game by writing a method.
2. Data validation: A method that accepts a Scanner object as its parameter, prompt the user to enter “yes” or “no”. as long as the user is not entering a valid input , prompt the user again
3. Flip the coin: This method accepts a Random object and returns” head” or “tail” based on the random number that was generated
4. Roll two dices: this method accepts a Random object. Generates two random number representing one of the numbers on a dice. Returns the sum of the die.
5. Choose a name for the computer: Come up with 10 different name for the computer. Then select a random name from the list that you created. Return the selected name.
6. Play: this method calls the other methods to play the game
a. Declare all the needed variables to keep track of the scores for each player, and Boolean variables to indicate who is playing at the moment.
b. Ask the user’s name
c. Decide who start the game first by calling one of the methods you created to flip the coin.
d. Write conditional statements to switch the game between the computer and the player based on the dice rolled and overall points. Read the output and the program description to figure out the conditions. You need to use couple while loops: one loop for the human player, one loop for the computer player. The previous two loops will be nested in another while loop to witch the game between the two players.
//declaring your variables
While (there is no winner)
{
//some codes
While (human is playing)
{
//some codes, conditional statements
}
//you may need some codes
While (computer is playing)
{
//some codes, conditional statements
}
//you may need some codes
}
e. Keep playing the game until the player or the computer has 100 or more points (30 or more for the simplicity). You are not allowed to hard code any numbers.
7. Main method: Calls the method play, keep playing the game as long as there are more players.
Explanation / Answer
import java.util.*;
public class PigGame
{
public static void main(String[] args)
{
int turnScore = 0;
int totalScore = 0;
int turnScore2 = 0;
int totalScore2 = 0;
final int WIN = 30;
int dice = 0;
int dice2 = 0;
String input = "r";
String input2 = "r";
char repeat;
Scanner keyboard = new Scanner(System.in);
Scanner s = new Scanner (System.in);
Random randomNumbers = new Random();
while(totalScore < WIN && totalScore2 < WIN)
{
//Player 1's turn
do
{
dice = randomNumbers.nextInt(6) + 1;
System.out.println();
System.out.println("You rolled: " + dice);
if(dice == 1)
{
turnScore = 0;
System.out.println("Turn over.");
System.out.println("Player 1 total is " + totalScore);
break;
}
else
{
turnScore += dice;
System.out.print("Player 1 turn total is " + turnScore + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over.");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}
while(input.equalsIgnoreCase("r") || dice != 1);
{
totalScore += turnScore;
}
if(totalScore >= WIN)
{
System.out.println("Your total Score is " + totalScore);
System.out.println("Player 1 wins!");
}
//player2's turn
System.out.println();
System.out.println("It is Player 2's turn.");
{ do
{
dice2 = randomNumbers.nextInt(6) + 1;
System.out.println("Player 2 rolled: " + dice2);
if(dice2 == 1)
{
turnScore2 = 0;
System.out.print("Turn over");
System.out.println("Player 2 total is " + totalScore2);
break;
}
else
{
turnScore2 += dice2;
System.out.print("Player 2 total is " +turnScore2 + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}
while(input2.equalsIgnoreCase("r") && dice != 1); {
totalScore2 += turnScore2;
}
}
if(totalScore2 >= WIN);
{
System.out.println("Player 2 score is " + totalScore2 + " ");
System.out.println("Player 2 wins");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.