This is a atwo part question: a. Using the Die class you created in Chapter 4, c
ID: 3668977 • Letter: T
Question
This is a atwo part question:
a. Using the Die class you created in Chapter 4, create a version of the dice game Pig that a user can play against the computer. The object of the game is to be the first to score 100 points. The user and computer take turns rolling a pair of dice following these rules:
On a turn, each player "rolls" two dice. If no 1 appears, the dice values are added to a runing total, and the player can choose whether to roll again or pass the turn to the other player.
If a 1 appears on one of the dice, nothing more is added to the player's total and it becomes the other player's turn.
If a 1 appears on both of the dice, not only is the player's turn over, but the player's entire acucmulated score is reset to zero.
In this version of the game, when the computer does not roll a 1 and can choose whether to roll again, generate a random value between 0 and 1. Have the computer continue to roll the dice when the value is 0.5 or more, and have the compter quit and pass the turn to the player when the value is not 0.5 or more.
Save the game as PigDiceGame.java.
b. Modify the PigDiceGame application so that if a player rolls a 1, not only does the player's turn end, but all the player's earned points during that round are elminiated. (Points from earlier rounds are not affected. That is, when either the player or computer rolls a 1, all the points accumulated since the other's turn are subtracted.) Save the game as PigDiceGame2.java.
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class PigDiceGame {
private static int getRandom(){
Random rn = new Random();
int maximum = 6, minimum = 1;
int n = maximum - minimum + 1;
int i = rn.nextInt() % n;
return minimum + i;
}
public static void pigiGame(){
int userScore = 0;
int computerScore = 0;
Scanner sc = new Scanner(System.in);
char c = 'n';
while(userScore < 100 && computerScore < 100){
int num1=0,num2=0;
num1 = getRandom();
num2 = getRandom();
//userTurn
while(num1!=1 && num2!=1){
userScore +=(num1+num2);
if(userScore >= 100)
break;
System.out.println("Do you want to continue...(y/n)");
c = sc.next().charAt(0);
if(c=='n')
break;
num1 = getRandom();
num2 = getRandom();
}
if(userScore >=100) // checking user total after its turn over
break;
if(num1==1 && num2 ==1) // reseting user total of 1 comes on both dice
userScore = 0;
System.out.println("User score : "+userScore);
//Computer turn
num1 = getRandom();
num2 = getRandom();
while(num1!=1 && num2!=1){
computerScore +=(num1+num2);
if(computerScore >= 100)
break;
double d = Math.random();
if(d < 0.5)
break;
num1 = getRandom();
num2 = getRandom();
}
if(computerScore >=100) // checking user total after its turn over
break;
System.out.println("Computer score : "+computerScore);
}
if(userScore >= 100)
System.out.println("User Won!!!!");
else if(computerScore >= 100)
System.out.println("Computer WOn!!!!!!");
}
public static void main(String[] args) {
pigiGame();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.