Write a program that allows the user to play multiple rounds of the dice game ``
ID: 3533454 • Letter: W
Question
Write a program that allows the user to play multiple rounds of the dice game ``Mexico" against a computer player. In ``Mexico", each player rolls 2 die, and the numerical values of each die are combined into a two-digit number where the higher valued die is assigned to the tens column and the lower valued die is assigned to the ones column. So, a roll of 4-2 would produce a score of 42, and a roll of 2-4 would also produce a score of 42. Scoring rank is determined as follows (from highest rank to lowest rank):
21 ``Mexico"
pair
highest score
In other words, a score of 21 is the highest rank, followed by 66, 55, ..., 11, 65, 64, ..., 32, 31. Your program should allow the user to play multiple games, and report the number of wins, losses, and ties for the player when he/she decides to quit.
Your program must contain the following programmer defined methods:
public static int rollDie()
This method generates and returns a random die value [1-6].
public static void displayRoll(int r)
This method takes a single die value, and prints the value to the screen in the following format:
Roll: <r>
public static int determineScore(int d1, int d2)
This method takes 2 die values and produces their score based on the rules stated above. For example, if the arguments to determineScore are 2 and 4, this method should return the value 42.
public static int determineWinner(int score1, int score2)
This method takes two scores, and returns 1 if score1 outranks score2, -1 if score2 outranks score1, and 0 if there is a tie. The ranking system is defined above.
Tips and Hints:
Your definition for the determineScore method should determine which parameter holds the larger value, multiply the larger parameter by 10, and add the smaller-valued parameter to that product. If both parameters store the same value, then multiply either of them by 11.
Your main method will be responsible for the following:
Some output statements
Calling and storing the results of 4 calls to the rollDie method
Calling the displayRoll method appropriately
Calling and storing the results of 2 calls to the determineScore method
Calling and storing the result of the determineWinner method
Keeping track of the number of wins, losses, and ties
Using a loop that allows the program to repeat if the user wishes to play again
Sample run(s):
You rolled:
Roll: 3
Roll: 3
Your score: 33
Your opponent rolled:
Roll: 4
Roll: 1
Your opponent's score: 41
You win!
Play again? (y/n): y
You rolled:
Roll: 3
Roll: 1
Your score: 31
Your opponent rolled:
Roll: 5
Roll: 2
Your opponent's score: 52
You lose.
Play again? (y/n): y
You rolled:
Roll: 5
Roll: 3
Your score: 53
Your opponent rolled:
Roll: 2
Roll: 2
Your opponent's score: 22
You lose.
Play again? (y/n): y
You rolled:
Roll: 1
Roll: 2
Your score: 21
Your opponent rolled:
Roll: 4
Roll: 5
Your opponent's score: 54
You win!
Play again? (y/n): n
Number of wins: 2
Number of losses: 2
Number of ties: 0
Explanation / Answer
import java.util.Scanner;
public class MexicoGame {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int d1, d2, d3, d4;
int score1, score2;
int gameResult;
int wins, loses, ties;
String again = "y";
wins = loses = ties = 0;
while (again.compareTo("y") == 0) {
d1 = rollDie();
d2 = rollDie();
d3 = rollDie();
d4 = rollDie();
System.out.println("You rolled:");
displayRoll(d1);
displayRoll(d2);
score1 = determineScore(d1, d2);
System.out.println("Your score: " + score1 + " ");
System.out.println("Your opponent rolled:");
displayRoll(d3);
displayRoll(d4);
score2 = determineScore(d3, d4);
System.out.println("Your opponent's score: " + score2 + " ");
gameResult = determineWinner(score1, score2);
if (gameResult == -1) {
loses++;
System.out.println("You lose.");
}
else if (gameResult == 1) {
wins++;
System.out.println("You win!");
}
else {
ties++;
System.out.println("Tie.");
}
System.out.print("Play again? (y/n): ");
again = keyboard.next();
System.out.println();
}
System.out.println("Number of wins: " + wins);
System.out.println("Number of loses: " + loses);
System.out.println("Number of ties: " + ties);
}
public static int determineScore(int d1, int d2) {
return d1 + d2 + 9 * (d1 > d2 ? d1 : d2);
}
public static void displayRoll(int r) {
System.out.println("Roll: " + r);
}
public static int rollDie() {
return (int)(Math.random() * 6) + 1;
}
public static int determineWinner(int score1, int score2) {
int winner = -1;
//you win via 21
if(score1 == 21 && score2 != 21) {
winner = 1;
}
//you win via pair
else if(score2 != 21 && score1 % 11 == 0 &&
(score2 % 11 != 0 || score1 > score2)) {
winner = 1;
}
//you win via highest non-21, non-pair
else if(score2 != 21 && score2 % 11 != 0 && score1 > score2) {
winner = 1;
}
//tie
else if(score1 == score2) {
winner = 0;
}
return winner;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.