Need help with Java poker dice project. Must use the methods provided. Can\'t ge
ID: 3761921 • Letter: N
Question
Need help with Java poker dice project. Must use the methods provided. Can't get it to run, not sure what to fix.
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.):
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 this assignment you are required to implement a scoring function that scores all of the above hands EXCEPT for the Straight. Your code must correctly score 5 of a kind, 4 of a kind, full house, three of kind, two pair, one pair, and highest card X "hands" of dice. For 2 points of extra credit, you may add the ability to score Straights to your scoring function. Indicate in the comments of your code that your code scores Straights as well as the other hands.
For this assignment you must start with the following "skeleton" of Java code. Import this into your Eclipse workspace and fill in the methods as directed. For this assignment you WILL want to add extra methods beyond the methods defined in the skeleton. Feel free to add any 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.
package osu.cse1223;
import java.util.Scanner;
public class Project10 {
public static void main(String[] args) {
Scanner Scanner = new Scanner(System.in);
int[] dice = new int[5];
int[] count = new int[5];
int choice = Scanner.nextInt();
rollDice(dice);
String s =diceToString(dice);
System.out.print("Your current dice:" + s);
System.out.println("Select a die to re-roll (-1 to keep remaining dice):");
promptForReroll(dice, choice);
while(choice!=-1);{
System.out.println("Keeping remaining dice...");
System.out.println("Rerolling...");
System.out.println("Your final dice:");
for(int k=0;k<5;k++){
System.out.println(dice[k]+"");
}
}
getCounts(dice);
getResult(dice);
promptForPlayAgain(Scanner);
resetDice(dice);
}
// Given an array of integers as input, sets every element of the array to zero.
private static void resetDice(int[] dice) {
for(int i = 0; i < 5; i++){
dice[i] = 0;
}
}
// Given an array of integers as input, checks each element of the array. If the value
// of that element is zero, generate a number between 1 and 6 and replace the zero with
// it. Otherwise, leave it as is and move to the next element.
private static void rollDice(int[] dice) {
int i = 0;
while(i < 5){
int roll =(int)(Math.random()*6)+1;
dice[i] = roll;
i++;
}
}
// Given an array of integers as input, create a formatted String that contains the
// values in the array in the order they appear in the array. For example, if the
// array contains the values [0, 3, 6, 5, 2] then the String returned by this method
// should be "0 3 6 5 2".
private static String diceToString(int[] dice) {
String res = "";
for(int i=0; i< 5; i++){
res= res + dice[i];
}
return res;
}
// Given an array of integers and a Scanner as input, prompt the user with a message
// to indicate which dice should be re-rolled. If the user enters a valid index (between
// 0 and the total number of dice -1) then set the die at that index to zero. If the
// user enters a -1, end the loop and return to the calling program. If the user enters
// any other invalid index, provide an error message and ask again for a valid index.
private static void promptForReroll(int[] dice, int choice) {
for(int i =0; i<5; i++){
if(i==choice){
dice[i]=0;
}
}
}
// Given a Scanner as input, prompt the user to play again. The only valid entries
// from the user are 'Y' or 'N', in either upper or lower case. If the user enters
// a 'Y' the method should return a value of true to the calling program. If the user
// enters a 'N' the method should return a value of false. If the user enters anything
// other than Y or N (including an empty line), an error message should be displayed
// and the user should be prompted again until a valid response is received.
private static boolean promptForPlayAgain(Scanner Scanner) {
System.out.println("Would you like to play again?(Y or N)");
String xy = Scanner.nextLine();
boolean flag=true;
if(xy=="Y" || xy=="y"){
flag= true;
}
else if(xy=="N" || xy=="n"){
flag= false;
}
return flag;
}
// Given an array of integers, determines the result as a hand of Poker Dice. The
// result is determined as:
// * Five of a kind - all five integers in the array have the same value
// * Four of a kind - four of the five integers in the array have the same value
// * Full House - three integers in the array have the same value, and the remaining two
// integers have the same value as well (Three of a kind and a pair)
// * Three of a kind - three integers in the array have the same value
// * Two pair - two integers in the array have the same value, and two other integers
// in the array have the same value
// * One pair - two integers in the array have the same value
// * Highest value - if none of the above hold true, the Highest Value in the array
// is used to determine the result
//
// The method should evaluate the array and return back to the calling program a String
// containing the score from the array of dice.
//
// EXTRA CREDIT: Include in your scoring a Straight, which is 5 numbers in sequence
// [1,2,3,4,5] or [2,3,4,5,6].
private static String getResult(int[] dice) {
int[] count = new int [5];
String resValue = " ";
for (int i = 0; i < 5; i++) {
if (count[i] == 5) {
resValue = "Five of a kind ";
} else if (count[i] == 4) {
resValue = "Four of a kind ";
} else if (count[i] == 3) {
for (int j = 0; j < 5; j++) {
if (count[j]==2) {
resValue = "Full House ";
}
}
resValue = "Three of a Kind ";
} else if (count[i] == 2) {
resValue = "One Pair ";
for (int j = 0; j < 5; j++) {
if (count[j] == 2) {
resValue = "Two Pairs ";
}
}
resValue = "One Pair ";
} else {
resValue = "Highest Card ";
}
}
return resValue;
}
// Given an array of integers as input, return back an array with the counts of the
// individual values in it. You may assume that all elements in the array will have
// a value between 1 and 6. For example, if the array passed into the method were:
// [1, 2, 3, 3, 5]
// Then the array of counts returned back by this method would be:
// [1, 1, 2, 0, 1, 0]
// (Where index 0 holds the counts of the value 1, index 1 holds the counts of the value
// 2, index 2 holds the counts of the value 3, etc.)
// HINT: This method is very useful for determining the score of a particular hand
// of poker dice. Use it as a helper method for the getResult() method above.
private static int[] getCounts(int[] dice) {
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(dice[i]==dice[j]){
}
}
}
return dice;
}
}
[5, 5, 5, 5, 5] Four of a Kind Four of the five dice show the same value [1, 2, 1, 1, 1]
[4, 4, 4, 3, 4] Full House Three of the five dice show the same value, the other two show a different value [1, 2, 2, 1, 1]
[3, 3, 3, 5, 5] Three of a Kind Three of the five dice show the same value, the other two show different values [1, 2, 3, 1, 1]
[3, 3, 3, 2, 5] Straight All five dice together show a sequence of values 1-5 or 2-6 [2, 3, 1, 5, 4]
[6, 3, 2, 4, 5] Two Pair Two of the five dice show the same value, and two other dice show a different shared value [1, 2, 2, 1, 3]
[3, 5, 6, 3, 6] One Pair Two of the five dice show the same value, the other dice show different values [1, 2, 2, 3, 4]
[3, 5, 6, 3, 1] Highest Card X If none of the above hands exist, then the score for the hand is "Highest Card X" where X is the highest value in the list [1, 2, 3, 5, 6] - Highest Card 6
[1, 2, 6, 4, 5] - Highest Card 6
Explanation / Answer
private static int[] getCont(int[] dice)
{
int[] cont = new int[6];
String resValue = "";
for (int k = 0; k < dice.length; k++)
{
if (dice[k] == 1)
{
cont[0]++;
}
else if (dice[k] == 2)
{
cont[1]++;
}
else if (dice[k] == 3)
{
cont[2]++;
}
else if (dice[k] == 4)
{
cont[3]++;
}
else if (dice[k] == 5)
{
cont[4]++;
}
else if (dice[k] == 6)
{
cont[5]++;
}
}
return cont;
}
private static String getResult(int[] dice)
{
int[] cont = getCont(dice);
String resValue = " ";
for (int k = 0; k < cont.length; k++)
{
if (cont[k] == 5)
{
resValue = "Five of a kind ";
break;
}
else if (cont[k] == 4)
{
resValue = "Four of a kind "; break;
}
else if (cont[k] == 3)
{
resValue = "Three of a Kind ";
for (int j = 0; j < cont.length; j++)
{
if (cont[j]==2)
{
resValue = "Full House "; break;
}
}
break;
}
else if (cont[k] == 2)
{
resValue = "One Pair ";
for (int j = 0; j < cont.length; j++)
{
if (cont[j] == 2)
{
resValue = "Two Pairs "; break;
}
}
break;
} else
{
resValue = "Highest Card ";
}
}
return resValue;
}