Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3809737 • Letter: T

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

Explanation / Answer

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

__________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote