SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Flow Of Control
Exercise:
Programming Projects
Question:10 | ISBN:9780132830317 | Edition: 5

Question

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

• ROLL AGAIN or

• 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 “r” to roll again or “h” to hold. The computer program should play according to the following rule:

• 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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// GameOfPig.java
import java.util.Random;
import java.util.Scanner;
public class GameOfPig
{
	public static void main(String[] args)
	{
		Random rand = new Random();
		Scanner keyboard = new Scanner(System.in);

		int humanTotalScore = 0;
		int compTotalScore = 0;
		int compSubTotal = 0;
		boolean run = true;
		String response;
		int roll = 0;

		do
		{
			System.out.println("*** Human's Turn ***");
			run = true;
			do
			{
				roll = 1 + rand.nextInt(6);
				System.out.println("Human rolled " + roll + ".");

				if (roll == 1)
				{
					System.out.println("Human's score: " + humanTotalScore 
							+ " and Computer's score: " + compTotalScore);
					run = false;
				}
				else
				{
					humanTotalScore += roll;
					System.out.println("Human's score: " + humanTotalScore 
							+ " and Computer's score: " + compTotalScore);

					if (humanTotalScore >= 100)
					{
						run = false;
					}
					else
					{
						System.out.print("Enter 'r' to roll again or 'h' to hold: ");
						response = keyboard.next();
						if (response.equalsIgnoreCase("h"))
						{
							run = false;
						}
					}
				}
			} while (run);

			if (humanTotalScore < 100)
			{
				System.out.println("\n*** Computer's Turn ***");

				compSubTotal = 0;
				run = true;
				do
				{
					roll = 1 + rand.nextInt(6);
					System.out.println("Computer rolled " + roll + ".");

					if (roll == 1)
					{
						System.out.println("Human's score: " + humanTotalScore 
								+ " and Computer's score: " + compTotalScore);
						run = false;
					}
					else
					{
						compSubTotal += roll;
						compTotalScore += roll;
						System.out.println("Human's score: " + humanTotalScore 
								+ " and Computer's score: " + compTotalScore);

						if (compSubTotal >= 20)
						{
							System.out.println("Computer's subtotal >= 20.");
							run = false;
						}
						
						if (compTotalScore >= 100)
						{
							run = false;
						}
					}
				} while (run && compSubTotal < 20);
			}
			System.out.println();
		} while (humanTotalScore < 100 && compTotalScore < 100);

		if (humanTotalScore >= 100)
			System.out.println("Human wins the game!");
		else
			System.out.println("Computer wins the game!");

		keyboard.close();
	}
}

Output:

*** Human's Turn ***
Human rolled 3.
Human's score: 3 and Computer's score: 0
Enter 'r' to roll again or 'h' to hold: r
Human rolled 5.
Human's score: 8 and Computer's score: 0
Enter 'r' to roll again or 'h' to hold: r
Human rolled 2.
Human's score: 10 and Computer's score: 0
Enter 'r' to roll again or 'h' to hold: h

*** Computer's Turn ***
Computer rolled 1.
Human's score: 10 and Computer's score: 0

*** Human's Turn ***
Human rolled 6.
Human's score: 16 and Computer's score: 0
Enter 'r' to roll again or 'h' to hold: h

*** Computer's Turn ***
Computer rolled 5.
Human's score: 16 and Computer's score: 5
Computer rolled 4.
Human's score: 16 and Computer's score: 9
Computer rolled 5.
Human's score: 16 and Computer's score: 14
Computer rolled 6.
Human's score: 16 and Computer's score: 20
Computer's subtotal >= 20.

*** Human's Turn ***
Human rolled 4.
Human's score: 20 and Computer's score: 20
Enter 'r' to roll again or 'h' to hold: r
Human rolled 2.
Human's score: 22 and Computer's score: 20
Enter 'r' to roll again or 'h' to hold: r
Human rolled 1.
Human's score: 22 and Computer's score: 20

*** Computer's Turn ***
Computer rolled 4.
Human's score: 22 and Computer's score: 24
Computer rolled 3.
Human's score: 22 and Computer's score: 27
Computer rolled 3.
Human's score: 22 and Computer's score: 30
Computer rolled 3.
Human's score: 22 and Computer's score: 33
Computer rolled 1.
Human's score: 22 and Computer's score: 33

