The game of Pig is a simple twoplayer dice game in which the

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.

Notes

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

static int usersTurn(int oldTotal): user rolls until they either:

hold: the turn total is added to the oldTotal and the sum is returned

roll a 1: the oldTotal is returned

static int computersTurn(int oldTotal): computer rolls until it either:

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

rolls a 1: the oldTotal is returned

static int rollDie(): simulates rolling a die (random number from 1 to 6)

Sample Output (user input shown in red)


Welcome to the game of Pig!

You rolled: 4
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: 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: 4
Your turn score is 14 and your total score is 0
If you hold, you will have 14 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 3
Your turn score is 17 and your total score is 0
If you hold, you will have 17 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 5
Your turn score is 22 and your total score is 0
If you hold, you will have 22 points.
Enter \'r\' to roll again, \'s\' to stop.
s
Your score is 22

It is the computer\'s turn.
The computer rolled: 2
The computer rolled: 5
The computer rolled: 5
The computer rolled: 3
The computer rolled: 2
The computer rolled: 4
The computer holds.
The computer\'s score is 21

You rolled: 1
You lose your turn! Your total is 22
Your score is 22

It is the computer\'s turn.
The computer rolled: 3
The computer rolled: 6
The computer rolled: 3
The computer rolled: 2
The computer rolled: 3
The computer rolled: 1
The computer lost its turn! Computer total is 21
The computer\'s score is 21

You rolled: 3
Your turn score is 3 and your total score is 22
If you hold, you will have 25 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 2
Your turn score is 5 and your total score is 22
If you hold, you will have 27 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 3
Your turn score is 8 and your total score is 22
If you hold, you will have 30 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 3
Your turn score is 11 and your total score is 22
If you hold, you will have 33 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 2
Your turn score is 13 and your total score is 22
If you hold, you will have 35 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 2
Your turn score is 15 and your total score is 22
If you hold, you will have 37 points.
Enter \'r\' to roll again, \'s\' to stop.
s
Your score is 37

It is the computer\'s turn.
The computer rolled: 4
The computer rolled: 6
The computer rolled: 3
The computer rolled: 6
The computer rolled: 6
The computer holds.
The computer\'s score is 46

You rolled: 1
You lose your turn! Your total is 37
Your score is 37

It is the computer\'s turn.
The computer rolled: 5
The computer rolled: 5
The computer rolled: 5
The computer rolled: 2
The computer rolled: 1
The computer lost its turn! Computer total is 46
The computer\'s score is 46

You rolled: 3
Your turn score is 3 and your total score is 37
If you hold, you will have 40 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 3
Your turn score is 6 and your total score is 37
If you hold, you will have 43 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 3
Your turn score is 9 and your total score is 37
If you hold, you will have 46 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 6
Your turn score is 15 and your total score is 37
If you hold, you will have 52 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 5
Your turn score is 20 and your total score is 37
If you hold, you will have 57 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 2
Your turn score is 22 and your total score is 37
If you hold, you will have 59 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 1
You lose your turn! Your total is 37
Your score is 37

It is the computer\'s turn.
The computer rolled: 6
The computer rolled: 2
The computer rolled: 2
The computer rolled: 4
The computer rolled: 1
The computer lost its turn! Computer total is 46
The computer\'s score is 46

You rolled: 4
Your turn score is 4 and your total score is 37
If you hold, you will have 41 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 3
Your turn score is 7 and your total score is 37
If you hold, you will have 44 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 3
Your turn score is 10 and your total score is 37
If you hold, you will have 47 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 5
Your turn score is 15 and your total score is 37
If you hold, you will have 52 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 6
Your turn score is 21 and your total score is 37
If you hold, you will have 58 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 5
Your turn score is 26 and your total score is 37
If you hold, you will have 63 points.
Enter \'r\' to roll again, \'s\' to stop.
s
Your score is 63

It is the computer\'s turn.
The computer rolled: 3
The computer rolled: 5
The computer rolled: 5
The computer rolled: 5
The computer rolled: 2
The computer holds.
The computer\'s score is 66

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

It is the computer\'s turn.
The computer rolled: 6
The computer rolled: 5
The computer rolled: 1
The computer lost its turn! Computer total is 66
The computer\'s score is 66

You rolled: 5
Your turn score is 5 and your total score is 63
If you hold, you will have 68 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 5
Your turn score is 10 and your total score is 63
If you hold, you will have 73 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 4
Your turn score is 14 and your total score is 63
If you hold, you will have 77 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 5
Your turn score is 19 and your total score is 63
If you hold, you will have 82 points.
Enter \'r\' to roll again, \'s\' to stop.
r
You rolled: 2
Your turn score is 21 and your total score is 63
If you hold, you will have 84 points.
Enter \'r\' to roll again, \'s\' to stop.
s
Your score is 84

It is the computer\'s turn.
The computer rolled: 2
The computer rolled: 3
The computer rolled: 6
The computer rolled: 6
The computer rolled: 2
The computer rolled: 3
The computer holds.
The computer\'s score is 88

You rolled: 1
You lose your turn! Your total is 84
Your score is 84

It is the computer\'s turn.
The computer rolled: 6
The computer rolled: 4
The computer rolled: 4
The computer holds.
The computer\'s score is 102

THE COMPUTER WINS!

Assignment Grading:

Contract: 10%

Purpose Statement: 10%

Examples: 10%

Algorithm: 10

Program: 60%:

Functionality: 50%

Code Formatting: 10%

CPS JAVA I NEED THE CODE

Solution

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(\"\ You 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(\"\ It is the computer\'s turn.\ \");
       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

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site