Question

The game of Pig is a simple two-player dice game in which the first player to...

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

use netbeans

0 0
Add a comment Improve this question Transcribed image text
Answer #1

GameOfPig.java

import java.util.Random;
import java.util.Scanner;

public class GameOfPig {
   static Random r;
   static Scanner sc=new Scanner(System.in);
   public static void main(String[] args) {
       int user_tot_score=0,comp_to_score=0;
       r=new Random();

System.out.println("Welcome to the game of Pig!");

while(true)
{
     
   user_tot_score=usersTurn(user_tot_score);
     
   comp_to_score=computersTurn(comp_to_score);
      
}

   }

   private static int usersTurn(int user_tot_score) {
       char ch;
       int rand=0,user_temp_tot=0,user_turn_score=0;
       while(true)
   {
         
       rand=rollDie();
if(rand==1)
{
   System.out.println("You rolled: "+rand);
   user_temp_tot=0;
   user_turn_score=0;
   break;
}
System.out.println("\nYou rolled: "+rand);
user_turn_score+=rand;

user_temp_tot=user_tot_score+user_turn_score;

System.out.println("Your turn score is "+user_turn_score+" and your total score is "+user_tot_score);
System.out.println("If you hold, you will have "+user_temp_tot+" points");
System.out.println("Enter 'r' to roll again, 's' to stop.");
ch = sc.next(".").charAt(0);
if(ch=='r'||ch=='R')
continue;
else
{
    if(user_temp_tot>=100)
       {
           System.out.println("User Wins !");
           user_tot_score=user_temp_tot;
           System.out.println("The User's score is "+user_tot_score);
           System.exit(0);
       }
user_tot_score=user_temp_tot;
System.out.println("Your score is "+user_tot_score);
user_turn_score=0;
user_temp_tot=0;
break;
}
   }
       return user_tot_score;
   }
   private static int computersTurn(int comp_to_score)
   {
       int rand1,com_turn_score=0,com_temp_tot=0;
       System.out.println("\nIt is the computer's turn.\n");
       while(true)
   {
       rand1=rollDie();
       System.out.println("The computer rolled: "+rand1);
       if(rand1==1)
       {
           System.out.println("The computer lost its turn!");
           System.out.println("The computer's score is "+comp_to_score);
           com_turn_score=0;
           com_temp_tot=0;
           break;
       }
       com_turn_score+=rand1;
       com_temp_tot=comp_to_score+com_turn_score;
       if(com_temp_tot>=100)
       {
           System.out.println("Computer Wins !");
           comp_to_score=com_temp_tot;
           System.out.println("The computer's score is "+comp_to_score);
           System.exit(0);
       }
       if(com_turn_score>=20 )
       {
           comp_to_score+=com_turn_score;
           System.out.println("The computer holds.");
           System.out.println("The computer's score is "+comp_to_score);

           com_turn_score=0;
           break;
       }
       else
       {
           continue;
       }
         
   }
       return comp_to_score;
     
   }
   static int rollDie()
   {
       return r.nextInt(6) + 1;
   }

}

_____________________________________

Output:

Welcome to the game of Pig!

You rolled: 2
Your turn score is 2 and your total score is 0
If you hold, you will have 2 points
Enter 'r' to roll again, 's' to stop.
r
You rolled: 1

It is the computer's turn.

The computer rolled: 6
The computer rolled: 2
The computer rolled: 5
The computer rolled: 3
The computer rolled: 4
The computer holds.
The computer's score is 20

You rolled: 5
Your turn score is 5 and your total score is 0
If you hold, you will have 5 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 6
Your turn score is 11 and your total score is 0
If you hold, you will have 11 points
Enter 'r' to roll again, 's' to stop.
r
You rolled: 1

It is the computer's turn.

The computer rolled: 6
The computer rolled: 5
The computer rolled: 6
The computer rolled: 2
The computer rolled: 4
The computer holds.
The computer's score is 43