*** Human's Turn ***
Human rolled 1.
Human's score: 22 and Computer's score: 33

*** Computer's Turn ***
Computer rolled 1.
Human's score: 22 and Computer's score: 33

*** Human's Turn ***
Human rolled 6.
Human's score: 28 and Computer's score: 33
Enter 'r' to roll again or 'h' to hold: r
Human rolled 5.
Human's score: 33 and Computer's score: 33
Enter 'r' to roll again or 'h' to hold: r
Human rolled 6.
Human's score: 39 and Computer's score: 33
Enter 'r' to roll again or 'h' to hold: r
Human rolled 2.
Human's score: 41 and Computer's score: 33
Enter 'r' to roll again or 'h' to hold: r
Human rolled 4.
Human's score: 45 and Computer's score: 33
Enter 'r' to roll again or 'h' to hold: r
Human rolled 2.
Human's score: 47 and Computer's score: 33
Enter 'r' to roll again or 'h' to hold: r
Human rolled 1.
Human's score: 47 and Computer's score: 33

*** Computer's Turn ***
Computer rolled 3.
Human's score: 47 and Computer's score: 36
Computer rolled 1.
Human's score: 47 and Computer's score: 36

*** Human's Turn ***
Human rolled 5.
Human's score: 52 and Computer's score: 36
Enter 'r' to roll again or 'h' to hold: r
Human rolled 3.
Human's score: 55 and Computer's score: 36
Enter 'r' to roll again or 'h' to hold: r
Human rolled 2.
Human's score: 57 and Computer's score: 36
Enter 'r' to roll again or 'h' to hold: h

*** Computer's Turn ***
Computer rolled 3.
Human's score: 57 and Computer's score: 39
Computer rolled 1.
Human's score: 57 and Computer's score: 39

*** Human's Turn ***
Human rolled 2.
Human's score: 59 and Computer's score: 39
Enter 'r' to roll again or 'h' to hold: h

*** Computer's Turn ***
Computer rolled 5.
Human's score: 59 and Computer's score: 44
Computer rolled 2.
Human's score: 59 and Computer's score: 46
Computer rolled 4.
Human's score: 59 and Computer's score: 50
Computer rolled 5.
Human's score: 59 and Computer's score: 55
Computer rolled 6.
Human's score: 59 and Computer's score: 61
Computer's subtotal >= 20.

*** Human's Turn ***
Human rolled 1.
Human's score: 59 and Computer's score: 61

*** Computer's Turn ***
Computer rolled 4.
Human's score: 59 and Computer's score: 65
Computer rolled 5.
Human's score: 59 and Computer's score: 70
Computer rolled 3.
Human's score: 59 and Computer's score: 73
Computer rolled 3.
Human's score: 59 and Computer's score: 76
Computer rolled 4.
Human's score: 59 and Computer's score: 80
Computer rolled 1.
Human's score: 59 and Computer's score: 80

*** Human's Turn ***
Human rolled 5.
Human's score: 64 and Computer's score: 80
Enter 'r' to roll again or 'h' to hold: r
Human rolled 3.
Human's score: 67 and Computer's score: 80
Enter 'r' to roll again or 'h' to hold: r
Human rolled 5.
Human's score: 72 and Computer's score: 80
Enter 'r' to roll again or 'h' to hold: r
Human rolled 4.
Human's score: 76 and Computer's score: 80
Enter 'r' to roll again or 'h' to hold: r
Human rolled 4.
Human's score: 80 and Computer's score: 80
Enter 'r' to roll again or 'h' to hold: r
Human rolled 6.
Human's score: 86 and Computer's score: 80
Enter 'r' to roll again or 'h' to hold: r
Human rolled 5.
Human's score: 91 and Computer's score: 80
Enter 'r' to roll again or 'h' to hold: r
Human rolled 4.
Human's score: 95 and Computer's score: 80
Enter 'r' to roll again or 'h' to hold: r
Human rolled 5.
Human's score: 100 and Computer's score: 80

Human wins the game!

 

0 0

Discussions

Post the discussion to improve the above solution.