Poker Simulator. Implement a simulation of a popular casino game called video po
ID: 3535225 • Letter: P
Question
Poker Simulator. Implement a simulation of a popular casino game called video poker. The card deck contains 52 cards,
13 of each suit. At the beginning of the game,the deck is shuffled. You need to devise a fair method for shuffling. (It does
not have to be efficient.) Then the top five cards of the deck are presented to the player. The player can reject none,
some, or all five of the cards. The rejected cards are replaced from the top of the deck. Now the hand is scored. Your
program should pronounce it to be one of the following:
* No pair- The lowest hand, containing five separate cards that do not match up to create any of the hands below.
* One Pair- Two cards of the same value, for example two queens.
* Two pairs- Two pairs, for example two queens and two fives.
* Three of a kind- Three cards of the same value, for example three queens.
* Straight- Five cards with consecutive values, not necessarily of the same suit, such as 4, 5, 6, 7, and8. An ace can
either precede a 2 or follow a king.
* Flush- Five cards, not necessarily in order, of the same suit.
* Full House- Three of a kind and a pair,for example three queens and and two 5's.
* Straight Flush- A straight and a flush:Five cards with consecutive values of the same suit.
* Royal Flush- The best possible hand in poker. A 10, jack, queen, king, and ace, all of the same suit.
The program must contain the classes Card, Deck, Player, Game, and a Test class
Explanation / Answer
Here is my Card class: package javapoker;
public class Card
{
private short rank, suit;
private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private static String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
public static String rankAsString( int __rank ) {
return ranks[__rank];
}
Card(short suit, short rank)
{
this.rank=rank;
this.suit=suit;
}
public @Override String toString()
{
return ranks[rank] + " of " + suits[suit];
}
public short getRank() {
return rank;
}
public short getSuit() {
return suit;
}
public boolean sameSuit(Card card) {
// TODO Auto-generated method stub
return false;
}
}
Here is my Deck class: package javapoker;
import java.util.ArrayList;
import java.util.Random;
public class Deck {
private ArrayList<Card> cards;
Deck()
{
cards = new ArrayList<Card>();
int index_1, index_2;
Random generator = new Random();
Card temp;
for (short a=0; a<=3; a++)
{
for (short b=0; b<=12; b++)
{
cards.add( new Card(a,b) );
}
}
int size = cards.size() -1;
for (short i=0; i<100; i++)
{
index_1 = generator.nextInt( size );
index_2 = generator.nextInt( size );
temp = (Card) cards.get( index_2 );
cards.set( index_2 , cards.get( index_1 ) );
cards.set( index_1, temp );
}
}
public Card drawFromDeck()
{
return cards.remove( cards.size()-1 );
}
public int getTotalCards()
{
return cards.size();
//we could use this method when making
//a complete poker game to see if we needed a new deck
}
}
Here is my Player class (it is very flawed, feel free to trash it and start over): package javapoker;
import java.util.Random;
public class Player
{
private static final int random = 0;
public static void main(String[] args)
{
Card [] hand = new Card [5];
hand[0] = new //I know variables go here but I'm trying and
//failing to match them to the random card suits and ranks
hand[1] = new
hand[2] = new
hand[3] = new
hand[4] = new
System.out.println("The hand dealt is: ");
for (int i = 0; i < hand.length; i++)
System.out.println(hand[i]);
if (flush(hand))
System.out.print("Your hand is a flush");
else if (threeKind(hand) && twoKind(hand))
System.out.print("Your hand is a full house");
else if (fourKind(hand))
System.out.print("Your hand is a four of a kind");
else if (threeKind(hand))
System.out.print("Your hand is a three of a kind");
else if (twoKind(hand))
System.out.print("Your hand is a two of a kind");
else if (straight(hand))
System.out.println("You've got a straight hand");
else if (straightFlush(hand))
System.out.println("You've got a straight flush");
else
System.out.print("Trade them all in");
System.out.println();
System.out.print("Thanks for playing");
}
public static boolean flush(Card [] hand)
{
boolean isFlush = true;
Card firstOne = new Card(hand[0].getSuit(), hand[0].face());
//I think I need to define method-Face for the Card class to get rid of these errors
for (int i = 1; isFlush && i < hand.length; i++)
if (!firstOne.sameSuit(hand[i]))
isFlush = false;
return isFlush;
}
public static boolean fullHouse(Card [] hand)
{
System.out.println("in full house");
return (threeKind(hand) && twoKind(hand));
}
public static boolean fourKind(Card [] hand)
{
int count = 0;
for (int i = 0; count < 3 && i < hand.length; i++)
{
count = 0;
for (int j = i + 1; j < hand.length; j++)
if (hand[i].sameFace(hand[j]))
count++;
}
return (count == 3);
}
public static boolean threeKind(Card [] hand)
{
int count = 0;
for (int i = 0; count < 2 && i < hand.length; i++)
{
count = 0;
for (int j = i + 1; j < hand.length; j++)
if (hand[i].sameFace(hand[j]))
count++;
}
return (count == 2);
}
public static boolean twoKind(Card [] hand)
{
int count = 0;
for (int i = 0; count < 1 && i < hand.length; i++)
{
count = 0;
for (int j = i +1; j < hand.length; j++)
if (hand[i].sameFace(hand[j]))
count ++;
}
return (count == 1);
}
public static boolean straight(Card [] hand)
{
boolean isStraight = true;
Card firstOne = new Card(hand[0].getSuit(), hand[0].face());
for (int i = 1; isStraight && i < hand.length; i++)
if (!firstOne.sameFace(hand[i]))
isStraight = false;
return isStraight;
}
public static boolean straightFlush(Card [] hand)
{
System.out.println("in straight flush");
return (straight(hand) && flush(hand));
}
}
here is my test class