package PJ4;
import java.util.*;
//=================================================================================
/** class PlayingCardException: It is used for errors related to Card and Deck objects
* Do not modify this class!
*/
class PlayingCardException extends Exception {
/* Constructor to create a PlayingCardException object */
PlayingCardException (){
super ();
}
PlayingCardException ( String reason ){
super ( reason );
}
}
//=================================================================================
/** class Card : for creating playing card objects
* it is an immutable class.
* Rank – valid values are 1 to 13
* Suit – valid values are 0 to 4
* Do not modify this class!
*/
class Card {
/* constant suits and ranks */
static final String[] Suit = {“Joker”,”Clubs”, “Diamonds”, “Hearts”, “Spades” };
static final String[] Rank = {“”,”A”,”2″,”3″,”4″,”5″,”6″,”7″,”8″,”9″,”10″,”J”,”Q”,”K”};
/* Data field of a card: rank and suit */
private int cardRank; /* values: 1-13 (see Rank[] above) */
private int cardSuit; /* values: 0-4 (see Suit[] above) */
/* Constructor to create a card */
/* throw PlayingCardException if rank or suit is invalid */
public Card(int suit, int rank) throws PlayingCardException {
// suit =0 is joker, rank must be 1 or 2
if (suit==0) {
if ((rank <1) || (rank >2))
throw new PlayingCardException(“Invalid rank for Joker:”+rank);
cardRank=rank;
cardSuit=0;
} else {
if ((rank < 1) || (rank > 13))
throw new PlayingCardException(“Invalid rank:”+rank);
else
cardRank = rank;
if ((suit < 1) || (suit > 4))
throw new PlayingCardException(“Invalid suit:”+suit);
else
cardSuit = suit;
}
}
/* Accessor and toString */
/* You may impelemnt equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() {
if (cardSuit == 0) return Suit[cardSuit]+” #”+cardRank;
else return Rank[cardRank] + ” ” + Suit[cardSuit];
}
/* Few quick tests here */
public static void main(String args[])
{
try {
Card c1 = new Card(4,1); // A Spades
System.out.println(c1);
c1 = new Card(1,10); // 10 Clubs
System.out.println(c1);
c1 = new Card(0,2); // Joker #2
System.out.println(c1);
c1 = new Card(5,10); // generate exception here
}
catch (PlayingCardException e)
{
System.out.println(“PlayingCardException: “+e.getMessage());
}
}
}
//=================================================================================
/** class Decks represents : n decks of 52 (or 54) playing cards
* Use class Card to construct n * 52 (or 54) playing cards!
*
* Do not add new data fields!
* Do not modify any methods
* You may add private methods
*/
class Decks {
/* this is used to keep track of original n*52 or n*54 cards */
private List<Card> originalDecks;
/* this starts with copying cards from originalDecks */
/* it is used to play the card game */
/* see reset(): resets gameDecks to originalDecks */
private List<Card> gameDecks;
/* number of decks in this object */
private int numberDecks;
private boolean withJokers;
/**
* Constructor: Creates one deck of 52 or 54 (withJokers = false or true)
* playing cards in originalDecks and copy them to gameDecks.
* initialize numberDecks=1
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & gameDecks
*/
public Decks(boolean withJokers)
{
// implement this method!
}
/**
* Constructor: Creates n decks (54 or 52 cards each deck – with or without Jokers)
* of playing cards in originalDecks and copy them to gameDecks.
* initialize numberDecks=n
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & gameDecks
*/
public Decks(int n, boolean withJokers)
{
// implement this method!
}
/**
* Task: Shuffles cards in gameDecks.
* Hint: Look at java.util.Collections
*/
public void shuffle()
{
// implement this method!
}
/**
* Task: Deals cards from the gameDecks.
*
* @param numberCards number of cards to deal
* @return a list containing cards that were dealt
* @throw PlayingCardException if numberCard > number of remaining cards
*
* Note: You need to create ArrayList to stored dealt cards
* and should removed dealt cards from gameDecks
*
*/
public List<Card> deal(int numberCards) throws PlayingCardException
{
// implement this method!
return null;
}
/**
* Task: Resets gameDecks by getting all cards from the originalDecks.
*/
public void reset()
{
// implement this method!
}
/**
* Task: Return number of decks.
*/
public int getNumberDecks()
{
return numberDecks;
}
/**
* Task: Return withJokers.
*/
public boolean getWithJokers()
{
return withJokers;
}
/**
* Task: Return number of remaining cards in gameDecks.
*/
public int remainSize()
{
return gameDecks.size();
}
/**
* Task: Returns a string representing cards in the gameDecks
*/
public String toString()
{
return “”+gameDecks;
}
/* Quick test */
/* */
/* Do not modify these tests */
/* Generate 2 decks of 54 cards */
/* Loop 2 times: */
/* Deal 27 cards for 5 times */
/* Expect exception at 5th time*/
/* reset() */
public static void main(String args[]) {
System.out.println(“******* Create 2 decks of cards ********n”);
Decks decks = new Decks(2, true);
System.out.println(“getNumberDecks:” + decks.getNumberDecks());
System.out.println(“getWithJokers:” + decks.getWithJokers());
for (int j=0; j < 2; j++)
{
System.out.println(“n************************************************n”);
System.out.println(“Loop # ” + j + “n”);
System.out.println(“Before shuffle:”+decks.remainSize()+” cards”);
System.out.println(“nt”+decks);
System.out.println(“n==============================================n”);
int numHands = 5;
int cardsPerHand = 27;
for (int i=0; i < numHands; i++)
{
decks.shuffle();
System.out.println(“After shuffle:”+decks.remainSize()+” cards”);
System.out.println(“nt”+decks);
try {
System.out.println(“nnHand “+i+”:”+cardsPerHand+” cards”);
System.out.println(“nt”+decks.deal(cardsPerHand));
System.out.println(“nnRemain:”+decks.remainSize()+” cards”);
System.out.println(“nt”+decks);
System.out.println(“n==============================================n”);
}
catch (PlayingCardException e)
{
System.out.println(“*** In catch block:PlayingCardException:Error Msg: “+e.getMessage());
}
}
decks.reset();
}
}
}
package PJ4;
import java.util.*;
/*
* Ref: http://en.wikipedia.org/wiki/Video_poker
* http://www.freeslots.com/poker.htm
*
*
* Short Description and Poker rules:
*
* Video poker is also known as draw poker.
* The dealer uses a 52-card deck, which is played fresh after each playerHand.
* The player is dealt one five-card poker playerHand.
* After the first draw, which is automatic, you may hold any of the cards and draw
* again to replace the cards that you haven’t chosen to hold.
* Your cards are compared to a table of winning combinations.
* The object is to get the best possible combination so that you earn the highest
* payout on the bet you placed.
*
* Winning Combinations
*
* 1. One Pair: one pair of the same card
* 2. Two Pair: two sets of pairs of the same card denomination.
* 3. Three of a Kind: three cards of the same denomination.
* 4. Straight: five consecutive denomination cards of different suit.
* 5. Flush: five non-consecutive denomination cards of the same suit.
* 6. Full House: a set of three cards of the same denomination plus
* a set of two cards of the same denomination.
* 7. Four of a kind: four cards of the same denomination.
* 8. Straight Flush: five consecutive denomination cards of the same suit.
* 9. Royal Flush: five consecutive denomination cards of the same suit,
* starting from 10 and ending with an ace
*
*/
/* This is the video poker game class.
* It uses Decks and Card objects to implement video poker game.
* Please do not modify any data fields or defined methods
* You may add new data fields and methods
* Note: You must implement defined methods
*/
public class VideoPoker {
// default constant values
private static final int startingBalance=100;
private static final int numberOfCards=5;
// default constant payout value and playerHand types
private static final int[] multipliers={1,2,3,5,6,10,25,50,1000};
private static final String[] goodHandTypes={
“One Pair” , “Two Pairs” , “Three of a Kind”, “Straight”, “Flush “,
“Full House”, “Four of a Kind”, “Straight Flush”, “Royal Flush” };
// must use only one deck
private final Decks oneDeck;
// holding current poker 5-card hand, balance, bet
private List<Card> playerHand;
private int playerBalance;
private int playerBet;
/** default constructor, set balance = startingBalance */
public VideoPoker()
{
this(startingBalance);
}
/** constructor, set given balance */
public VideoPoker(int balance)
{
this.playerBalance= balance;
oneDeck = new Decks(1, false);
}
/** This display the payout table based on multipliers and goodHandTypes arrays */
private void showPayoutTable()
{
System.out.println(“nn”);
System.out.println(“Payout Table Multiplier “);
System.out.println(“=======================================”);
int size = multipliers.length;
for (int i=size-1; i >= 0; i–) {
System.out.println(goodHandTypes[i]+”t|t”+multipliers[i]);
}
System.out.println(“nn”);
}
/** Check current playerHand using multipliers and goodHandTypes arrays
* Must print yourHandType (default is “Sorry, you lost”) at the end of function.
* This can be checked by testCheckHands() and main() method.
*/
private void checkHands()
{
// implement this method!
}
/*************************************************
* add new private methods here ….
*
*************************************************/
public void play()
{
/** The main algorithm for single player poker game
*
* Steps:
* showPayoutTable()
*
* ++
* show balance, get bet
* verify bet value, update balance
* reset deck, shuffle deck,
* deal cards and display cards
* ask for positions of cards to replace
* get positions in one input line
* update cards
* check hands, display proper messages
* update balance if there is a payout
* if balance = O:
* end of program
* else
* ask if the player wants to play a new game
* if the answer is “no” : end of program
* else : showPayoutTable() if user wants to see it
* goto ++
*/
// implement this method!
}
/*************************************************
* Do not modify methods below
/*************************************************
/** testCheckHands() is used to test checkHands() method
* checkHands() should print your current hand type
*/
public void testCheckHands()
{
try {
playerHand = new ArrayList<Card>();
// set Royal Flush
playerHand.add(new Card(3,1));
playerHand.add(new Card(3,10));
playerHand.add(new Card(3,12));
playerHand.add(new Card(3,11));
playerHand.add(new Card(3,13));
System.out.println(playerHand);
checkHands();
System.out.println(“———————————–“);
// set Straight Flush
playerHand.set(0,new Card(3,9));
System.out.println(playerHand);
checkHands();
System.out.println(“———————————–“);
// set Straight
playerHand.set(4, new Card(1,8));
System.out.println(playerHand);
checkHands();
System.out.println(“———————————–“);
// set Flush
playerHand.set(4, new Card(3,5));
System.out.println(playerHand);
checkHands();
System.out.println(“———————————–“);
/************************************************************************************* | |
* | |
* This program is used to test PJ4.VideoPoker class | |
* More info are given in Readme file | |
* | |
* PJ4 class allows user to run program as follows: | |
* | |
* java PJ4 // default credit is $100 | |
* or java PJ4 NNN // set initial credit to NNN | |
* | |
* Do not modify this file! | |
* | |
**************************************************************************************/ | |
import PJ4.VideoPoker; | |
class TestVideoPoker { | |
public static void main(String args[]) | |
{ | |
VideoPoker pokergame; | |
if (args.length > 0) | |
pokergame = new VideoPoker(Integer.parseInt(args[0])); | |
else | |
pokergame = new VideoPoker(); | |
pokergame.play(); | |
} | |
} |
Expert Answer
In the question posted, the VideoPoker class is incomplete. The testCheckHand is incomplete and rest of the code following it is missing. So leaving out that portion, I have completed the VideoPoker and Decks classes and shown you the output of TestVideoPoker class.
*********PLEASE MAKE SURE YOU COPY THE ORIGINAL CODE FROM testCheckHand() onwards into the new file i have given. I hope I am clear.
I have left a comment where you need to copy the rest of missing portions from your original file… Its at the end of the file. I HAVE INTENTIONALLY LEFT THE BRACES UNMATCHED SO THAT THE CODE WILL NOT COMPILE AND YOU DONT MISS COPYING THE ORIGINAL CONTENTS.
Please do rate the answer if it helped. Thank you very much.
Decks.java
package PJ4;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
//=================================================================================
/** class Decks represents : n decks of 52 (or 54) playing cards
* Use class Card to construct n * 52 (or 54) playing cards!
*
* Do not add new data fields!
* Do not modify any methods
* You may add private methods
*/
class Decks {
/* this is used to keep track of original n*52 or n*54 cards */
private List<Card> originalDecks;
/* this starts with copying cards from originalDecks */
/* it is used to play the card game */
/* see reset(): resets gameDecks to originalDecks */
private List<Card> gameDecks;
/* number of decks in this object */
private int numberDecks;
private boolean withJokers;
/**
* Constructor: Creates one deck of 52 or 54 (withJokers = false or true)
* playing cards in originalDecks and copy them to gameDecks.
* initialize numberDecks=1
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & gameDecks
*/
public Decks(boolean withJokers)
{
this(1, withJokers);
}
/**
* Constructor: Creates n decks (54 or 52 cards each deck – with or without Jokers)
* of playing cards in originalDecks and copy them to gameDecks.
* initialize numberDecks=n
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & gameDecks
*/
public Decks(int n, boolean withJokers)
{
numberDecks = n;
this.withJokers = withJokers;
originalDecks = new ArrayList<Card>();
gameDecks = new ArrayList<Card>();
for(int deck = 1; deck <= n; deck++)
{
try {
if(withJokers)
{
originalDecks.add(new Card(0, 1));
originalDecks.add(new Card(0, 2));
}
for(int suit = 1; suit <= 4; suit++)
for(int rank = 1; rank <= 13; rank++)
originalDecks.add(new Card(suit,rank));
} catch (PlayingCardException e) {
System.out.println(e.getMessage());
}
}
gameDecks.addAll(originalDecks);
}
/**
* Task: Shuffles cards in gameDecks.
* Hint: Look at java.util.Collections
*/
public void shuffle()
{
Collections.shuffle(gameDecks, new Random(System.currentTimeMillis()));
}
/**
* Task: Deals cards from the gameDecks.
*
* @param numberCards number of cards to deal
* @return a list containing cards that were dealt
* @throw PlayingCardException if numberCard > number of remaining cards
*
* Note: You need to create ArrayList to stored dealt cards
* and should removed dealt cards from gameDecks
*
*/
public List<Card> deal(int numberCards) throws PlayingCardException
{
if(numberCards > remainSize())
throw new PlayingCardException(“Deck has fewer than ” + numberCards + ” cards”);
List<Card> dealt = new ArrayList<Card>();
for(int i = 1; i <= numberCards; i++)
{
Card c = gameDecks.remove(0);
dealt.add(c);
}
return dealt;
}
/**
* Task: Resets gameDecks by getting all cards from the originalDecks.
*/
public void reset()
{
gameDecks.clear();
gameDecks.addAll(originalDecks);
}
/**
* Task: Return number of decks.
*/
public int getNumberDecks()
{
return numberDecks;
}
/**
* Task: Return withJokers.
*/
public boolean getWithJokers()
{
return withJokers;
}
/**
* Task: Return number of remaining cards in gameDecks.
*/
public int remainSize()
{
return gameDecks.size();
}
/**
* Task: Returns a string representing cards in the gameDecks
*/
public String toString()
{
return “”+gameDecks;
}
/* Quick test */
/* */
/* Do not modify these tests */
/* Generate 2 decks of 54 cards */
/* Loop 2 times: */
/* Deal 27 cards for 5 times */
/* Expect exception at 5th time*/
/* reset() */
public static void main(String args[]) {
System.out.println(“******* Create 2 decks of cards ********n”);
Decks decks = new Decks(2, true);
System.out.println(“getNumberDecks:” + decks.getNumberDecks());
System.out.println(“getWithJokers:” + decks.getWithJokers());
for (int j=0; j < 2; j++)
{
System.out.println(“n************************************************n”);
System.out.println(“Loop # ” + j + “n”);
System.out.println(“Before shuffle:”+decks.remainSize()+” cards”);
System.out.println(“nt”+decks);
System.out.println(“n==============================================n”);
int numHands = 5;
int cardsPerHand = 27;
for (int i=0; i < numHands; i++)
{
decks.shuffle();
System.out.println(“After shuffle:”+decks.remainSize()+” cards”);
System.out.println(“nt”+decks);
try {
System.out.println(“nnHand “+i+”:”+cardsPerHand+” cards”);
System.out.println(“nt”+decks.deal(cardsPerHand));
System.out.println(“nnRemain:”+decks.remainSize()+” cards”);
System.out.println(“nt”+decks);
System.out.println(“n==============================================n”);
}
catch (PlayingCardException e)
{
System.out.println(“*** In catch block:PlayingCardException:Error Msg: “+e.getMessage());
}
}
decks.reset();
}
}
}
VideoPoker.java
package PJ4;
import java.util.*;
/*
* Ref: http://en.wikipedia.org/wiki/Video_poker
* http://www.freeslots.com/poker.htm
*
*
* Short Description and Poker rules:
*
* Video poker is also known as draw poker.
* The dealer uses a 52-card deck, which is played fresh after each playerHand.
* The player is dealt one five-card poker playerHand.
* After the first draw, which is automatic, you may hold any of the cards and draw
* again to replace the cards that you haven’t chosen to hold.
* Your cards are compared to a table of winning combinations.
* The object is to get the best possible combination so that you earn the highest
* payout on the bet you placed.
*
* Winning Combinations
*
* 1. One Pair: one pair of the same card
* 2. Two Pair: two sets of pairs of the same card denomination.
* 3. Three of a Kind: three cards of the same denomination.
* 4. Straight: five consecutive denomination cards of different suit.
* 5. Flush: five non-consecutive denomination cards of the same suit.
* 6. Full House: a set of three cards of the same denomination plus
* a set of two cards of the same denomination.
* 7. Four of a kind: four cards of the same denomination.
* 8. Straight Flush: five consecutive denomination cards of the same suit.
* 9. Royal Flush: five consecutive denomination cards of the same suit,
* starting from 10 and ending with an ace
*
*/
/* This is the video poker game class.
* It uses Decks and Card objects to implement video poker game.
* Please do not modify any data fields or defined methods
* You may add new data fields and methods
* Note: You must implement defined methods
*/
public class VideoPoker {
// default constant values
private static final int startingBalance=100;
private static final int numberOfCards=5;
// default constant payout value and playerHand types
private static final int[] multipliers={1,2,3,5,6,10,25,50,1000};
private static final String[] goodHandTypes={
“One Pair” , “Two Pairs” , “Three of a Kind”, “Straight”, “Flush “,
“Full House”, “Four of a Kind”, “Straight Flush”, “Royal Flush” };
// must use only one deck
private final Decks oneDeck;
// holding current poker 5-card hand, balance, bet
private List<Card> playerHand;
private int playerBalance;
private int playerBet;
private int handType; //declare a variable to store handtype
/** default constructor, set balance = startingBalance */
public VideoPoker()
{
this(startingBalance);
}
/** constructor, set given balance */
public VideoPoker(int balance)
{
this.playerBalance= balance;
oneDeck = new Decks(1, false);
}
/** This display the payout table based on multipliers and goodHandTypes arrays */
private void showPayoutTable()
{
System.out.println(“nn”);
System.out.println(“Payout Table Multiplier “);
System.out.println(“=======================================”);
int size = multipliers.length;
for (int i=size-1; i >= 0; i–) {
System.out.println(goodHandTypes[i]+”t|t”+multipliers[i]);
}
System.out.println(“nn”);
}
/** Check current playerHand using multipliers and goodHandTypes arrays
* Must print yourHandType (default is “Sorry, you lost”) at the end of function.
* This can be checked by testCheckHands() and main() method.
*/
private void checkHands()
{
ArrayList<Card> copy = new ArrayList<Card>(playerHand);
Collections.sort(playerHand, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o1.getRank() – o2.getRank();
}
});
int countCards[] = new int[14];
for(Card c: playerHand)
countCards[c.getRank()] ++;
int numPairs = 0;
int numThreeOfKind = 0;
int numFourOfKind = 0 ;
for(int i = 1; i <= 13; i++)
{
switch(countCards[i])
{
case 2:
numPairs ++;
break;
case 3:
numThreeOfKind++;
break;
case 4:
numFourOfKind++;
break;
}
}
handType = -1;
if(checkRoyalFlush())
handType = 8;
else if(checkStraight() && checkFlush())
{
handType = 7; //straight flush
}
else if(numFourOfKind == 1)
handType = 6; //four of a kind
else if(numThreeOfKind == 1 && numPairs == 1)
handType = 5; //full house
else if(checkFlush())
handType = 4; //flush
else if(checkStraight())
{
handType = 3; //straight
}
else if(numThreeOfKind == 1)
handType = 2; //three of a kind
else if(numPairs == 2)
handType = 1; //2 pairs
else if(numPairs == 1)
handType = 0; //1 pair
if(handType == -1)
System.out.println(“Sorry, you lost!”);
else
System.out.println(“Hand: ” + goodHandTypes[handType]);
playerHand.clear();
playerHand.addAll(copy);
}
/*************************************************
* add new private methods here ….
*
*************************************************/
private void getBetAmount()
{
Scanner keybd = new Scanner(System.in);
System.out.println(“Cash Balance: ” + playerBalance);
while(true)
{
System.out.println(“How much do you want to bet? “);
playerBet = keybd.nextInt();
if(playerBet<= 0 || playerBet > playerBalance)
System.out.println(“Bet amount should be +ve amount upto your cash balance.”);
else
break;
}
keybd.nextLine();//flush new line
}
private boolean checkStraight()
{
int first = playerHand.get(0).getRank();
int second = playerHand.get(1).getRank();
if((second == first + 1) || (first == 1 && second == 10))
{
//check cards at index 2 3 4 if they are in ascending order
for(int i = 2, j = second + 1; i < 5; i++, j++)
{
if(playerHand.get(i).getRank() != j)
return false;
}
return true;
}
else
return false;
}
private boolean checkFlush()
{
int suit = playerHand.get(0).getSuit();
for(int i = 1; i < 5; i++)
{
if(playerHand.get(i).getSuit() != suit)
return false;
}
return true;
}
private boolean checkRoyalFlush()
{
if(playerHand.get(0).getRank() == 1)
{
int suit = playerHand.get(0).getSuit();
for(int i = 1,rank = 10; rank <= 13; i++, rank++)
{
Card c = playerHand.get(i);
if(c.getRank() != rank || c.getSuit() != suit)
return false;
}
return true;
}
else
return false;
}
public void play()
{
/** The main algorithm for single player poker game
*
* Steps:
* showPayoutTable()
*
* ++
* show balance, get bet
* verify bet value, update balance
* reset deck, shuffle deck,
* deal cards and display cards
* ask for positions of cards to replace
* get positions in one input line
* update cards
* check hands, display proper messages
* update balance if there is a payout
* if balance = O:
* end of program
* else
* ask if the player wants to play a new game
* if the answer is “no” : end of program
* else : showPayoutTable() if user wants to see it
* goto ++
*/
showPayoutTable();
while(true)
{
getBetAmount();
playerBalance -= playerBet; // update balance
oneDeck.reset();
oneDeck.shuffle();
try {
playerHand = oneDeck.deal(5);
showCards(playerHand, “Current Hand: “);
replaceCards();
checkHands();
payout();
if(!repeatGame())
break;
} catch (PlayingCardException e) {
System.out.println(e.getMessage());
}
}
}
private boolean repeatGame()
{
if(playerBalance == 0) // can not play if balance not sufficient
return false;
else
{
Scanner keybd = new Scanner(System.in);
while(true)
{
System.out.println(“Play again (y/n)? “);
String ans = keybd.next();
if(ans.equalsIgnoreCase(“n”))
return false;
else if(ans.equalsIgnoreCase(“y”))
{
System.out.println(“Do you want to see the pay table (y/n)? “);
ans = keybd.next();
if(ans.equalsIgnoreCase(“y”))
showPayoutTable();
return true;
}
}
}
}
private void payout()
{
if(handType != -1)
{
playerBalance += multipliers[handType] * playerBet;
}
System.out.println(“Cash Balance: ” + playerBalance);
}
private void replaceCards()
{
Scanner keybd = new Scanner(System.in);
boolean loop = true;
int i;
int positions[] = null;
while(loop)
{
//keybd.nextLine();//clear newline after entering bet amount
System.out.println(“Which cards to keep? Enter positions (e.g. 1 3 4): “);
String input = keybd.nextLine().trim();
if(input.equals(“”))
break;
String tokens[] = input.split(“\s”);
positions = new int[tokens.length];
for( i = 0; i < tokens.length; i++)
{
try
{
positions[i] = Integer.parseInt(tokens[i]);
if(positions [i] <1 || positions[i] > 5)
{
System.out.println(“Card Position should shoud be a value in range 1-5.”);
break;
}
}catch(NumberFormatException e)
{
System.out.println(“Invalid card position!”);
break;
}
}
if(i == tokens.length)
loop = false;
}
ArrayList<Card> retained = new ArrayList<Card>();
if(positions != null)
{
for(i = 0; i < positions.length; i++)
{
retained.add(playerHand.get(positions[i] – 1));
}
}
playerHand.clear();
playerHand.addAll(retained);
showCards(playerHand, “Cards with you:”);
try {
playerHand.addAll(oneDeck.deal(numberOfCards – retained.size()));
} catch (PlayingCardException e) {
System.out.println(e.getMessage());
}
showCards(playerHand, “New Hand:”);
}
private void showCards(List<Card> cards, String message)
{
System.out.println(message + ” ” + Arrays.toString(cards.toArray()));
}
/*************************************************
* Do not modify methods below
/*************************************************
/** testCheckHands() is used to test checkHands() method
* checkHands() should print your current hand type
*/
public void testCheckHands()
{
//COPY FROM ORIGINAL CODE HERE ONWARDS…….
OUTPUT
Payout Table Multiplier
=======================================
Royal Flush | 1000
Straight Flush | 50
Four of a Kind | 25
Full House | 10
Flush | 6
Straight | 5
Three of a Kind | 3
Two Pairs | 2
One Pair | 1
Cash Balance: 100
How much do you want to bet?
10
Current Hand: [2 Clubs, 4 Clubs, A Hearts, 3 Spades, 2 Spades]
Which cards to keep? Enter positions (e.g. 1 3 4):
1 5
Cards with you: [2 Clubs, 2 Spades]
New Hand: [2 Clubs, 2 Spades, 6 Hearts, 10 Spades, J Clubs]
Hand: One Pair
Cash Balance: 100
Play again (y/n)?
y
Do you want to see the pay table (y/n)?
n
Cash Balance: 100
How much do you want to bet?
10
Current Hand: [Q Hearts, K Hearts, 8 Clubs, Q Diamonds, 2 Hearts]
Which cards to keep? Enter positions (e.g. 1 3 4):
1 2 5
Cards with you: [Q Hearts, K Hearts, 2 Hearts]
New Hand: [Q Hearts, K Hearts, 2 Hearts, 4 Spades, 3 Spades]
Sorry, you lost!
Cash Balance: 90
Play again (y/n)?
y
Do you want to see the pay table (y/n)?
n
Cash Balance: 90
How much do you want to bet?
10
Current Hand: [2 Spades, 10 Hearts, 2 Hearts, 9 Hearts, Q Hearts]
Which cards to keep? Enter positions (e.g. 1 3 4):
1 2 3 4
Cards with you: [2 Spades, 10 Hearts, 2 Hearts, 9 Hearts]
New Hand: [2 Spades, 10 Hearts, 2 Hearts, 9 Hearts, 4 Hearts]
Hand: One Pair
Cash Balance: 90
Play again (y/n)?
n