Java programming Make sure to use the main topics Write a program that allows a
ID: 653749 • Letter: J
Question
Java programming Make sure to use the main topicsWrite a program that allows a single Player (the user) to play a simple two dice game of chance against "The Odds. Game Description: There is a single player, with two six sided die. The sides of each die are labeled with the numbers from 1 to 6, we will call this the value of the die. A game is made up of rounds, a single round is played as such: The player rolls their two dice. The dice are displayed, in some reasonable format. A determination is made as to whether or not the player won the round, this determination is made via the following rules: A pair is when both dice have the same number on their top faces. If the player has any pair then they win the round. A nonpair is then when both dice have a different number on their top faces. If the player has any nonpair then they lose the round. The result of the round (with respect to the Player) is reported. The player is asked if they wish to play another round. Once the player indicates that they do not wish to play another round: Before exiting, the program displays a short report stating how many rounds were played, of those - how many were won and how many were lost. Rules and Requirements: All user input must be validated (user validation loop) in a reasonable manner. Given the following method heading, you must write the corresponding definition for a void return method that displays a brief accounting of the rules and format of your game. Given the following method heading, you must write the corresponding definition for a int return method that generates and returns a random number in the Integer range of min to max inclusive. Given the following method heading, you must write the corresponding definition for a void return method that prints the pair of dice (d1 and d2) to the screen, in some reasonable report format. Given the following method heading, you must write the corresponding definition for a boolean return method that returns true if and only if dl = d2. Given the following function heading, you must write the corresponding definition for a void return method that generates a reasonable report of a games outcomes. Your program may use additional methods if desired. Welcome to Computer Dice You will first roll your dice Next the outcome of your roll will be determined: any pair and you Win anything else and you Lose Do you wish to play again [y, n] : 5 Do you wish to play again [y, n] : yes Do you wish to play again [y, n] : Sorry, you lose! Do you wish to play again [y, n] : y Congrats, you win! Do you wish to play again [y, n] : n Computer Dice Results
Explanation / Answer
import java.util.*;
public class Dice{
public static void main(String[] args){
int a, b, win = 0, lose = 0, play = 0;
String choice;
Random dice = new Random();
Scanner in = new Scanner(System.in);
System.out.println("Welcome to Computer Dice");
System.out.println("-------------------------------------");
System.out.println("Next the outcome of your roll will be determined:");
System.out.println("any Pair and you Win");
System.out.println("any Straight and you just roll again");
System.out.println("anything else and you Lose");
System.out.println("-------------------------------------");
while(true){
play++;
System.out.println("Player --------");
a = 1 + dice.nextInt(6);
b = 1 + dice.nextInt(6);
System.out.println(a + " " + b);
if(a == b){
win++;
System.out.println("Congrats, you win!");
}
else if(Math.abs(a - b) == 1){
System.out.println("Its a tie!");
}
else{
lose++;
System.out.println("Sorry, you lose!");
}
System.out.print("Do you wish to play again [y, n] : ");
choice = in.next();
if(choice.equals("y")) continue;
else if(choice.equals("n")){
System.out.println("Computer Dice Results ---------------------");
System.out.println("You played " + play + " rounds");
System.out.println("Rounds won : " + win + " Rounds lost : " + lose);
System.out.println("---------------------");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.