Write a program that plays the game of pig where one player


Programming Assignment: The Game of Pig

The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die:

• If the player rolls a 1, then the player gets no new points and it becomes the other player's turn.

• If the player rolls 2 through 6, then he or she can either

o ROLL AGAIN

or

o HOLD:

At this point, the sum of all rolls is added to the player's score and it becomes the other player's turn.

Write a program that plays the game of Pig, where one player is a human and the other is the computer. When it is the human's turn, the program should show the score of both players and the previous roll. Allow the human to input whether to roll again or hold.

The computer program should play according to the following rules:

• Keep rolling when it is the computer's turn until it has accumulated 20 or more points, then hold.
• If the computer wins or rolls a 1, then the turn ends immediately.

Allow the human to roll first.

Notes

To make your task easier, you should modularize your codes as the following methods:

• staticintusersTurn(intoldTotal): user rolls until they either:

o hold: the turn total is added to the oldTotal and the sum is returned
o roll a 1: the oldTotal is returned

• staticintcomputersTurn(intoldTotal): computer rolls until it either:

o holds (once it has accumulated 20 or more points for this turn): the turn total is added to the oldTotal and the sum is returned
o rolls a 1: the oldTotal is returned

• staticintrollDie(): simulates rolling a die (random number from 1 to 6).

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Write a program that plays the game of pig where one player
Reference No:- TGS02245493

Now Priced at $35 (50% Discount)

Recommended (91%)

Rated (4.3/5)