PLEASE HELP ME WITH THIS PROJECT.
For this lab you will write a Java program that plays the game Poker Dice. In this game, five dice are rolled and scored as if they were a hand of playing cards. The game goes through two separate phases. In the first phase, a "hand" of dice is presented to the player. The player then selects which dice he wants to keep and which he wants to re-roll. Once that decision is finished, all of the dice that the player has marked to re-roll are re-rolled and the final "hand" of dice is displayed. The hand should then be scored according to the rules of Poker Dice (given below) and the result displayed on the screen.
Scoring Poker Dice The following table shows the values of different Poker Dice hands in order (i.e. Five of a Kind beats Four of a Kind, Four of a Kind beats Full House, etc.):
Hand Description Examples
-------------- ------------------------------------- --------------------
Five of a kind All five dice show the same value [1, 1, 1, 1, 1]
[5, 5, 5, 5, 5]
Four of a kind Four of the five dice show the same [1, 2, 1, 1, 1]
value [4, 4, 4, 3, 4]
Full house Three of the five dice show the same [1, 2, 2, 1, 1]
value, the other two show a different [3, 3, 3, 5, 5]
matched value
Three of a kind Three of the five dice show the same [1, 2, 3, 1, 1]
value, the other two show different [3, 3, 3, 2, 5]
values
Straight All five dice together show a sequence [2, 3, 1, 5, 4]
of values 1-5 or 2-6. [6, 3, 2, 4, 5]
Two pair Two of the five dice show the same [1, 2, 2, 1, 3]
value, and two other dice show a [3, 5, 6, 3, 6]
different shared value
One pair Two of the five dice show the same [1, 2, 2, 3, 4]
value, the other dice show different [3, 5, 6, 3, 1]
values
Highest value X If none of the above hands exist, then [1, 2, 3, 5, 6] - Highest value 6
the score for the hand is [6, 2, 1, 4, 5] - Highest value 6
"Highest value X" where X is the
highest value in the list
Note that for scoring, only the highest score is reported. So a hand like [5, 5, 5, 5, 5] should only be reported as "Five of a kind" even though it also fits the definition of "Four of a kind" and "Two Pair" etc. The idea behind scoring is that the hand is scored with only the best possible result, and scores are showed in descending order in the table above.
In order to build this program you will need to write a number of methods. The methods are listed below in the skeleton code that you must use to build your program, and details of how the methods should be implemented are in the comments for the methods in the code skeleton below. The methods that you will write are:
promptToPlay - a method that prompts the user with a message and returns back true if they enter a 'Y' and false otherwise. See the transcript below for the messages you will need to display with this method, including related error messages.
rollDice - a method that takes a random number generator and an array and replaces all of the zeroes in the array with random numbers between 1 and 6 taken from the generator. Then sorts the array so that the values in it are in ascending order. HINT Don't try to write your own sorting logic, use Arrays.sort() to sort your array.
promptForReroll - a method that takes an array and a Scanner and prompts the user for the indexes of the array that they want to re-roll, checking to make sure the indexes given are within the bounds of the array, and setting the values at those indexes to 0 so they can be rerolled by passing the array to rollDice. See the transcript below for the messages you will need to display with this method, including related error messages.
getCounts - a method that takes an array and counts how often each of the values 1 through 6 appear within it, returning a new array containing those counts. (This method should be used to determine the ranks of most of your hands - for example, a "Four of a kind" would have one position in the counts array with a 4 in it, a "Full house" would have one position with a 3 and one position with a 2, etc.)
inSequence - a method that takes a sorted array of integer and returns true if the numbers in the array represent an ascending sequence and false otherwise (use this method to determine if you have a Straight or not).
contains - a method that takes an array (not necessarily sorted) and a value and returns true if the value appears in the array at least once, false otherwise. (Think about how you can use this method with the array returned by getCounts to determine if you have a particular score or not. You should use this method to help you write the scoreDice method.)
scoreDice - a method that takes an array of dice in sorted order and returns a String representing the score of the dice as given in the table above. Note that every array of dice has a score - if none of the named scores apply to it, then it's score will be "Highest value X" where X is the value of largest element in the dice array.
main - the method that will put all of these pieces together into one program to play the game.
There are unit tests for each of these methods. You should write each method separately and test it to make sure it works. Only once you have written and tested all of the other methods should you start in on the main method that uses them together to play a game of Poker Dice. WRITE YOUR CODE INCREMENTALLY. Write one method and test it to make sure it works before moving on to the next method. Incremental programming is your friend!
The following transcript shows a game where multiple hands are scored - remember that elements in BOLD are user input. Your program, if built correctly, will be able to exactly replicate this transcript when the value of 33 is selected as the random seed.
Enter a random seed: 33 Welcome to the Poker Dice game! Roll 5 dice and try to assemble the best poker hand. Would you like to play [Y/N]? y Your current roll: [4, 4, 5, 6, 6] Select a die to re-roll (-1 to keep remaining dice): 0 Your current roll: [0, 4, 5, 6, 6] Select a die to re-roll (-1 to keep remaining dice): 4 Your current roll: [0, 4, 5, 6, 0] Select a die to re-roll (-1 to keep remaining dice): -1 Keeping remaining dice... Rerolling... Your final roll: [2, 2, 4, 5, 6] One pair Would you like to play again [Y/N]? Y Your current roll: [1, 3, 3, 3, 6] Select a die to re-roll (-1 to keep remaining dice): 5 Error: Index must be between 0 and 4 Your current roll: [1, 3, 3, 3, 6] Select a die to re-roll (-1 to keep remaining dice): -2 Error: Index must be between 0 and 4 Your current roll: [1, 3, 3, 3, 6] Select a die to re-roll (-1 to keep remaining dice): 0 Your current roll: [0, 3, 3, 3, 6] Select a die to re-roll (-1 to keep remaining dice): -1 Keeping remaining dice... Rerolling... Your final roll: [3, 3, 3, 5, 6] Three of a kind Would you like to play again [Y/N]? sfjdl ERROR! Only 'Y' and 'N' allowed as input! Would you like to play again [Y/N]? yes ERROR! Only 'Y' and 'N' allowed as input! Would you like to play again [Y/N]? y Your current roll: [1, 3, 4, 5, 5] Select a die to re-roll (-1 to keep remaining dice): 2 Your current roll: [1, 3, 0, 5, 5] Select a die to re-roll (-1 to keep remaining dice): 3 Your current roll: [1, 3, 0, 0, 5] Select a die to re-roll (-1 to keep remaining dice): 4 Your current roll: [1, 3, 0, 0, 0] Select a die to re-roll (-1 to keep remaining dice): -1 Keeping remaining dice... Rerolling... Your final roll: [1, 1, 3, 5, 6] One pair Would you like to play again [Y/N]? y Your current roll: [1, 1, 1, 3, 5] Select a die to re-roll (-1 to keep remaining dice): 0 Your current roll: [0, 1, 1, 3, 5] Select a die to re-roll (-1 to keep remaining dice): 1 Your current roll: [0, 0, 1, 3, 5] Select a die to re-roll (-1 to keep remaining dice): 2 Your current roll: [0, 0, 0, 3, 5] Select a die to re-roll (-1 to keep remaining dice): -1 Keeping remaining dice... Rerolling... Your final roll: [2, 3, 4, 5, 6] Straight Would you like to play again [Y/N]? y Your current roll: [3, 3, 4, 4, 6] Select a die to re-roll (-1 to keep remaining dice): 0 Your current roll: [0, 3, 4, 4, 6] Select a die to re-roll (-1 to keep remaining dice): 1 Your current roll: [0, 0, 4, 4, 6] Select a die to re-roll (-1 to keep remaining dice): 2 Your current roll: [0, 0, 0, 4, 6] Select a die to re-roll (-1 to keep remaining dice): 3 Your current roll: [0, 0, 0, 0, 6] Select a die to re-roll (-1 to keep remaining dice): 4 Your current roll: [0, 0, 0, 0, 0] Select a die to re-roll (-1 to keep remaining dice): -1 Keeping remaining dice... Rerolling... Your final roll: [2, 3, 4, 4, 6] One pair Would you like to play again [Y/N]? n Goodbye!
For this assignment you must start with the following "skeleton" of Java code. Create a project and a class named PokerDice.java and then paste this skeleton into your code to work with. Feel free to add any additional methods you find useful, but make sure that you add comments indicating what they do following the form of the rest of the comments in the code.
/*
* A program that plays and scores a round of the game Poker Dice. In this game,
* five dice are rolled. The player is allowed to select a number of those five dice
* to re-roll. The dice are re-rolled and then scored as if they were a poker hand.
* The following hands MUST be correctly scored in this assignment to receive
* full credit:
* * Highest value x
* * One pair
* * Two pair
* * Three of a kind
* * Straight
* * Full house
* * Four of a kind
* * Five of a kind
*
* @author ENTER YOUR NAME HERE
* @version THE DATE HERE
*
*/
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class PokerDice {
/**
* Prompts the user with a message passed into method and returns true if
* the user enters a 'Y' and false if the user enters an 'N'. The method
* should work if the user uses upper or lower case inputs. If the user
* enters any other value, the method should display an error message and
* continue prompting until a valid value is entered.
*
* @param inScanner
* Scanner to provide in put from user
* @param msg
* message to display to prompt the user to enter a value
* @return
*/
public static boolean promptToPlay(Scanner inScanner, String msg) {
// TODO - complete this method
// TODO - the following line is only here to allow this program to
// compile. Replace it and remove this comment when you complete
// this method.
return false;
}
/**
* Replaces the zeroes in the array dice with random values between 1 and 6
* chosen from the Random generator rnd. Then sorts the array so that the
* values are in sorted order.
*
* @param rnd
* generator to pull values from
* @param dice
* array to fill with values.
*/
public static void rollDice(Random rnd, int[] dice) {
// TODO - complete this method
}
/**
* Displays the array dice as the user's current set of die rolls and
* prompts them to indicate the indices of dice to be re-rolled, one index
* at a time. If the user enters a -1, the loop ends. If the user enters an
* invalid index other than -1, the method displays an error message and
* continues prompting until a valid index or -1 is entered.
*
* @param dice
* array to set dice to be rerolled
* @param inScanner
* Scanner to provide input from user
*/
public static void promptForReroll(int[] dice, Scanner inScanner) {
// TODO - complete this method
}
/**
* Returns an array where each position represents the counts of a value in
* the array dice. For example, if dice is [1, 2, 3, 3, 5], then this method
* would return [1, 1, 2, 0, 1, 0] - where index 0 holds the number of 1s,
* index 1 holds the number of 2s, index 2 holds the number of 3s, etc.
*
* @param dice
* die rolls to count
* @return array holding counts of each value in the array dice
*/
public static int[] getCounts(int[] dice) {
// TODO - complete this method
// TODO - the following line is only here to allow this program to
// compile. Replace it and remove this comment when you complete
// this method.
return new int[0];
}
/**
* Given a SORTED array in increasing order, returns true if the numbers
* form an unbroken sequence, false otherwise. For example, [1,2,3,4,5]
* would be true, but [1,2,3,4,6] would be false.
*
* @param dice
* array sorted in increasing order
* @return true if values in dice are in an unbroken sequence, false
* otherwise
*/
public static boolean inSequence(int[] dice) {
// TODO - complete this method
// TODO - the following line is only here to allow this program to
// compile. Replace it and remove this comment when you complete
// this method.
return false;
}
/**
* Returns true if value is somewhere in the array, false otherwise
*
* @param array
* array to search for value
* @param value
* item to look for in the array
* @return true if value is in array, otherwise false
*/
public static boolean contains(int[] array, int value) {
// TODO - complete this method
// TODO - the following line is only here to allow this program to
// compile. Replace it and remove this comment when you complete
// this method.
return false;
}
/**
* Scores the value of the array of dice as a poker hand. Returns a String
* giving the rank of the hand. See the assignment write-up for details on
* how the rank string should be formatted.
*
* @param dice
* die rolls in sorted order
* @return String holding the rank of the roll in the array dice
*/
public static String scoreDice(int[] dice) {
// TODO - complete this method
// TODO - the following line is only here to allow this program to
// compile. Replace it and remove this comment when you complete
// this method.
return "METHOD NOT IMPLEMENTED YET";
}
/**
*
* @param args
*/
public static void main(String[] args) {
// TODO - complete this method
}
}
/*
* A program that plays and scores a round of the game Poker Dice.
In this game,
* five dice are rolled. The player is allowed to select a number of
those five dice
* to re-roll. The dice are re-rolled and then scored as if they
were a poker hand.
* The following hands MUST be correctly scored in this assignment
to receive
* full credit:
* * Highest value x
* * One pair
* * Two pair
* * Three of a kind
* * Straight
* * Full house
* * Four of a kind
* * Five of a kind
*
* @author ENTER YOUR NAME HERE
* @version THE DATE HERE
*
*/
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class PokerDice {
/**
* Prompts the user with a message passed into method and returns
true if
* the user enters a 'Y' and false if the user enters an 'N'. The
method
* should work if the user uses upper or lower case inputs. If the
user
* enters any other value, the method should display an error
message and
* continue prompting until a valid value is entered.
*
* @param inScanner
* Scanner to provide in put from user
* @param msg
* message to display to prompt the user to enter a value
* @return
*/
public static boolean promptToPlay(Scanner inScanner, String msg)
{
// TODO - complete this method
// TODO - the following line is only here to allow this program
to
// compile. Replace it and remove this comment when you
complete
// this method.
String choice;
System.out.print(msg);
choice = inScanner.nextLine();
while(!choice.equalsIgnoreCase("y") &&
!choice.equalsIgnoreCase("n"))
{
System.out.println("ERROR! Only 'Y'
and 'N' allowed as input!");
System.out.print(msg);
choice = inScanner.nextLine();
}
System.out.println();
if(choice.equalsIgnoreCase("y"))
return true;
return false;
}
/**
* Replaces the zeroes in the array dice with random values between
1 and 6
* chosen from the Random generator rnd. Then sorts the array so
that the
* values are in sorted order.
*
* @param rnd
* generator to pull values from
* @param dice
* array to fill with values.
*/
public static void rollDice(Random rnd, int[] dice) {
// TODO - complete this method
for(int i=0;i<dice.length;i++)
{
if(dice[i] == 0)
dice[i] =
rnd.nextInt(6)+1;
}
Arrays.sort(dice);
}
/**
* Displays the array dice as the user's current set of die rolls
and
* prompts them to indicate the indices of dice to be re-rolled, one
index
* at a time. If the user enters a -1, the loop ends. If the user
enters an
* invalid index other than -1, the method displays an error message
and
* continues prompting until a valid index or -1 is entered.
*
* @param dice
* array to set dice to be rerolled
* @param inScanner
* Scanner to provide input from user
*/
public static void promptForReroll(int[] dice, Scanner inScanner)
{
// TODO - complete this method
int index;
boolean reRoll = false;
while(true)
{
System.out.println("Your current
roll: "+Arrays.toString(dice));
System.out.print("Select a die to
re-roll (-1 to keep remaining dice): ");
index = inScanner.nextInt();
if(index >=0 && index
<dice.length)
{
dice[index] =
0;
reRoll =
true;
}
else if(index == -1)
break;
else
System.out.println("Error: Index must be between 0 and
"+(dice.length-1));
}
System.out.println("Keeping remaining dice...");
if(reRoll)
System.out.println("Rerolling...");
inScanner.nextLine();
}
/**
* Returns an array where each position represents the counts of a
value in
* the array dice. For example, if dice is [1, 2, 3, 3, 5], then
this method
* would return [1, 1, 2, 0, 1, 0] - where index 0 holds the number
of 1s,
* index 1 holds the number of 2s, index 2 holds the number of 3s,
etc.
*
* @param dice
* die rolls to count
* @return array holding counts of each value in the array
dice
*/
public static int[] getCounts(int[] dice) {
// TODO - complete this method
// TODO - the following line is only here to allow this program
to
// compile. Replace it and remove this comment when you
complete
// this method.
int counts[] = new int[6]; // 0- count for 1, 1-count
for 2,....
for(int i=0;i<counts.length;i++)
counts[i] = 0;
for(int i=0;i<dice.length;i++)
{
counts[dice[i]-1]++;
}
return counts;
}
/**
* Given a SORTED array in increasing order, returns true if the
numbers
* form an unbroken sequence, false otherwise. For example,
[1,2,3,4,5]
* would be true, but [1,2,3,4,6] would be false.
*
* @param dice
* array sorted in increasing order
* @return true if values in dice are in an unbroken sequence,
false
* otherwise
*/
public static boolean inSequence(int[] dice) {
// TODO - complete this method
// TODO - the following line is only here to allow this program
to
// compile. Replace it and remove this comment when you
complete
// this method.
for(int i=0;i<dice.length-1;i++)
if(dice[i] != (dice[i+1]-1))
return false;
return true;
}
/**
* Returns true if value is somewhere in the array, false
otherwise
*
* @param array
* array to search for value
* @param value
* item to look for in the array
* @return true if value is in array, otherwise false
*/
public static boolean contains(int[] array, int value) {
// TODO - complete this method
// TODO - the following line is only here to allow this program
to
// compile. Replace it and remove this comment when you
complete
// this method.
for(int i=0;i<array.length;i++)
if(array[i] == value)
return
true;
return false;
}
/**
* Scores the value of the array of dice as a poker hand. Returns a
String
* giving the rank of the hand. See the assignment write-up for
details on
* how the rank string should be formatted.
*
* @param dice
* die rolls in sorted order
* @return String holding the rank of the roll in the array
dice
*/
public static String scoreDice(int[] dice) {
// TODO - complete this method
// TODO - the following line is only here to allow this program
to
// compile. Replace it and remove this comment when you
complete
// this method.
int counts[] = getCounts(dice); // get counts of the
values of the dice
// check for five of a kind
for(int i=0;i<counts.length;i++)
if(counts[i] == 5)
return "Five of
a kind";
//check for four of a kind
for(int i=0;i<counts.length;i++)
if(counts[i] == 4)
return "Four of
a kind";
//check for full house
boolean threeOfaKind =false, twoOfaKind = false;
for(int i=0;i<counts.length;i++)
{
if(counts[i] == 3)
threeOfaKind =
true;
else if(counts[i] == 2)
twoOfaKind =
true;
}
if(threeOfaKind && twoOfaKind)
return "Full house";
//check for three of a kind
if(threeOfaKind)
return "Three of a Kind";
//check for straight
//Arrays.sort(dice);
if(inSequence(dice))
return "Straight";
// check for twoPairs and onePair
int pairs = 0;
for(int i=0;i<counts.length;i++)
if(counts[i] == 2)
pairs++;
if(pairs == 2)
return "Two Pair";
else if(pairs == 1)
return "One pair";
// check for highest value
for(int i=6;i>0;i--)
if(contains(dice,i))
return "Highest
value "+i;
return null;
}
/**
*
* @param args
*/
public static void main(String[] args) {
long seed;
boolean response;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a random seed : ");
seed = scan.nextLong();
Random ran = new Random();
ran.setSeed(seed);
scan.nextLine();
System.out.println("Welcome to the Poker Dice
game!\nRoll 5 dice and try to assemble the best poker
hand.");
response = promptToPlay(scan,"Would you like to play
[Y/N]? ");
while(response)
{
int dice[] = new int[5];
rollDice(ran,dice);
promptForReroll(dice,scan);
rollDice(ran,dice);
System.out.println("Your final roll
: "+Arrays.toString(dice));
System.out.println(scoreDice(dice));
response = promptToPlay(scan,"Would
you like to play again [Y/N]? ");
}
scan.close();
System.out.println("Goodbye!");
}
}
//end of program
Output:



PLEASE HELP ME WITH THIS PROJECT. For this lab you will write a Java program that...