You rolled: 2
Your turn score is 2 and your total score is 0
If you hold, you will have 2 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 2
Your turn score is 4 and your total score is 0
If you hold, you will have 4 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 2
Your turn score is 6 and your total score is 0
If you hold, you will have 6 points
Enter 'r' to roll again, 's' to stop.
r
You rolled: 1

It is the computer's turn.

The computer rolled: 2
The computer rolled: 3
The computer rolled: 3
The computer rolled: 1
The computer lost its turn!
The computer's score is 43
You rolled: 1

It is the computer's turn.

The computer rolled: 2
The computer rolled: 5
The computer rolled: 2
The computer rolled: 5
The computer rolled: 2
The computer rolled: 6
The computer holds.
The computer's score is 65

You rolled: 2
Your turn score is 2 and your total score is 0
If you hold, you will have 2 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 4
Your turn score is 6 and your total score is 0
If you hold, you will have 6 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 4
Your turn score is 10 and your total score is 0
If you hold, you will have 10 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 2
Your turn score is 12 and your total score is 0
If you hold, you will have 12 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 4
Your turn score is 16 and your total score is 0
If you hold, you will have 16 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 2
Your turn score is 18 and your total score is 0
If you hold, you will have 18 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 5
Your turn score is 23 and your total score is 0
If you hold, you will have 23 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 4
Your turn score is 27 and your total score is 0
If you hold, you will have 27 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 4
Your turn score is 31 and your total score is 0
If you hold, you will have 31 points
Enter 'r' to roll again, 's' to stop.
s
Your score is 31

It is the computer's turn.

The computer rolled: 4
The computer rolled: 5
The computer rolled: 3
The computer rolled: 1
The computer lost its turn!
The computer's score is 65

You rolled: 4
Your turn score is 4 and your total score is 31
If you hold, you will have 35 points
Enter 'r' to roll again, 's' to stop.
r
You rolled: 1

It is the computer's turn.

The computer rolled: 1
The computer lost its turn!
The computer's score is 65

You rolled: 4
Your turn score is 4 and your total score is 31
If you hold, you will have 35 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 3
Your turn score is 7 and your total score is 31
If you hold, you will have 38 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 3
Your turn score is 10 and your total score is 31
If you hold, you will have 41 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 4
Your turn score is 14 and your total score is 31
If you hold, you will have 45 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 5
Your turn score is 19 and your total score is 31
If you hold, you will have 50 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 3
Your turn score is 22 and your total score is 31
If you hold, you will have 53 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 3
Your turn score is 25 and your total score is 31
If you hold, you will have 56 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 2
Your turn score is 27 and your total score is 31
If you hold, you will have 58 points
Enter 'r' to roll again, 's' to stop.
s
Your score is 58

It is the computer's turn.

The computer rolled: 4
The computer rolled: 4
The computer rolled: 6
The computer rolled: 3
The computer rolled: 3
The computer holds.
The computer's score is 85

You rolled: 5
Your turn score is 5 and your total score is 58
If you hold, you will have 63 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 4
Your turn score is 9 and your total score is 58
If you hold, you will have 67 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 2
Your turn score is 11 and your total score is 58
If you hold, you will have 69 points
Enter 'r' to roll again, 's' to stop.
r
You rolled: 1

It is the computer's turn.

The computer rolled: 6
The computer rolled: 2
The computer rolled: 4
The computer rolled: 1
The computer lost its turn!
The computer's score is 85

You rolled: 4
Your turn score is 4 and your total score is 58
If you hold, you will have 62 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 6
Your turn score is 10 and your total score is 58
If you hold, you will have 68 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 3
Your turn score is 13 and your total score is 58
If you hold, you will have 71 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 5
Your turn score is 18 and your total score is 58
If you hold, you will have 76 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 4
Your turn score is 22 and your total score is 58
If you hold, you will have 80 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 2
Your turn score is 24 and your total score is 58
If you hold, you will have 82 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 6
Your turn score is 30 and your total score is 58
If you hold, you will have 88 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 5
Your turn score is 35 and your total score is 58
If you hold, you will have 93 points
Enter 'r' to roll again, 's' to stop.
r

