Sei sulla pagina 1di 11

MUST USE THE FOLLOWING SKELETON:

/*
* Project10.java
*
* 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 scored in this assignment:
*
* High card
*
* One Pair
*
* Two Pair
*
* Three of a Kind
*
* Full House
*
* Four of a Kind
*
* Five of a Kind
* For extra credit, you may also implement:
*
* Straight
* See the write-up for Homework Lab 11 for more details.
*
* @author ENTER YOUR NAME HERE
*
*/
package osu.cse1223;
import java.util.Scanner;
public class Project10 {
public static void main(String[] args) {
// Fill in the body
}
// Given an array of integers as input, sets every element of the array to zero.
private static void resetDice(int[] dice) {
// Fill in the body
}
// 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) {
// Fill in the body
}
// 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) {
// Fill in the body
}
// 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, Scanner inScanner) {
// Fill in the body
}
// 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 inScanner) {
// Fill in body
}
// 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) {
// Fill in the body
}

// 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) {
// Fill in the body
}

}
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

Five of a Kind

All five dice show the same value

Four of a Kind

Four of the five dice show the same value

Full House

Three of the five dice show the same value, the other two show a different value

Three of a Kind

Three of the five dice show the same value, the other two show different values

Straight

All five dice together show a sequence of values 1-5 or 2-6

Two Pair

Two of the five dice show the same value, and two other dice show a different shared value

One Pair

Two of the five dice show the same value, the other dice show different values

Highest Card X

If none of the above hands exist, then the score for the hand is "Highest Card X" where X is t

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 should 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.
Project10.java
NOTE: To generate a random number between 1 and 6, use the Math.random() function as
previously used in lab assignments. In this case, a random roll between 1 and 6 would be
produced by this snippet of code:
int roll = (int)(Math.random()*6)+1;
Project 10 Sample Output
This is a sample transcript of what your program should do. Items in bold are user input and
should not be put on the screen by your program.
Your current dice: 4 2 1 4 6
Select a die to re-roll (-1 to keep remaining dice): 1
Your current dice: 4 0 1 4 6
Select a die to re-roll (-1 to keep remaining dice): 2
Your current dice: 4 0 0 4 6
Select a die to re-roll (-1 to keep remaining dice): 4
Your current dice: 4 0 0 4 0
Select a die to re-roll (-1 to keep remaining dice): -1
Keeping remaining dice...
Rerolling...
Your final dice: 4 2 3 4 6

One pair!
Would you like to play again [Y/N]?: y
Your current dice: 5 6 6 3 2
Select a die to re-roll (-1 to keep remaining dice): 0
Your current dice: 0 6 6 3 2
Select a die to re-roll (-1 to keep remaining dice): 3
Your current dice: 0 6 6 0 2
Select a die to re-roll (-1 to keep remaining dice): 4
Your current dice: 0 6 6 0 0
Select a die to re-roll (-1 to keep remaining dice): -1
Keeping remaining dice...
Rerolling...
Your final dice: 6 6 6 5 4
Three of a kind!
Would you like to play again [Y/N]?: y
Your current dice: 1 2 6 2 2
Select a die to re-roll (-1 to keep remaining dice): 0
Your current dice: 0 2 6 2 2
Select a die to re-roll (-1 to keep remaining dice): 2
Your current dice: 0 2 0 2 2
Select a die to re-roll (-1 to keep remaining dice): -1
Keeping remaining dice...
Rerolling...
Your final dice: 1 2 6 2 2
Three of a kind!
Would you like to play again [Y/N]?: y
Your current dice: 3 4 5 3 1
Select a die to re-roll (-1 to keep remaining dice): 0
Your current dice: 0 4 5 3 1
Select a die to re-roll (-1 to keep remaining dice): -1
Keeping remaining dice...
Rerolling...
Your final dice: 1 4 5 3 1
One pair!
Would you like to play again [Y/N]?: n
Goodbye!
Note that your output depends on the choices made by the user. Remember to check that the user
inputs a valid index for a die to re-roll (between 0 and 4, or -1 to quit) and that the user inputs
either a Y or an N when asked to play again. The play again response should accept either capital
or lower-case letters. A second transcript might look like this (note that this transcript implements
the extra credit that allows you to score a Straight on this assignment) :

Your current dice: 3 1 5 4 2


Select a die to re-roll (-1 to keep remaining dice): 6
Error: Index must be between 0 and 4 (-1 to quit)!
Select a die to re-roll (-1 to keep remaining dice): -2
Error: Index must be between 0 and 4 (-1 to quit)!
Select a die to re-roll (-1 to keep remaining dice): -1
Keeping remaining dice...
Rerolling...
Your final dice: 3 1 5 4 2
Straight!
Would you like to play again [Y/N]?: Q
ERROR! Only 'Y' or 'N' allowed as input!
Would you like to play again [Y/N]?: q
ERROR! Only 'Y' or 'N' allowed as input!
Would you like to play again [Y/N]?: n
Goodbye!
HINT: Notice that the skeleton of code includes a method named getCounts() that provides an
array of counts for each value in your dice set. A number that shows up 5 times will have a count
of 5. A number that shows up 4 times will have a count of 4. Think about how these counts apply
to all of the different possible combinations detailed above.
EXTRA CREDIT HINT: It is easiest to figure out whether your dice are a Straight or not if you
first put them into sorted order. For this you can either use the Bubble Sort algorithm discussed
in Closed Lab OR you can look up the library method Arrays.sort() in the Java documentation.
As a suggestion, consider researching and using Arrays.sort() rather than coding your own
sorting algorithm for this assignment.

