Card Shuffling and Dealing
Using the code in the book (or
slides if you do not have the book), modify Fig.7.11 to deal a five-card
poker hand. Then modify class DeckOfCards of Fig.7.10 to include methods
that determine whether a hand contains:
- a pair
- two pairs
- three of a kind (e.g., three jacks)
- four of a kind (e.g., four aces)
- a flush (i.e., all five cards of the same suit)
- a straight (i.e., five cards of consecutive face
values)
- a full house (i.e., two cards of one face value and
three cards of another face value)
Hint: Add methods getFace and
getSuit to class Card of Fig.7.9
Then write an application that deals
two five-card poker hands, evaluates each hand and determines which is better.
Make your application simulate
the dealer. The dealer's five-card hand is dealt "face down,"
so the player cannot see it. The application should then evaluate the
dealer's hand, and, based on the quality of the hand, the dealer should
draw one, two, or three more cards to replace the corresponding number of
unneeded cards in the original hand. The application should then
reevaluate the dealer's hand.
Answer
public
class card{
private
final String Face;
private
final String Suit;
public
card(String Cardface, String Cardsuit){
Face
= Cardface;
Suit
= Cardsuit;
}
public
String toString(){
return
Face + " of " + Suit;
}
public
String getFace(){
return
Face;
}
public
String getSuit(){
return
Suit;
}
}
////////////////////////////////
DeckOfCards.java ////////////////////////////////
import
java.util.Random;
public
class DeckOfCards{
//
modified to correct strings to String
public
static final String[] Faces = { "Ace", "Deuce",
"Three", "Four",
"Five",
"Six", "Seven", "Eight",
"Nine",
"Ten", "Jack", "Queen",
"King"};
public
static final String[] Suits = {"Hearts", "Diamonds",
"Clubs",
"Spades"};
//shuffle
public
static void shuffle(card[] Deck){
Random
randomNumbers = new Random(); //creating random object
for
(int First = 0; First < Deck.length; First++){
int
Second = randomNumbers.nextInt(Deck.length);
card
temp = Deck[First];
Deck[First]
= Deck[Second];
Deck[Second]
= temp;
}
}
public
static int[] FacesIteration(card Hand[]){
int[]
iteration = new int[Faces.length];
for
(int count = 0; count < 13; count++)
iteration[count]
= 0;
for
(int h = 0; h < Hand.length; h++){
for
(int i = 0; i < 13; i++){
if
(Hand[h].getFace() == Faces[i])iteration[i]++;
}
}
return
iteration;
}
public
static boolean Twin(card Hand[]){
int
t = 0;
int[]
frequency = FacesIteration(Hand);
for
(int j = 0; j < frequency.length; j++){
if
(frequency[j] == 2){
t++;
}
}
return
(t == 1);
}
public
static boolean Triples(card Hand[]){
int
triple = 0;
int[]
frequency = FacesIteration(Hand);
for
(int j = 0; j < frequency.length; j++){
if
(frequency[j] == 3){
triple++;
break;
}
}
return
(triple == 1);
}
public
static boolean Quadruple(card Hand[]){
int[]
frequency = FacesIteration(Hand);
for
(int j = 0; j < Faces.length; j++)
{
if
(frequency[j] == 4){
return
true;
}
}
return
false;
}
public
static boolean flush( card[] Hand){
//
correct the name of getSuite to getSuit
String
cardSuit = Hand[0].getSuit();
for
(int k = 1; k < Hand.length; k++){
if
(Hand[k].getSuit() != cardSuit)
return
false;
}
return
true;
}
public
static boolean straight(card[] Hand){
int[]
frequency = FacesIteration(Hand);
if
(frequency[0] == 1){
for
(int counts = 9; counts < frequency.length; counts++){
if
(frequency[counts] != 1)
return
false;
}
return
true;
}
else{
//
declared counts and used i , corrected to counts instead of i that not defined
before
for
(int counts = 0; counts < frequency.length;counts++){
//
modified to correct if statement by removing more )
if
(frequency[counts] == 1 && (counts+4) < frequency.length )
{
boolean
Value = true;
//
corrected for loop to add condition
for
(int c = counts + 1, j = 0;j < 4 ;j++,c++){
if
(frequency[c] != 1){
Value
= false;
break;
}
}
return
Value;
}
}
return
false;
}
}
public
static boolean FullHand(card Hand[]){
return(Twin(Hand)
&& Triples(Hand));
}
public
static boolean DoublePairs(card Hand[]){
int[]
frequency = FacesIteration(Hand);
int
t = 0;
for
(int counts = 0; counts < frequency.length; counts++){
if
(frequency[counts] == 2){
t++;
}
}
return
(t == 2);
}
public
static void main(String[] args){
final
int NUMBEROFCARDS = 52;
//
modified to correct by removing private
card[]Deck
= new card [NUMBEROFCARDS];
for
(int Count = 0; Count < Deck.length; Count++)
//
corrected the name of count to Count
Deck[Count]
= new card (Faces[Count % 13], Suits[Count / 13]);
shuffle
(Deck);
card[]
Hand = new card[5];
for
(int counts = 0; counts < 5; counts++){
Hand[counts]
= Deck[counts];
}
System.out.println("The
required hand is: ");
for
(int counts = 0; counts < Hand.length;counts++){
System.out.println(Hand[counts]);
}
System.out.print("\nHand
contains: ");
if
(Quadruple(Hand)){
System.out.println("Quadruple
");
}
else
if( FullHand(Hand) ){
System.out.println("Full
Hand ");
}
// modified to correct if statement by removing more )
else
if (straight(Hand)){
System.out.println("Straight");
}
else
if (Triples(Hand)){
System.out.println("Triples
");
}
else
if (straight(Hand)){
System.out.println("Straight
");
}else
if (DoublePairs(Hand)){
System.out.println("Double
");
}
else
if (Twin(Hand)){
System.out.println("Twin
");
}
else{
System.out.println("No
match");
}
}
}