Modify the application below to deal a five-card poker hand. Then modify the cla
ID: 3692101 • Letter: M
Question
Modify the application below to deal a five-card poker hand. Then modify the class to include methods that determine whether a hand contains a pair, two pairs, three of a kind, four of a kind, a flush, a straight, or a full house. (Hint: Add methods getFace and getSuit to class Card of fig. 7.11)
public class Shuffle1 {
public static void main(String[] args) {
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
for (int i = 0; i < deck.length; i++) {
deck[i] = i;
}
for (int i = 0; i < deck.length; i++) {
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
for (int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println( rank + " of " + suit);
}
}
}
Explanation / Answer
public class Hand {
private Card[] cards;
private int[] value;
Hand(Deck d)
{
value = new int[6];
cards = new Card[5];
for (int x=0; x<5; x++)
{
cards[x] = d.drawFromDeck();
}
int[] ranks = new int[14];
int[] orderedRanks = new int[5];
boolean flush=true, straight=false;
int sameCards=1,sameCards2=;
int largeGroupRank=0,smallGroupRank=0;
int index=0,topStraightValue=0;
for (int x=0; x<=13; x++)
{
ranks[x]=0;
}
for (int x=0; x<=4; x++)
{
ranks[ cards[x].getRank() ]++;
}
for (int x=0; x<4; x++) {
if ( cards[x].getSuit() != cards[x+1].getSuit() )
flush=false;
}
for (int x=13; x>=1; x--)
{
if (ranks[x] > sameCards)
{
if (sameCards != 1)
{
sameCards2 = sameCards;
smallGroupRank = largeGroupRank;
}
sameCards = ranks[x];
largeGroupRank = x;
}
else if (ranks[x] > sameCards2)
{
sameCards2 = ranks[x];
smallGroupRank = x;
}
}
if (ranks[1]==1)
{
orderedRanks[index]=14;
index++;
}
for (int x=13; x>=2; x--)
{
if (ranks[x]==1)
{
orderedRanks[index]=x;
index++;
}
}
for (int x=1; x<=9; x++)
{
if (ranks[x]==1 && ranks[x+1]==1 && ranks[x+2]==1 && ranks[x+3]==1 && ranks[x+4]==1)
{
straight=true;
topStraightValue=x+4;
break;
}
}
if (ranks[10]==1 && ranks[11]==1 && ranks[12]==1 && ranks[13]==1 && ranks[1]==1)
{
straight=true;
topStraightValue=14;
}
for (int x=0; x<=5; x++)
{
value[x]=0;
}
if ( sameCards==1 )
{
value[0]=1;
value[1]=orderedRanks[0];
value[2]=orderedRanks[1];
value[3]=orderedRanks[2];
value[4]=orderedRanks[3];
value[5]=orderedRanks[4];
}
if (sameCards==2 && sameCards2==1)
{
value[0]=2;
value[1]=largeGroupRank;
value[2]=orderedRanks[0];
value[3]=orderedRanks[1];
value[4]=orderedRanks[2];
}
if (sameCards==2 && sameCards2==2) //two pair
{
value[0]=3;
value[1]= largeGroupRank>smallGroupRank ? largeGroupRank : smallGroupRank;
value[2]= largeGroupRank<smallGroupRank ? largeGroupRank : smallGroupRank;
value[3]=orderedRanks[0];
}
if (sameCards==3 && sameCards2!=2)
{
value[0]=4;
value[1]= largeGroupRank;
value[2]=orderedRanks[0];
value[3]=orderedRanks[1];
}
if (straight && !flush)
{
value[0]=5;
value[1]=topStraightValue;
}
if (flush && !straight)
{
value[0]=6;
value[1]=orderedRanks[0];
value[2]=orderedRanks[1];
value[3]=orderedRanks[2];
value[4]=orderedRanks[3];
value[5]=orderedRanks[4];
}
if (sameCards==3 && sameCards2==2)
{
value[0]=7;
value[1]=largeGroupRank;
value[2]=smallGroupRank;
}
if (sameCards==4)
{
value[0]=8;
value[1]=largeGroupRank;
value[2]=orderedRanks[0];
}
if (straight && flush)
{
value[0]=9;
value[1]=topStraightValue;
}
}
void displayAll()
{
for (int x=0; x<5; x++)
{
System.out.println(cards[x]);
}
}
int compareTo(Hand that)
{
for (int x=0; x<6; x++)
{
if (this.value[x]>that.value[x])
return 1;
else if (this.value[x]<that.value[x])
return -1;
}
return 0;
}
}//class
public class Shuffle1 {
public static void main(String[] args) {
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
for (int i = 0; i < deck.length; i++) {
deck[i] = i;
}
for (int i = 0; i < deck.length; i++) {
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
for (int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println( rank + " of " + suit);
}
for (int i=0; i<200; i++)
{
Deck deck= new Deck();
Hand hand= new Hand(deck);
Hand hand2= new Hand(deck);
hand.displayAll();
hand2.displayAll();
System.out.println(hand.compareTo(hand2));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.