Write a program that allows a single Player (the user) to play a simple two dice
ID: 3633627 • Letter: W
Question
Write a program that allows a single Player (the user) to play a simple two dice game of chance against a single Opponent (the computer).Game Description:
-There is a single Player (human), with two dice.
-There is a single Opponent (computer), with two dice.
-Each die is six sided, and each of its sides is labeled with a unique number 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:
1- The computer Opponent rolls it two dice.
2-The dice are displayed.
3-The Player rolls their two dice.
4-The dice are displayed
5-A determination is made as to who won the round, this determination is made via the following rules:
- Larger values beat smaller values, same values are a tie.
-A pair is when both dice have the same number on their top faces, the value of the pair is this common value.
-A non-pair is then when both dice have a different number on their top faces, the major die is the one with the larger value and the minor die the one with the lesser value.
-Any pair beats any non-pair.
-Any two pairs are compared by their respective pair values.
-Any two non-pairs are first compared by their major die values and only in the case of a tie in majors, are compared by their minor die values.
6-The result of the round (with respect to the Player) is announced.
7-The result of the round (with respect to the Player) is tallied, by increasing the number of player wins, ties, or loses by one.
-The player decides if they wish to play another round.
-When the game is over, the results of the game (how many rounds the player won, tied and lost) are announced.
Rules and Requirements:
-You must model a pair of dice as two integers (one for each of the die) named major and minor respectively.
-You must have one pair of die designated for the Opponent and one pair of die designated for the Player.
-The value of the major die must be greater than or equal to the value of the minor die. Each time either or both are modified you must verify this condition and re-order them if necessary.
-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.
// displays a brief accounting of the rules and format of the game
public static void welcome()
-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 range of low to high inclusive.
// returns an int in the range [low, high]
public static int rollDie(int low, int high)
-Given the following method heading, you must write the corresponding definition for a int return method that returns the larger of d1 and d2.
public static int majorDie(int d1, int d2)
-Given the following method heading, you must write the corresponding definition for a int return method that returns the smaller of d1 and d2.
public static int minorDie(int d1, int d2)
-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.
public static void printDice(int d1, int d2)
-Given the following method heading, you must write the corresponding definition for a boolean return method that returns true if and only if d1 == d2.
public static boolean isPair(int d1, int d2)
-Given the following method heading, you must write the corresponding definition for a int return method that determines the result of a single round of the game, (with respect to the Player).
// return: -1 -> Player lost; 0 -> tie; 1 -> Player won
public static int result(int oMajor, int oMinor, int pMajor, int pMinor)
-Given the following function heading, you must write the corresponding definition for a void return method that generates a reasonable report of a game's outcome statistics.
// Report the games outcome statistics
public static void report(int wins, int ties, int loses)
-Your program may use additional methods if desired.
Sample run(s):
miller.cs: Sample9
Welcome to Computer Dice
---------------------------------------------
Your opponent will first roll their two dice.
Next You will roll your two dice.
The outcome of each round will be based on
the values of your dice - high beats low,
pairs beat non-pairs.
---------------------------------------------
Opponent
--------
4 3
Player
--------
4 1
Sorry, you lose!
Do you wish to play again [y, n] : y
Opponent
--------
6 3
Player
--------
5 1
Sorry, you lose!
Do you wish to play again [y, n] : y
Opponent
--------
2 2
Player
--------
4 1
Sorry, you lose!
Do you wish to play again [y, n] : y
Opponent
--------
5 4
Player
--------
5 5
Congrats, you win!
Do you wish to play again [y, n] : y
Opponent
--------
4 3
Player
--------
5 3
Congrats, you win!
Do you wish to play again [y, n] : n
Computer Dice Results
---------------------
You played 5 rounds.
You won 2 rounds.
You tied 0 rounds.
You lost 3 rounds.
---------------------
Explanation / Answer
please rate - thanks
import java.util.*;
public class dice
{public static void main(String [] args)
{Scanner in=new Scanner(System.in);
int pMajor,pMinor,oMajor,oMinor,tp,tc,win=0,lost=0,tie=0,d1,d2,res;
int low=1,high=6;
String yes;
char y;
welcome();
do
{d1=rollDie(low,high);
d2=rollDie(low,high);
oMajor=majorDie(d1,d2);
oMinor=minorDie(d1,d2);
System.out.println("opponent --------");
printDice(oMajor,oMinor);
d1=rollDie(low,high);
d2=rollDie(low,high);
pMajor=majorDie(d1,d2);
pMinor=minorDie(d1,d2);
System.out.println("player ------");
printDice(pMajor,pMinor);
res=result(oMajor,oMinor,pMajor,pMinor);
if(res==0)
{tie++;
System.out.println("It's a tie!");
}
else if(res==1)
{win++;
System.out.println("Congrats, you win!");
}
else
{lost++;
System.out.println("Sorry, you lose!");
}
System.out.print("Do you wish to play again [y, n]: ");
yes=in.next();
y=Character.toUpperCase(yes.charAt(0));
}while(y=='Y');
report(win,tie,lost);
}
public static int result(int oMajor, int oMinor, int pMajor, int pMinor)
{
boolean pPair=isPair(pMajor,pMinor);
boolean oPair=isPair(oMajor,oMinor);
if(pPair)
{if(oPair)
{if(pMajor==oMajor)
return 0;
else if(pMajor>oMajor)
return 1;
else
return -1;
}
else
return 1;
}
else if(oPair)
return -1;
else if(pMajor==oMajor)
{if(pMinor==oMinor)
return 0;
else if(pMinor>oMinor)
return 1;
else
return -1;
}
else if(pMajor>oMajor)
return 1;
return -1;
}
public static void report(int wins, int ties, int loses)
{System.out.println("PLAYER SUMMARY");
System.out.println("Games Won: "+wins);
System.out.println("Games Lost: "+loses);
System.out.println("Games Tied: "+ties);
}
public static boolean isPair(int d1, int d2)
{if(d1==d2)
return true;
return false;
}
public static void printDice(int d1, int d2)
{System.out.println(d1+" "+d2);
}
public static int rollDie(int low, int high)
{int d;
d=(int)(Math.random()*high)+low;
return d;
}
public static int majorDie(int d1, int d2)
{if(d1>d2)
return d1;
return d2;
}
public static int minorDie(int d1, int d2)
{if(d1<d2)
return d1;
return d2;
}
public static void welcome()
{System.out.println("RULES");
System.out.println("- Larger values beat smaller values, same values are a tie.");
System.out.println("-A pair is when both dice have the same number on their top faces, the ");
System.out.println("value of the pair is this common value.");
System.out.println("-A non-pair is then when both dice have a different number on their top");
System.out.println("faces, the major die is the one with the larger value and the minor die the");
System.out.println("one with the lesser value.");
System.out.println("-Any pair beats any non-pair.");
System.out.println("-Any two pairs are compared by their respective pair values.");
System.out.println("-Any two non-pairs are first compared by their major die values and only in");
System.out.println("the case of a tie in majors, are compared by their minor die values.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.