In this assignment, you will implement a simulation of a popular casino game usu
ID: 3699040 • Letter: I
Question
In this assignment, you will implement a simulation of a popular casino game usually 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.) The player pays a token for each game. Then the top five cards of the deck are presented to the player. The player can reject none, some, or all 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. Payout: 1
• Two pairs—Two pairs, for example two queens and two 5’s. Payout: 2
• Three of a kind—Three cards of the same value, for example three queens. Payout: 3
• Straight—Five cards with consecutive values, not necessarily of the same suit, such as 4, 5, 6, 7, and 8. The ace can either precede a 2 or follow a king. Payout: 4
• Flush—Five cards, not necessarily in order, of the same suit. Payout: 5
• Full House—Three of a kind and a pair, for example three queens and two 5’s. Payout: 6
• Four of a Kind—Four cards of the same value, such as four queens. Payout: 25
• Straight Flush—A straight and a flush: Five cards with consecutive values of the same suit. Payout: 50
• Royal Flush—The best possible hand in poker. A 10, jack, queen, king, and ace, all of the same suit. Payout: 250
Instead of your player betting a single token each time, allow the player to bet between 1-5 tokens. If a player bets n tokens they win n times as much as indicated in the above.
Card Class
public class Card implements Comparable<Card>{
private int suit; // use integers 1-4 to encode the suit
private int rank; // use integers 1-13 to encode the rank
public Card(int s, int r){
//make a card with suit s and value v
}
public int compareTo(Card c){
// use this method to compare cards so they
// may be easily sorted
}
public String toString(){
// use this method to easily print a Card object
}
// add some more methods here if needed
}
Deck Class
public class Deck {
private Card[] cards;
private int top; // the index of the top of the deck
// add more instance variables if needed
public Deck(){
// make a 52 card deck here
}
public void shuffle(){
// shuffle the deck here
}
public Card deal(){
// deal the top card in the deck
}
// add more methods here if needed
}
Game Class
public class Game {
private Player p;
private Deck cards;
// you'll probably need some more here
public Game(String[] testHand){
// This constructor is to help test your code
// use the contents of testHand to
// make a hand for the player
// use the following encoding for cards
// c = clubs
// d = diamonds
// h = hearts
// s = spades
// 1-13 correspond to ace - king
// example: s1 = ace of spades
// example: testhand = {s1, s13, s12, s11, s10} = royal flush
}
public Game(){
// This no-argument constructor is to actually play a normal game
}
public void play(){
// this method should play the game
}
public String checkHand(ArrayList<Card> hand){
// this method should take an ArrayList of cards
// as input and then determine what evaluates to and
// return that as a String
}
// you will likely want many more methods here
// per discussion in class
}
Player Class
public class Player {
private ArrayList<Card> hand; // the player's cards
private double bankroll;
private double bet;
// you may choose to use more instance variables
public Player(){
// create a player here
}
public void addCard(Card c){
// add the card c to the player's hand
}
public void removeCard(Card c){
// remove the card c from the player's hand
}
public void bets(double amt){
// player makes a bet
}
public void winnings(double odds){
// adjust bankroll if player wins
}
public double getBankroll(){
// return current balance of bankroll
}
// you may wish to use more methods here
}
Tester Class
public class PokerTest
public static void main(String[] args){
if (args.length<1){
Game g = new Game();
g.play();
}
else{
Game g = new Game(args);
g.play();
}
}
}
Explanation / Answer
Start and end of classes will be indicated in bold.
Class Card CODE
//class Card implements interface Comparable which is used for sorting an array of Card objects based
on the card face values. It's
public class Card implements Comparable<Card>
{
private int face;
private int suit;
private String faces[] = { "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" ,"Ace"};
private String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
public Card( int cardFace, int cardSuit )
{
face = cardFace;
suit = cardSuit;
}
public String toString()
{
return faces[face] + " of " + suits[suit];
}
public int getFace()
{
return face;
}
public int getSuit()
{
return suit;
}
public int compareTo(Card compareCard)
{
int compareFace = ((Card) compareCard).getFace();
return compareFace - this.face;
}
}
END Class Card
CODE for class Deck
import java.util.Random;
import java.util.Scanner;
public class Deck
{
private Card[] deck;
private int currentCard = 0;
private final int NUMBER_OF_CARDS = 52;
private Random randomNumbers = new Random();
private Hand handPlayer = new Hand();
private static final int ROYAL_FLUSH = 8;
private static final int FOUR_OF_A_KIND = 7;
private static final int FULL_HOUSE = 6;
private static final int FLUSH = 5;
private static final int STRAIGHT = 4;
private static final int THREE_OF_A_KIND = 3;
private static final int TWO_PAIRS = 2;
private static final int>
private static final int NO_PAIR = 0;
private static String[] CardOrder = {"NO Pair","One Pair","Two Pair","Three Of a Kind", "Straight","Flush","Full House","Four Of A Kind","Royal Flush"};
// Constructor for creating a deck of cards
public Deck()
{
deck = new Card[ NUMBER_OF_CARDS ];
for ( int count = 0; count < deck.length; count++ )
{
deck[ count ] = new Card( count%13, count / 13 );
}
}
// Method to shuffle cards using Random Class from java.lang
public void shuffle()
{
for(int first = 0; first < deck.length; first++)
{
int second = randomNumbers.nextInt(NUMBER_OF_CARDS);
Card temp = deck[first]; deck[first] = deck[second];
deck[second] = temp;
}
}
public void dealHand()
{
for(int i = 0;i<5;i++)
{
handPlayer.setHand(i,deck[currentCard++]);
}
}
public void playPoker()
{
shuffle();
dealHand();
System.out.println("Your hand is:");
handPlayer.printHand();
Scanner input = new Scanner(System.in);
System.out.println("specify number of cards you want to change");
int choice = input.nextInt();
if(choice == 5)
{
for(int i=0;i<5;i++)
{
changeHand(i);
}
}
else if(choice != 0)
{
for(int i=choice; i>0;i--)
{
System.out.println("Enter the position of card you want change"); changeHand(input.nextInt());
}
}
System.out.printf("Your Result is : %s",CardOrder[handPlayer.score()]);
System.out.println("Your hand is:");
handPlayer.printHand();
}
public void changeHand(int cardIndex)
{
handPlayer.setHand(cardIndex,deck[currentCard++]);
}
}
END OF CLASS DECK
START CLASS HAND
import java.util.Arrays;
public class Hand
{
private Card[] hand = new Card[5];
private int count = 0;
private static final int ROYAL_FLUSH = 8;
private static final int FOUR_OF_A_KIND = 7;
private static final int FULL_HOUSE = 6;
private static final int FLUSH = 5;
private static final int STRAIGHT = 4;
private static final int THREE_OF_A_KIND = 3;
private static final int TWO_PAIRS = 2;
private static final int>
private static final int NO_PAIR = 0;
public void setHand(int number,Card dealtCard)
{
hand[number] = dealtCard;
count++;
}
public Card getCard(int number)
{
return hand[number];
}
public void sortHand()
{
Arrays.sort(hand);
}
public void printHand()
{
for(int i=0;i<5;i++)
{
System.out.printf("%d.%s ",i+1,hand[i]);
}
}
public int score()
{
sortHand();
if(royalFlush())
return ROYAL_FLUSH;
else if(fourOfaKind())
return FOUR_OF_A_KIND;
else if(fullHouse())
return FULL_HOUSE;
else if(flush())
return FLUSH;
else if(straight())
return STRAIGHT;
else if(threeOfaKind())
return THREE_OF_A_KIND;
else if(twoPairs())
return TWO_PAIRS;
else if(onePair())
return ONE_PAIR;
else
return NO_PAIR;
}
public boolean royalFlush()
{
if(straight() && flush())
return true;
else
return false;
}
public boolean fullHouse()
{
if(threeOfaKind())
{
if(hand[3].getFace() == hand[4].getFace())
return true;
}
return false;
}
public boolean straight()
{
for(int i=0;i<4;i++)
{
if(!(hand[i].getFace() == hand[i+1].getFace() - 1));
return false;
}
return true;
}
public boolean flush()
{
for(int i=0;i<4;i++)
{
if(!(hand[i].getSuit() == hand[i+1].getSuit()))
return false;
}
return true;
}
public boolean fourOfaKind()
{
if(threeOfaKind())
{
if(hand[2].getFace() == hand[3].getFace())
return true;
else if(hand[2].getFace() == hand[4].getFace())
{
Card temp = hand[3];
hand[3] = hand[4];
hand[4] = temp;
return true;
}
}
return false;
}
public boolean threeOfaKind()
{
for(int i=0;i<3;i++)
{
if(hand[i].getFace() == hand[i+2].getFace())
{
for(int j=0;j<i;j++)
{
Card temp = hand[j];
hand[j] = hand[j+3];
hand[j+3] = temp;
}
return true;
}
}
return false;
}
public boolean twoPairs()
{
int[] pair = {5,5};
int count = 0;
for(int i=0;i<4;i++)
{
if(hand[i].getFace() == hand[i+1].getFace())
{
pair[count] = i;
count++;
}
}
if(count == 2)
{
if(!(pair[0] == 0))
{
Card temp = hand[0];
hand[0] = hand[2];
hand[2] = hand[4];
hand[4] = temp;
}
else
{
if(!(pair[1] == 2))
{
Card temp = hand[2];
hand[2] = hand[4];
hand[4] = temp;
}
}
return true;
}
return false;
}
public boolean onePair()
{
for(int i=0;i<4;i++)
{
if(hand[i].getFace() == hand[i+1].getFace())
{
Card temp = hand[0];
hand[0] = hand[i];
hand[i] = temp;
temp = hand[1];
hand[1] = hand[i+1];
hand[i+1] = temp;
Arrays.sort(hand,2,4);
return true;
}
}
return false;
}
}
END CLASS HAND
START CLASS POKER
public class Poker
{
public static void main(String[] args)
{
Deck myDeck = new Deck();
myDeck.playPoker();
}
}
//Please comment in here in case of any quieries and do rate the solution if it is helpful..Thanks