You rolled: 6
Your turn score is 41 and your total score is 58
If you hold, you will have 99 points
Enter 'r' to roll again, 's' to stop.
s
Your score is 99

It is the computer's turn.

The computer rolled: 5
The computer rolled: 2
The computer rolled: 1
The computer lost its turn!
The computer's score is 85

You rolled: 5
Your turn score is 5 and your total score is 99
If you hold, you will have 104 points
Enter 'r' to roll again, 's' to stop.
s
User Wins !
The User's score is 104

__________Thank You

Add a comment
Know the answer?
Add Answer to:
The game of Pig is a simple two-player dice game in which the first player to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Two player Dice game In C++ The game of ancient game of horse, not the basketball...

    Two player Dice game In C++ The game of ancient game of horse, not the basketball version, is a two player game in which the first player to reach a score of 100 wins. Players take alternating turns. On each player’s turn he/she rolls a six-sided dice. After each roll: a. If the player rolls a 3-6 then he/she can either Roll again or Hold. If the player holds then the player gets all of the points summed up during...

  • The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players...

    The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 (”pig”) is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player’s turn, the player is faced with two decisions: roll If the player rolls a 1: the player scores nothing and it becomes the...

  • please write the following program in Java PIG is one of a family of games called...

    please write the following program in Java PIG is one of a family of games called jeopardy dice games, since a player's main decision after each roll is whether to jeopardize previous gains by trying for potentially even greater gains. In PIG, all players start with ZERO points, and each turn involves rolling a die to earn points. The first player to earn 100 points or more total shouts "PIG!" and wins the game. On their turn, a player does...

  • For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two p...

    For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn,...

  • python code( using functions please)! Rules of the game: - This game requires a dice. The...

    python code( using functions please)! Rules of the game: - This game requires a dice. The goal of the game is to collect the correct number of coins to create a dollar. Players take coins based on a roll of the dice. The numbers on the dice correlate to the coin values as follows:  1—penny  2—nickel  3—dime  4—quarter  5— ( pick a random card from 1 to 4): o 1 = penny o 2 =...

  • can someone help me fix my jeopardy dice game I am having a hard time figuring...

    can someone help me fix my jeopardy dice game I am having a hard time figuring out what i am doing wrong #include #include using namespace std; //this function makes a number between 1 and 6 come out just like a dice int rollDie() { return (rand() % 6+1); } //this function asks the user with a printed statement if they want to roll the dice and gives instructions for yes and no (y/n) void askYoNs(){ cout<<"Do you want to...

  • java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a...

    java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a randomly-chosen player. On their turn a player rolls a single 6-sided die 1 or more times. After each roll, the player has a choice: hold Stop rolling and add the total of al numbers rolled this turn to their score, or roll Roll the die again. If the player rolls a 1, their turn ends and they score 0 for the turn. Points scored...

  • 9. In the casino dice game Craps, players make wagers on a sequence of rolls of...

    9. In the casino dice game Craps, players make wagers on a sequence of rolls of a pair of dice. A sequence of rolls starts with the "shooter" making an initial roll of two dice called the "come-out” roll. If the sum of the dice on the initial roll is 7 or 11 then a player with a bet on the Pass Line wins. If the initial roll results in a sum of 2, 3, or 12 ("craps") then a...

  • // This program creates a simulated player who takes one turn in the // Pig dice...

    // This program creates a simulated player who takes one turn in the // Pig dice game. The simulated player keeps rolling the die until // the total for the turn is 20 or greater, or until a 1 is rolled. // If a 1 is rolled, the player's score for the turn is 0. Otherwise // the player's score is the sum of the rolls for the turn. // ///////////////////////////////////////////////////////////////////// #include<iostream> #include<cstdlib> #include<time.h> using namespace std; int randNumGen(int upper,...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT