import java.util.Random; import java.util.Scanner; public class PigGame { boolea
ID: 3883231 • Letter: I
Question
import java.util.Random;
import java.util.Scanner;
public class PigGame {
boolean isComputer;
int humanScore; // total score so far for all turns
int computerScore;
int turnScore; // score for this turn
int rollDice() //generates a random number between 2-6
{
Random rand = new Random();
return rand.nextInt(6-1)+1;
}
PigGame() // default constructor
{
isComputer=false;
humanScore=0; // total score so far for all turns
turnScore=0;
computerScore=0;
}
void playGame() //performs the task of a player's turn.
{
boolean gameOver=false;
Scanner sc=new Scanner(System.in);
System.out.println("Welcome to the game of Pig! ");
while (!gameOver)//game is not over
{
// human’s turn
turnScore=0;
boolean turnOver=false;
System.out.println();
do
{
//roll dice
int dice=rollDice();
System.out.println("You rolled: "+dice);
if (dice == 1)
{
System.out.println("You lose your turn! your total is "+humanScore);
//set turn over to true and exit do-while loop
turnOver=true;
}
else
{
//accumulate turn score
turnScore+=dice;
//display current turn score and total score
System.out.println("Your turn score is "+ turnScore +" and your total score is "+humanScore);
System.out.println("If you hold, you will have "+turnScore+" points.");
//ask user’s input whether to roll again or hold?
System.out.println("Enter 'r' to roll again, 'h' to hold.");
char choice=sc.next().charAt(0);
if (choice == 'h')
//set turn over to true and exit do-while loop
turnOver=true;
}
} while (!turnOver);
//add in any points the human got and output current total
humanScore+=turnScore;
System.out.println("The Human's score is "+humanScore);
//check for human winning
if (humanScore >= 100)
{
System.out.println("YOU WIN!");
//set game over to true and exit the while loop
gameOver=true;
}
// computer’s turn
turnScore=0;
turnOver=false;
System.out.println(" It is the computer's turn. ");
do
{
int dice=rollDice();
System.out.println("The computer rolled: "+dice);
if (dice == 1)
{
System.out.println("The computer lost its turn!Computer total is " +turnScore);
// and output total
//set turn over to true and exit do-while loop
turnOver=true;//and set turn over true
break;
}
else
{
//accumulate turn score
turnScore+=dice;
//turn score >= 20 or total plus turn score >= 100
if (turnScore>=20||turnScore>=100){
System.out.println("The computer holds");
turnOver=true;//and set turn over true
}
}
} while (!turnOver);//turn is not over
///add in any points the computer got and output current total
computerScore+=turnScore;
System.out.println("The computer's score is "+computerScore);
//check for computer winning
if (computerScore >= 100)
{
System.out.println("THE COMPUTER WINS!");
//set game over to true and exit the while loop
gameOver=true;
} ------------ Getting error here please solve --------------------------------------- (
Multiple markers at this line
- Syntax error, insert "else Statement" to complete
IfStatement
- Syntax error, insert "}" to complete Block )
}
}
public static void main(String[] args) {
PigGame gop=new PigGame();
gop.playGame();//call play game method
}
}
Explanation / Answer
NOte: some logic is wrong..So I changes that.only when the computer or human holds the turn score have to be added to their corresponding total score.If they lost by throwing dice 1 then tuen score is 0.So their corresponding score will remain same.But In this case also u added that turn score to total score.So I modified that.Now working fine perfectly...If u have any further doubts regarding this just giveme comment...Now could u plz rate me well.Thank you.
_______________
PigGame.java
import java.util.Random;
import java.util.Scanner;
public class PigGame {
boolean isComputer;
int humanScore; // total score so far for all turns
int computerScore;
int turnScore; // score for this turn
int rollDice() // generates a random number between 2-6
{
Random rand = new Random();
return rand.nextInt(6 - 1) + 1;
}
PigGame() // default constructor
{
isComputer = false;
humanScore = 0; // total score so far for all turns
turnScore = 0;
computerScore = 0;
}
void playGame() // performs the task of a player's turn.
{
boolean gameOver = false;
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the game of Pig! ");
while (!gameOver) // game is not over
{
// human’s turn
turnScore = 0;
boolean turnOver = false;
System.out.println();
do {
// roll dice
int dice = rollDice();
System.out.println("You rolled: " + dice);
if (dice == 1) {
turnScore = 0;
System.out.println("You lose your turn! your total is " + humanScore);
// set turn over to true and exit do-while loop
turnOver = true;
} else {
// accumulate turn score
turnScore += dice;
// display current turn score and total score
System.out.println("Your turn score is " + turnScore + " and your total score is " + humanScore);
System.out.println("If you hold, you will have " + turnScore + " points.");
// ask user’s input whether to roll again or hold?
System.out.println("Enter 'r' to roll again, 'h' to hold.");
char choice = sc.next().charAt(0);
if (choice == 'h') {
humanScore += turnScore;
// set turn over to true and exit do-while loop
turnOver = true;
}
}
} while (!turnOver);
// add in any points the human got and output current total
//humanScore += turnScore;
System.out.println("The Human's score is " + humanScore);
// check for human winning
if (humanScore >= 100) {
System.out.println("YOU WIN!");
// set game over to true and exit the while loop
gameOver = true;
break;
}
// computer’s turn
turnScore = 0;
turnOver = false;
System.out.println(" It is the computer's turn. ");
do {
int dice = rollDice();
System.out.println("The computer rolled: " + dice);
if (dice == 1) {
turnScore = 0;
System.out.println("The computer lost its turn! Computer total is " + computerScore);
// and output total
// set turn over to true and exit do-while loop
turnOver = true; // and set turn over true
break;
} else {
// accumulate turn score
turnScore += dice;
// turn score >= 20 or total plus turn score >= 100
if (turnScore >= 20 || turnScore >= 100) {
System.out.println("The computer holds");
// /add in any points the computer got and output current total
computerScore += turnScore;
turnOver = true; // and set turn over true
}
}
} while (!turnOver); // turn is not over
System.out.println("The computer's score is " + computerScore);
// check for computer winning
if (computerScore >= 100) {
System.out.println("THE COMPUTER WINS!");
// set game over to true and exit the while loop
gameOver = true;
}
}
}
public static void main(String[] args) {
PigGame gop = new PigGame();
gop.playGame(); // call play game method
}
}
_________________
Output:
Welcome to the game of Pig!
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, 'h' to hold.
r
You rolled: 3
Your turn score is 8 and your total score is 0
If you hold, you will have 8 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 2
Your turn score is 10 and your total score is 0
If you hold, you will have 10 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 3
Your turn score is 13 and your total score is 0
If you hold, you will have 13 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 2
Your turn score is 15 and your total score is 0
If you hold, you will have 15 points.
Enter 'r' to roll again, 'h' to hold.
h
The Human's score is 15
It is the computer's turn.
The computer rolled: 4
The computer rolled: 2
The computer rolled: 1
The computer lost its turn! Computer total is 0
The computer's score is 0
You rolled: 5
Your turn score is 5 and your total score is 15
If you hold, you will have 5 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 5
Your turn score is 10 and your total score is 15
If you hold, you will have 10 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 14 and your total score is 15
If you hold, you will have 14 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 5
Your turn score is 19 and your total score is 15
If you hold, you will have 19 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 5
Your turn score is 24 and your total score is 15
If you hold, you will have 24 points.
Enter 'r' to roll again, 'h' to hold.
h
The Human's score is 39
It is the computer's turn.
The computer rolled: 4
The computer rolled: 1
The computer lost its turn! Computer total is 0
The computer's score is 0
You rolled: 1
You lose your turn! your total is 39
The Human's score is 39
It is the computer's turn.
The computer rolled: 5
The computer rolled: 4
The computer rolled: 2
The computer rolled: 4
The computer rolled: 3
The computer rolled: 2
The computer holds
The computer's score is 20
You rolled: 4
Your turn score is 4 and your total score is 39
If you hold, you will have 4 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 1
You lose your turn! your total is 39
The Human's score is 39
It is the computer's turn.
The computer rolled: 5
The computer rolled: 2
The computer rolled: 3
The computer rolled: 1
The computer lost its turn! Computer total is 20
The computer's score is 20
You rolled: 4
Your turn score is 4 and your total score is 39
If you hold, you will have 4 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 1
You lose your turn! your total is 39
The Human's score is 39
It is the computer's turn.
The computer rolled: 1
The computer lost its turn! Computer total is 20
The computer's score is 20
You rolled: 2
Your turn score is 2 and your total score is 39
If you hold, you will have 2 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 1
You lose your turn! your total is 39
The Human's score is 39
It is the computer's turn.
The computer rolled: 5
The computer rolled: 4
The computer rolled: 5
The computer rolled: 1
The computer lost its turn! Computer total is 20
The computer's score is 20
You rolled: 3
Your turn score is 3 and your total score is 39
If you hold, you will have 3 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 2
Your turn score is 5 and your total score is 39
If you hold, you will have 5 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 2
Your turn score is 7 and your total score is 39
If you hold, you will have 7 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 11 and your total score is 39
If you hold, you will have 11 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 15 and your total score is 39
If you hold, you will have 15 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 19 and your total score is 39
If you hold, you will have 19 points.
Enter 'r' to roll again, 'h' to hold.
h
The Human's score is 58
It is the computer's turn.
The computer rolled: 2
The computer rolled: 5
The computer rolled: 5
The computer rolled: 4
The computer rolled: 1
The computer lost its turn! Computer total is 20
The computer's score is 20
You rolled: 3
Your turn score is 3 and your total score is 58
If you hold, you will have 3 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 2
Your turn score is 5 and your total score is 58
If you hold, you will have 5 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 3
Your turn score is 8 and your total score is 58
If you hold, you will have 8 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 12 and your total score is 58
If you hold, you will have 12 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 16 and your total score is 58
If you hold, you will have 16 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 20 and your total score is 58
If you hold, you will have 20 points.
Enter 'r' to roll again, 'h' to hold.
h
The Human's score is 78
It is the computer's turn.
The computer rolled: 5
The computer rolled: 3
The computer rolled: 3
The computer rolled: 3
The computer rolled: 2
The computer rolled: 3
The computer rolled: 1
The computer lost its turn! Computer total is 20
The computer's score is 20
You rolled: 2
Your turn score is 2 and your total score is 78
If you hold, you will have 2 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 2
Your turn score is 4 and your total score is 78
If you hold, you will have 4 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 5
Your turn score is 9 and your total score is 78
If you hold, you will have 9 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 3
Your turn score is 12 and your total score is 78
If you hold, you will have 12 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 16 and your total score is 78
If you hold, you will have 16 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 20 and your total score is 78
If you hold, you will have 20 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 3
Your turn score is 23 and your total score is 78
If you hold, you will have 23 points.
Enter 'r' to roll again, 'h' to hold.
h
The Human's score is 101
YOU WIN!
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.