Program:

import java.util.Scanner;
public class Project10
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int[] dice = new int[5];
int[] tmpdice = new int[5];
do
{
resetDice(dice);
System.out.print("Your current dice:");
rollDice(dice);
System.out.print(diceToString(dice));
System.out.println();
promptForReroll(dice, in);
System.out.println("Rerolling...");
rollDice(dice);
System.out.println("Your final dice: ");
System.out.println(diceToString(dice));
System.out.println(getResult(dice)+"!");
}while(promptForPlayAgain(in));
System.out.println("GOOD BYE!");
}
// Method to set every element of the array to zero.
private static void resetDice(int[] dice)
{
int arylength = 5;
for(int lp = 0; lp < arylength; lp++)
{
dice[lp] = 0;
}
}
// Method togenerate a number between 1 and 6 and replace
the zero with it.
private static void rollDice(int[] dice)
{
int lp = 0;
int arylength = 5;
while(lp < arylength)
{
if(dice[lp] == 0)
{
int roll = (int)(Math.random()*6)+1;
dice[lp] = roll;

lp++;
}
else
{
lp++;
}
}
}
// Method to create a formatted String of the array.
private static String diceToString(int[] dice)
{
//add stuff
String result ="";
for(int lp=0; lp < dice.length; lp++)
{
result += dice[lp] + " ";
}
return result;
}
// Method to prompt the user with a message to indicate
which dice should be re-rolled.
private static void promptForReroll(int[] dice, Scanner
inScanner)
{
System.out.print("Select a die to re-roll (-1 to keep
remaining dice): ");
int choice = inScanner.nextInt();
while(choice != -1)
{
if(choice > 4 || choice < -1)
{
System.out.println("Erorr: Indx must be
between 0 and 4 (-1 to quit)!");
}
else
{
dice[choice] = 0;
}
System.out.println("Your current dice: " +
diceToString(dice));
System.out.print("Select a die to re-roll (-1 to
keep remaining dice): ");
choice = inScanner.nextInt();
}
System.out.println("Keeping remaining die...");

}
// Method to prompt the user to play again.
private static boolean promptForPlayAgain(Scanner inScanner)
{
String option;
while(true)
{
System.out.println("Do you want to play
again(Y/N)?:");
option = inScanner.next();
if(option.equalsIgnoreCase("Y"))
return true;
else if(option.equalsIgnoreCase("N"))
return false;
else
System.out.println("Invalid response, try
again!");
}
}
// Method to determine the result as a hand of Poker Dice.
private static String getResult(int[] dice)
{
int[] set =getCounts(dice);
String resValue="";
for(int lp=0; lp < set.length; lp++)
{
if(set[lp] == 5)
resValue = "Five of a kind ";
else if(set[lp] == 4)
resValue = "Four of a kind ";
else if(set[lp] == 3)
{
for(int j=0; j < set.length; j++)
{
if(set[j] == 2)
{
resValue = "Full House ";
}
}
resValue = "Three of a Kind ";
}
else if(set[lp] == 2)
{
for(int j=0; j < set.length; j++)
{
if(set[j] == 2)

{
resValue = "Two Pairs ";
}
}
resValue = "One Pair ";
}
else{
resValue = "Highest Card ";
}
}
return resValue;
}
// Method to return back an array with the counts of the
individual values in it.
private static int[] getCounts(int[] dice)
{
int[] set = new int[6];
String resValue ="";
for(int lp=0; lp < dice.length; lp++)
{
if(dice[lp] == 1)
set[0]++;
else if(dice[lp] == 2)
set[1]++;
else if(dice[lp] == 3)
set[2]++;
else if(dice[lp] == 4)
set[3]++;
else if(dice[lp] == 5)
set[4]++;
else if(dice[lp] == 6)
set[5]++;
}
return set;
}
}

Result:

Your current dice:1 1 2 4 6


Select a die to re-roll (-1 to keep
Your current dice: 0 1 2 4 6
Select a die to re-roll (-1 to keep
Keeping remaining die...
Rerolling...
Your final dice:
1 1 2 4 6
Highest Card !
Do you want to play again(Y/N)?:
y
Your current dice:4 3 6 2 5
Select a die to re-roll (-1 to keep
Your current dice: 4 0 6 2 5
Select a die to re-roll (-1 to keep
Your current dice: 4 0 0 2 5
Select a die to re-roll (-1 to keep
Keeping remaining die...
Rerolling...
Your final dice:
4 2 5 2 5
Highest Card !
Do you want to play again(Y/N)?:n

remaining dice): 0
remaining dice): -1

remaining dice): 1
remaining dice): 2
remaining dice): -1

Potrebbero piacerti anche