- Upload a zip file, including PlayingCard.java and VideoPoker.java, - Implement
ID: 3577839 • Letter: #
Question
- Upload a zip file, including PlayingCard.java and VideoPoker.java,
- Implement poker game program in package PJ4:
Part I (40%) Implement Decks class
Part II (60%) Implement VideoPoker class
See PJ4/PlayingCard.java and PJ4/VideoPoker.java for more info.
- Use TestVideoPoker.java to test correctness of your program
- Compile programs (you are in directory containing Readme file):
javac PJ4/*.java
javac TestVideoPoker.java
- Run programs (you are in directory containing Readme file):
// Run tests in PJ4 classes
java PJ4.Decks
java PJ4.VideoPoker
// Run main test program
java TestVideoPoker
******************************************************************************************
- Sample output : java PJ4.Decks
******* Create 2 decks of cards *********
************************************************
Loop # 0
Before shuffle:104 cards
[A Clubs, 2 Clubs, 3 Clubs, 4 Clubs, 5 Clubs, 6 Clubs, 7 Clubs, 8 Clubs, 9 Clubs, 10 Clubs, J Clubs, Q Clubs, K Clubs, A Diamonds, 2 Diamonds, 3 Diamonds, 4 Diamonds, 5 Diamonds, 6 Diamonds, 7 Diamonds, 8 Diamonds, 9 Diamonds, 10 Diamonds, J Diamonds, Q Diamonds, K Diamonds, A Hearts, 2 Hearts, 3 Hearts, 4 Hearts, 5 Hearts, 6 Hearts, 7 Hearts, 8 Hearts, 9 Hearts, 10 Hearts, J Hearts, Q Hearts, K Hearts, A Spades, 2 Spades, 3 Spades, 4 Spades, 5 Spades, 6 Spades, 7 Spades, 8 Spades, 9 Spades, 10 Spades, J Spades, Q Spades, K Spades, A Clubs, 2 Clubs, 3 Clubs, 4 Clubs, 5 Clubs, 6 Clubs, 7 Clubs, 8 Clubs, 9 Clubs, 10 Clubs, J Clubs, Q Clubs, K Clubs, A Diamonds, 2 Diamonds, 3 Diamonds, 4 Diamonds, 5 Diamonds, 6 Diamonds, 7 Diamonds, 8 Diamonds, 9 Diamonds, 10 Diamonds, J Diamonds, Q Diamonds, K Diamonds, A Hearts, 2 Hearts, 3 Hearts, 4 Hearts, 5 Hearts, 6 Hearts, 7 Hearts, 8 Hearts, 9 Hearts, 10 Hearts, J Hearts, Q Hearts, K Hearts, A Spades, 2 Spades, 3 Spades, 4 Spades, 5 Spades, 6 Spades, 7 Spades, 8 Spades, 9 Spades, 10 Spades, J Spades, Q Spades, K Spades]
==============================================
After shuffle:104 cards
[9 Hearts, 7 Hearts, 6 Diamonds, K Diamonds, 8 Diamonds, 5 Hearts, 5 Diamonds, J Clubs, 2 Diamonds, 10 Spades, 4 Diamonds, A Clubs, 8 Spades, K Hearts, 9 Spades, 7 Diamonds, 6 Diamonds, K Spades, 8 Clubs, 9 Spades, 6 Clubs, 8 Clubs, 2 Hearts, A Clubs, Q Hearts, J Hearts, 2 Diamonds, Q Hearts, A Diamonds, 7 Diamonds, 5 Clubs, 6 Clubs, K Hearts, J Hearts, J Clubs, 3 Spades, 3 Diamonds, 3 Clubs, 2 Hearts, 8 Hearts, 8 Diamonds, 8 Spades, A Diamonds, 3 Clubs, 6 Hearts, 3 Hearts, 4 Spades, 4 Clubs, 8 Hearts, 2 Spades, 4 Spades, K Clubs, 5 Hearts, 5 Spades, Q Spades, K Diamonds, 5 Diamonds, 3 Diamonds, J Spades, 2 Clubs, 2 Spades, 2 Clubs, 3 Hearts, 9 Diamonds, 9 Clubs, 7 Hearts, Q Diamonds, 10 Clubs, J Diamonds, 10 Diamonds, 5 Clubs, K Spades, J Spades, 9 Hearts, K Clubs, A Hearts, 4 Hearts, 9 Clubs, 10 Spades, 6 Hearts, 10 Diamonds, 6 Spades, A Spades, J Diamonds, Q Spades, Q Clubs, A Hearts, 7 Clubs, 4 Clubs, 7 Clubs, Q Clubs, 7 Spades, A Spades, 6 Spades, 7 Spades, 4 Diamonds, 10 Hearts, 9 Diamonds, 4 Hearts, Q Diamonds, 10 Clubs, 3 Spades, 10 Hearts, 5 Spades]
Hand 0:30 cards
[5 Spades, 10 Hearts, 3 Spades, 10 Clubs, Q Diamonds, 4 Hearts, 9 Diamonds, 10 Hearts, 4 Diamonds, 7 Spades, 6 Spades, A Spades, 7 Spades, Q Clubs, 7 Clubs, 4 Clubs, 7 Clubs, A Hearts, Q Clubs, Q Spades, J Diamonds, A Spades, 6 Spades, 10 Diamonds, 6 Hearts, 10 Spades, 9 Clubs, 4 Hearts, A Hearts, K Clubs]
Remain:74 cards
[9 Hearts, 7 Hearts, 6 Diamonds, K Diamonds, 8 Diamonds, 5 Hearts, 5 Diamonds, J Clubs, 2 Diamonds, 10 Spades, 4 Diamonds, A Clubs, 8 Spades, K Hearts, 9 Spades, 7 Diamonds, 6 Diamonds, K Spades, 8 Clubs, 9 Spades, 6 Clubs, 8 Clubs, 2 Hearts, A Clubs, Q Hearts, J Hearts, 2 Diamonds, Q Hearts, A Diamonds, 7 Diamonds, 5 Clubs, 6 Clubs, K Hearts, J Hearts, J Clubs, 3 Spades, 3 Diamonds, 3 Clubs, 2 Hearts, 8 Hearts, 8 Diamonds, 8 Spades, A Diamonds, 3 Clubs, 6 Hearts, 3 Hearts, 4 Spades, 4 Clubs, 8 Hearts, 2 Spades, 4 Spades, K Clubs, 5 Hearts, 5 Spades, Q Spades, K Diamonds, 5 Diamonds, 3 Diamonds, J Spades, 2 Clubs, 2 Spades, 2 Clubs, 3 Hearts, 9 Diamonds, 9 Clubs, 7 Hearts, Q Diamonds, 10 Clubs, J Diamonds, 10 Diamonds, 5 Clubs, K Spades, J Spades, 9 Hearts]
==============================================
After shuffle:74 cards
[2 Hearts, Q Hearts, 6 Clubs, 8 Diamonds, 6 Clubs, 8 Hearts, J Spades, J Spades, 2 Diamonds, 3 Diamonds, 3 Clubs, 5 Clubs, 7 Diamonds, 9 Diamonds, 2 Diamonds, 2 Clubs, 4 Spades, K Hearts, 2 Spades, 4 Clubs, Q Diamonds, 3 Diamonds, K Clubs, 7 Diamonds, 2 Clubs, 10 Diamonds, K Spades, J Clubs, 8 Diamonds, 5 Diamonds, J Diamonds, 6 Diamonds, 8 Hearts, 4 Diamonds, 5 Hearts, J Clubs, 3 Clubs, 9 Spades, 3 Hearts, 9 Hearts, 7 Hearts, A Diamonds, 7 Hearts, 2 Hearts, J Hearts, 9 Spades, A Diamonds, 8 Spades, K Hearts, K Diamonds, 8 Spades, 10 Clubs, K Diamonds, 8 Clubs, A Clubs, 10 Spades, 5 Clubs, 5 Hearts, J Hearts, 9 Clubs, Q Spades, A Clubs, 8 Clubs, 3 Spades, 9 Hearts, 6 Hearts, 2 Spades, 3 Hearts, Q Hearts, 5 Diamonds, 4 Spades, 5 Spades, K Spades, 6 Diamonds]
Hand 1:30 cards
[6 Diamonds, K Spades, 5 Spades, 4 Spades, 5 Diamonds, Q Hearts, 3 Hearts, 2 Spades, 6 Hearts, 9 Hearts, 3 Spades, 8 Clubs, A Clubs, Q Spades, 9 Clubs, J Hearts, 5 Hearts, 5 Clubs, 10 Spades, A Clubs, 8 Clubs, K Diamonds, 10 Clubs, 8 Spades, K Diamonds, K Hearts, 8 Spades, A Diamonds, 9 Spades, J Hearts]
Remain:44 cards
[2 Hearts, Q Hearts, 6 Clubs, 8 Diamonds, 6 Clubs, 8 Hearts, J Spades, J Spades, 2 Diamonds, 3 Diamonds, 3 Clubs, 5 Clubs, 7 Diamonds, 9 Diamonds, 2 Diamonds, 2 Clubs, 4 Spades, K Hearts, 2 Spades, 4 Clubs, Q Diamonds, 3 Diamonds, K Clubs, 7 Diamonds, 2 Clubs, 10 Diamonds, K Spades, J Clubs, 8 Diamonds, 5 Diamonds, J Diamonds, 6 Diamonds, 8 Hearts, 4 Diamonds, 5 Hearts, J Clubs, 3 Clubs, 9 Spades, 3 Hearts, 9 Hearts, 7 Hearts, A Diamonds, 7 Hearts, 2 Hearts]
==============================================
After shuffle:44 cards
[J Spades, 3 Clubs, 7 Hearts, 5 Clubs, J Clubs, 4 Spades, A Diamonds, J Spades, 2 Spades, 7 Diamonds, 6 Diamonds, 9 Hearts, J Clubs, 3 Clubs, 2 Diamonds, 5 Diamonds, 2 Hearts, 2 Clubs, 8 Hearts, 2 Clubs, 10 Diamonds, 9 Spades, K Clubs, J Diamonds, Q Diamonds, 4 Diamonds, 5 Hearts, 4 Clubs, 3 Diamonds, 3 Diamonds, Q Hearts, 7 Hearts, 6 Clubs, 8 Diamonds, 2 Hearts, 6 Clubs, 7 Diamonds, 8 Diamonds, 9 Diamonds, K Spades, K Hearts, 2 Diamonds, 3 Hearts, 8 Hearts]
Hand 2:30 cards
[8 Hearts, 3 Hearts, 2 Diamonds, K Hearts, K Spades, 9 Diamonds, 8 Diamonds, 7 Diamonds, 6 Clubs, 2 Hearts, 8 Diamonds, 6 Clubs, 7 Hearts, Q Hearts, 3 Diamonds, 3 Diamonds, 4 Clubs, 5 Hearts, 4 Diamonds, Q Diamonds, J Diamonds, K Clubs, 9 Spades, 10 Diamonds, 2 Clubs, 8 Hearts, 2 Clubs, 2 Hearts, 5 Diamonds, 2 Diamonds]
Remain:14 cards
[J Spades, 3 Clubs, 7 Hearts, 5 Clubs, J Clubs, 4 Spades, A Diamonds, J Spades, 2 Spades, 7 Diamonds, 6 Diamonds, 9 Hearts, J Clubs, 3 Clubs]
==============================================
After shuffle:14 cards
[A Diamonds, 5 Clubs, 9 Hearts, 6 Diamonds, J Clubs, J Spades, 2 Spades, 3 Clubs, J Clubs, 7 Diamonds, 7 Hearts, J Spades, 3 Clubs, 4 Spades]
Hand 3:30 cards
*** In catch block:PlayingCardException:Error Msg: Not enough cards to deal
************************************************
Loop # 1
Before shuffle:104 cards
[A Clubs, 2 Clubs, 3 Clubs, 4 Clubs, 5 Clubs, 6 Clubs, 7 Clubs, 8 Clubs, 9 Clubs, 10 Clubs, J Clubs, Q Clubs, K Clubs, A Diamonds, 2 Diamonds, 3 Diamonds, 4 Diamonds, 5 Diamonds, 6 Diamonds, 7 Diamonds, 8 Diamonds, 9 Diamonds, 10 Diamonds, J Diamonds, Q Diamonds, K Diamonds, A Hearts, 2 Hearts, 3 Hearts, 4 Hearts, 5 Hearts, 6 Hearts, 7 Hearts, 8 Hearts, 9 Hearts, 10 Hearts, J Hearts, Q Hearts, K Hearts, A Spades, 2 Spades, 3 Spades, 4 Spades, 5 Spades, 6 Spades, 7 Spades, 8 Spades, 9 Spades, 10 Spades, J Spades, Q Spades, K Spades, A Clubs, 2 Clubs, 3 Clubs, 4 Clubs, 5 Clubs, 6 Clubs, 7 Clubs, 8 Clubs, 9 Clubs, 10 Clubs, J Clubs, Q Clubs, K Clubs, A Diamonds, 2 Diamonds, 3 Diamonds, 4 Diamonds, 5 Diamonds, 6 Diamonds, 7 Diamonds, 8 Diamonds, 9 Diamonds, 10 Diamonds, J Diamonds, Q Diamonds, K Diamonds, A Hearts, 2 Hearts, 3 Hearts, 4 Hearts, 5 Hearts, 6 Hearts, 7 Hearts, 8 Hearts, 9 Hearts, 10 Hearts, J Hearts, Q Hearts, K Hearts, A Spades, 2 Spades, 3 Spades, 4 Spades, 5 Spades, 6 Spades, 7 Spades, 8 Spades, 9 Spades, 10 Spades, J Spades, Q Spades, K Spades]
==============================================
After shuffle:104 cards
[2 Spades, 8 Diamonds, 7 Diamonds, 4 Diamonds, Q Hearts, A Clubs, J Spades, 9 Clubs, J Clubs, 2 Clubs, 4 Hearts, 7 Hearts, A Spades, 8 Clubs, A Diamonds, 3 Diamonds, 6 Clubs, K Diamonds, 9 Diamonds, 9 Diamonds, 6 Clubs, 7 Spades, 2 Hearts, A Clubs, 6 Hearts, 9 Clubs, K Spades, A Diamonds, 3 Diamonds, 5 Spades, K Hearts, 2 Hearts, Q Hearts, 9 Spades, 8 Spades, J Diamonds, 4 Hearts, 6 Spades, 4 Spades, Q Clubs, 3 Hearts, 3 Spades, Q Diamonds, 5 Spades, A Spades, Q Diamonds, 10 Clubs, 5 Hearts, 5 Hearts, 9 Hearts, 3 Hearts, 6 Diamonds, 2 Spades, Q Clubs, 7 Diamonds, 10 Diamonds, 2 Diamonds, 6 Diamonds, 8 Clubs, 4 Clubs, K Spades, 10 Hearts, 2 Diamonds, A Hearts, J Diamonds, 8 Hearts, 5 Clubs, A Hearts, 5 Diamonds, Q Spades, 3 Clubs, J Hearts, 3 Spades, 6 Hearts, 10 Hearts, K Diamonds, 10 Spades, 8 Hearts, J Clubs, 4 Diamonds, 10 Clubs, J Spades, 4 Clubs, 7 Clubs, 10 Spades, 5 Diamonds, 2 Clubs, 6 Spades, J Hearts, 8 Diamonds, K Clubs, 3 Clubs, 7 Spades, 9 Spades, 7 Clubs, Q Spades, K Hearts, 5 Clubs, 9 Hearts, 10 Diamonds, K Clubs, 8 Spades, 7 Hearts, 4 Spades]
Hand 0:30 cards
[4 Spades, 7 Hearts, 8 Spades, K Clubs, 10 Diamonds, 9 Hearts, 5 Clubs, K Hearts, Q Spades, 7 Clubs, 9 Spades, 7 Spades, 3 Clubs, K Clubs, 8 Diamonds, J Hearts, 6 Spades, 2 Clubs, 5 Diamonds, 10 Spades, 7 Clubs, 4 Clubs, J Spades, 10 Clubs, 4 Diamonds, J Clubs, 8 Hearts, 10 Spades, K Diamonds, 10 Hearts]
Remain:74 cards
[2 Spades, 8 Diamonds, 7 Diamonds, 4 Diamonds, Q Hearts, A Clubs, J Spades, 9 Clubs, J Clubs, 2 Clubs, 4 Hearts, 7 Hearts, A Spades, 8 Clubs, A Diamonds, 3 Diamonds, 6 Clubs, K Diamonds, 9 Diamonds, 9 Diamonds, 6 Clubs, 7 Spades, 2 Hearts, A Clubs, 6 Hearts, 9 Clubs, K Spades, A Diamonds, 3 Diamonds, 5 Spades, K Hearts, 2 Hearts, Q Hearts, 9 Spades, 8 Spades, J Diamonds, 4 Hearts, 6 Spades, 4 Spades, Q Clubs, 3 Hearts, 3 Spades, Q Diamonds, 5 Spades, A Spades, Q Diamonds, 10 Clubs, 5 Hearts, 5 Hearts, 9 Hearts, 3 Hearts, 6 Diamonds, 2 Spades, Q Clubs, 7 Diamonds, 10 Diamonds, 2 Diamonds, 6 Diamonds, 8 Clubs, 4 Clubs, K Spades, 10 Hearts, 2 Diamonds, A Hearts, J Diamonds, 8 Hearts, 5 Clubs, A Hearts, 5 Diamonds, Q Spades, 3 Clubs, J Hearts, 3 Spades, 6 Hearts]
==============================================
After shuffle:74 cards
[A Hearts, A Spades, 7 Spades, 6 Spades, 5 Diamonds, 8 Clubs, 5 Spades, 10 Diamonds, 7 Diamonds, Q Clubs, 3 Hearts, 6 Diamonds, 3 Diamonds, Q Clubs, 9 Clubs, 8 Diamonds, Q Hearts, K Spades, 2 Hearts, 6 Hearts, 3 Hearts, 8 Hearts, A Hearts, 3 Spades, K Diamonds, 10 Hearts, 7 Diamonds, 5 Spades, A Diamonds, 2 Clubs, A Clubs, A Spades, 5 Hearts, 2 Spades, 8 Spades, 2 Spades, J Spades, Q Hearts, 5 Clubs, 3 Spades, 3 Diamonds, 2 Hearts, A Diamonds, J Diamonds, 9 Hearts, 4 Hearts, J Diamonds, 4 Hearts, Q Diamonds, K Spades, 9 Spades, K Hearts, 6 Clubs, J Clubs, 4 Clubs, 9 Diamonds, 4 Spades, 2 Diamonds, 10 Clubs, 8 Clubs, 9 Diamonds, Q Diamonds, 6 Diamonds, 2 Diamonds, Q Spades, 9 Clubs, J Hearts, 6 Hearts, 7 Hearts, 3 Clubs, 6 Clubs, 4 Diamonds, 5 Hearts, A Clubs]
Hand 1:30 cards
[A Clubs, 5 Hearts, 4 Diamonds, 6 Clubs, 3 Clubs, 7 Hearts, 6 Hearts, J Hearts, 9 Clubs, Q Spades, 2 Diamonds, 6 Diamonds, Q Diamonds, 9 Diamonds, 8 Clubs, 10 Clubs, 2 Diamonds, 4 Spades, 9 Diamonds, 4 Clubs, J Clubs, 6 Clubs, K Hearts, 9 Spades, K Spades, Q Diamonds, 4 Hearts, J Diamonds, 4 Hearts, 9 Hearts]
Remain:44 cards
[A Hearts, A Spades, 7 Spades, 6 Spades, 5 Diamonds, 8 Clubs, 5 Spades, 10 Diamonds, 7 Diamonds, Q Clubs, 3 Hearts, 6 Diamonds, 3 Diamonds, Q Clubs, 9 Clubs, 8 Diamonds, Q Hearts, K Spades, 2 Hearts, 6 Hearts, 3 Hearts, 8 Hearts, A Hearts, 3 Spades, K Diamonds, 10 Hearts, 7 Diamonds, 5 Spades, A Diamonds, 2 Clubs, A Clubs, A Spades, 5 Hearts, 2 Spades, 8 Spades, 2 Spades, J Spades, Q Hearts, 5 Clubs, 3 Spades, 3 Diamonds, 2 Hearts, A Diamonds, J Diamonds]
==============================================
After shuffle:44 cards
[3 Diamonds, Q Clubs, 5 Spades, 7 Diamonds, 5 Hearts, Q Hearts, 5 Diamonds, K Spades, 2 Hearts, A Spades, 10 Diamonds, Q Hearts, 3 Hearts, A Clubs, 3 Diamonds, 3 Spades, A Hearts, 2 Hearts, 6 Diamonds, A Diamonds, 10 Hearts, J Diamonds, 6 Spades, 7 Spades, 8 Clubs, 7 Diamonds, 8 Diamonds, K Diamonds, J Spades, 8 Spades, 3 Spades, A Hearts, 5 Clubs, A Spades, 3 Hearts, 2 Spades, 2 Spades, 2 Clubs, A Diamonds, 5 Spades, 8 Hearts, Q Clubs, 6 Hearts, 9 Clubs]
Hand 2:30 cards
[9 Clubs, 6 Hearts, Q Clubs, 8 Hearts, 5 Spades, A Diamonds, 2 Clubs, 2 Spades, 2 Spades, 3 Hearts, A Spades, 5 Clubs, A Hearts, 3 Spades, 8 Spades, J Spades, K Diamonds, 8 Diamonds, 7 Diamonds, 8 Clubs, 7 Spades, 6 Spades, J Diamonds, 10 Hearts, A Diamonds, 6 Diamonds, 2 Hearts, A Hearts, 3 Spades, 3 Diamonds]
Remain:14 cards
[3 Diamonds, Q Clubs, 5 Spades, 7 Diamonds, 5 Hearts, Q Hearts, 5 Diamonds, K Spades, 2 Hearts, A Spades, 10 Diamonds, Q Hearts, 3 Hearts, A Clubs]
==============================================
After shuffle:14 cards
[5 Diamonds, A Clubs, 5 Hearts, 10 Diamonds, Q Hearts, Q Clubs, A Spades, K Spades, 3 Diamonds, 2 Hearts, Q Hearts, 7 Diamonds, 5 Spades, 3 Hearts]
Hand 3:30 cards
*** In catch block:PlayingCardException:Error Msg: Not enough cards to deal
******************************************************************************************
- Sample output : java PJ4.VideoPoker
[A Spades, 10 Spades, Q Spades, J Spades, K Spades]
Royal Flush!
-----------------------------------
[9 Spades, 10 Spades, Q Spades, J Spades, K Spades]
Straight Flush!
-----------------------------------
[9 Spades, 10 Spades, Q Spades, J Spades, 8 Diamonds]
Straight!
-----------------------------------
[9 Spades, 10 Spades, Q Spades, J Spades, 5 Spades]
Flush!
-----------------------------------
[8 Spades, 8 Clubs, Q Spades, 8 Diamonds, 8 Hearts]
Four of a Kind!
-----------------------------------
[8 Spades, 8 Clubs, Q Spades, 8 Diamonds, J Spades]
Three of a Kind!
-----------------------------------
[8 Spades, 8 Clubs, J Diamonds, 8 Diamonds, J Spades]
Full House!
-----------------------------------
[8 Spades, 9 Diamonds, J Diamonds, 8 Diamonds, J Spades]
Two Pairs!
-----------------------------------
[3 Diamonds, 9 Diamonds, J Diamonds, 8 Diamonds, J Spades]
Royal Pair!
-----------------------------------
[3 Diamonds, 9 Diamonds, 3 Spades, 8 Diamonds, J Spades]
Sorry, you lost!
-----------------------------------
******************************************************************************************
$ java TestVideoPoker 1000
Payout Table Multiplier
=======================================
Royal Flush | 250
Straight Flush | 50
Four of a Kind | 25
Full House | 9
Flush | 6
Straight | 5
Three of a Kind | 3
Two Pairs | 2
Royal Pair | 1
-----------------------------------
Balance:$1000
Enter bet:200
Hand:[7 Hearts, 9 Diamonds, K Diamonds, A Spades, J Hearts]
Enter positions of cards to keep (e.g. 1 4 5 ): <-- replace all cards
Hand:[10 Hearts, 7 Spades, 8 Diamonds, 6 Clubs, 8 Clubs]
Sorry, you lost!
Your balance:$800, one more game (y or n)?y
Want to see payout table (y or n)y
Payout Table Multiplier
=======================================
Royal Flush | 250
Straight Flush | 50
Four of a Kind | 25
Full House | 9
Flush | 6
Straight | 5
Three of a Kind | 3
Two Pairs | 2
Royal Pair | 1
-----------------------------------
Balance:$800
Enter bet:200
Hand:[10 Hearts, Q Clubs, 5 Diamonds, 4 Hearts, 2 Clubs]
Enter positions of cards to keep (e.g. 1 4 5 ):1 2 3 4 5 <-- keep all cards
Hand:[10 Hearts, Q Clubs, 5 Diamonds, 4 Hearts, 2 Clubs]
Sorry, you lost!
Your balance:$600, one more game (y or n)?y
Want to see payout table (y or n)n
-----------------------------------
Balance:$600
Enter bet:200
Hand:[6 Diamonds, 8 Hearts, J Clubs, 5 Spades, 9 Hearts]
Enter positions of cards to keep (e.g. 1 4 5 ):1 5 <-- keep 2 cards
Hand:[6 Diamonds, 5 Diamonds, 3 Diamonds, 9 Diamonds, 9 Hearts]
Sorry, you lost!
Your balance:$400, one more game (y or n)?y
Want to see payout table (y or n)200
-----------------------------------
Balance:$400
Enter bet:200
Hand:[K Spades, 4 Hearts, J Spades, J Clubs, 5 Spades]
Enter positions of cards to keep (e.g. 1 4 5 ):3 4
Hand:[9 Clubs, Q Clubs, J Spades, J Clubs, 2 Clubs]
Royal Pair!
Your balance:$400, one more game (y or n)?y
Want to see payout table (y or n)n
-----------------------------------
Balance:$400
Enter bet:400
Hand:[2 Spades, 2 Diamonds, 3 Clubs, A Diamonds, 5 Hearts]
Enter positions of cards to keep (e.g. 1 4 5 ):1 2
Hand:[2 Spades, 2 Diamonds, K Hearts, 4 Clubs, 9 Spades]
Sorry, you lost!
Your balance is 0
Bye!
____________________________________________________________________________________________________________________________________
Playing Card.java
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 3
* Do not modify this class!
*/
class Card {
/* constant suits and ranks */
static final String[] Suit = {"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-3 (see Suit[] above) */
/* Constructor to create a card */
/* throw PlayingCardException if rank or suit is invalid */
public Card(int suit, int rank) throws PlayingCardException {
if ((rank < 1) || (rank > 13))
throw new PlayingCardException("Invalid rank:"+rank);
else
cardRank = rank;
if ((suit < 0) || (suit > 3))
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() { return Rank[cardRank] + " " + Suit[cardSuit]; }
/* Few quick tests here */
public static void main(String args[])
{
try {
Card c1 = new Card(3,1); // A Spades
System.out.println(c1);
c1 = new Card(0,10); // 10 Clubs
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 playing cards
* Use class Card to construct n * 52 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 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;
/**
* Constructor: Creates default one deck of 52 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()
{
// implement this method!
}
/**
* Constructor: Creates n decks (52 cards each deck) 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)
{
// 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 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 cards */
/* Loop 2 times: */
/* Deal 30 cards for 4 times */
/* Expect exception last time */
/* reset() */
public static void main(String args[]) {
System.out.println("******* Create 2 decks of cards ********* ");
Decks decks = new Decks(2);
for (int j=0; j < 2; j++)
{
System.out.println(" ************************************************ ");
System.out.println("Loop # " + j + " ");
System.out.println("Before shuffle:"+decks.remainSize()+" cards");
System.out.println(" "+decks);
System.out.println(" ============================================== ");
int numHands = 4;
int cardsPerHand = 30;
for (int i=0; i < numHands; i++)
{
decks.shuffle();
System.out.println("After shuffle:"+decks.remainSize()+" cards");
System.out.println(" "+decks);
try {
System.out.println(" Hand "+i+":"+cardsPerHand+" cards");
System.out.println(" "+decks.deal(cardsPerHand));
System.out.println(" Remain:"+decks.remainSize()+" cards");
System.out.println(" "+decks);
System.out.println(" ============================================== ");
}
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. Jacks or Better: a pair pays out only if the cards in the pair are Jacks,
* Queens, Kings, or Aces. Lower pairs do not pay out.
* 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,9,25,50,250};
private static final String[] goodHandTypes={
"Royal 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;
Decks(1);
}
/** This display the payout table based on multipliers and goodHandTypes arrays */
private void showPayoutTable()
{
System.out.println(" ");
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]+" | "+multipliers[i]);
}
System.out.println(" ");
}
/** 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 additional 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 keep
* 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("-----------------------------------");
// "Royal Pair" , "Two Pairs" , "Three of a Kind", "Straight", "Flush ",
// "Full House", "Four of a Kind", "Straight Flush", "Royal Flush" };
// set Four of a Kind
playerHand.clear();
playerHand.add(new Card(3,8));
playerHand.add(new Card(0,8));
playerHand.add(new Card(3,12));
playerHand.add(new Card(1,8));
playerHand.add(new Card(2,8));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Three of a Kind
playerHand.set(4, new Card(3,11));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Full House
playerHand.set(2, new Card(1,11));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Two Pairs
playerHand.set(1, new Card(1,9));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Royal Pair
playerHand.set(0, new Card(1,3));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// non Royal Pair
playerHand.set(2, new Card(3,3));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
/* Quick testCheckHands() */
public static void main(String args[])
{
VideoPoker pokergame = new VideoPoker();
pokergame.testCheckHands();
}
}
___________________________________________________________________________________________________________________
TestVideoPoker.java
/*************************************************************************************
*
* 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();
}
}
Explanation / Answer
PlayingCard.java
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 3
* Do not modify this class!
*/
class Card {
/* constant suits and ranks */
static final String[] Suit = {"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-3 (see Suit[] above) */
/* Constructor to create a card */
/* throw PlayingCardException if rank or suit is invalid */
public Card(int rank, int suit) throws PlayingCardException {
if ((rank < 1) || (rank > 13))
throw new PlayingCardException("Invalid rank:"+rank);
else
cardRank = rank;
if ((suit < 0) || (suit > 3))
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() { return Rank[cardRank] + " " + Suit[cardSuit]; }
/* Few quick tests here */
public static void main(String args[])
{
try {
Card c1 = new Card(1,3); // A Spades
System.out.println(c1);
c1 = new Card(10,0); // 10 Clubs
System.out.println(c1);
c1 = new Card(10,5); // generate exception here
}
catch (PlayingCardException e)
{
System.out.println("PlayingCardException: "+e.getMessage());
}
}
}
/** class Decks represents : n decks of playing cards
* Use class Card to construct n * 52 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 cards */
private List<Card> originalDecks;
/* this starts with n*52 cards deck from original deck */
/* it is used to keep track of remaining cards to deal */
/* see reset(): it resets dealDecks to a full deck */
private List<Card> dealDecks;
/* number of decks in this object */
private int numberDecks;
/**
* Constructor: Creates default one deck of 52 playing cards in originalDecks and
* copy them to dealDecks.
* initialize numberDecks=n
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & dealDecks
*/
public Decks()
{
// implement this method!
this(1);
}
/**
* Constructor: Creates n decks (52 cards each deck) of playing cards in // what do you mean by n number of decks?
* originalDecks and copy them to dealDecks.
* initialize numberDecks=n
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & dealDecks
*/
public Decks(int n)
{
// implement this method!
this.originalDecks = new ArrayList<Card>();
this.dealDecks = new ArrayList<Card>();
this.numberDecks= n;
createDeck(n);
Card[] holdCards = new Card[originalDecks.toArray().length];
holdCards = originalDecks.toArray(holdCards);
copyDeck(0, holdCards);
}
// private Card[] copyElements(){
// return null;
//}
private void createDeck(int i){
if(i!=0){
try{
this.originalDecks.add(new Card(1,0));
this.originalDecks.add(new Card(2,0));
this.originalDecks.add(new Card(3,0));
this.originalDecks.add(new Card(4,0));
this.originalDecks.add(new Card(5,0));
this.originalDecks.add(new Card(6,0));
this.originalDecks.add(new Card(7,0));
this.originalDecks.add(new Card(8,0));
this.originalDecks.add(new Card(9,0));
this.originalDecks.add(new Card(10,0));
this.originalDecks.add(new Card(11,0));
this.originalDecks.add(new Card(12,0));
this.originalDecks.add(new Card(13,0));
this.originalDecks.add(new Card(1,1));
this.originalDecks.add(new Card(2,1));
this.originalDecks.add(new Card(3,1));
this.originalDecks.add(new Card(4,1));
this.originalDecks.add(new Card(5,1));
this.originalDecks.add(new Card(6,1));
this.originalDecks.add(new Card(7,1));
this.originalDecks.add(new Card(8,1));
this.originalDecks.add(new Card(9,1));
this.originalDecks.add(new Card(10,1));
this.originalDecks.add(new Card(11,1));
this.originalDecks.add(new Card(12,1));
this.originalDecks.add(new Card(13,1));
this.originalDecks.add(new Card(1,2));
this.originalDecks.add(new Card(2,2));
this.originalDecks.add(new Card(3,2));
this.originalDecks.add(new Card(4,2));
this.originalDecks.add(new Card(5,2));
this.originalDecks.add(new Card(6,2));
this.originalDecks.add(new Card(7,2));
this.originalDecks.add(new Card(8,2));
this.originalDecks.add(new Card(9,2));
this.originalDecks.add(new Card(10,2));
this.originalDecks.add(new Card(11,2));
this.originalDecks.add(new Card( 12,2));
this.originalDecks.add(new Card(13,2));
this.originalDecks.add(new Card(1,3));
this.originalDecks.add(new Card(2,3));
this.originalDecks.add(new Card(3,3));
this.originalDecks.add(new Card(4,3));
this.originalDecks.add(new Card(5,3));
this.originalDecks.add(new Card(6,3));
this.originalDecks.add(new Card(7,3));
this.originalDecks.add(new Card(8,3));
this.originalDecks.add(new Card(9,3));
this.originalDecks.add(new Card(10,3));
this.originalDecks.add(new Card(11,3));
this.originalDecks.add(new Card(12,3));
this.originalDecks.add(new Card(13,3));}
catch(PlayingCardException ex){
System.out.println("PlayingCardException : "+ ex);
}
}
i--;
if(i!=0){
createDeck(i);}
}
private void copyDeck(int i, Card[] holdCards){
i =i;
dealDecks.add(holdCards[i]);
if(i!= holdCards.length-1 ){
i++;
copyDeck(i, holdCards);
}
}
/**
* Task: Shuffles cards in deal deck.
* Hint: Look at java.util.Collections
*/
public void shuffle()
{
// implement this method!
Collections.shuffle(dealDecks);
}
/**
* Task: Deals cards from the deal deck.
*
* @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 dealDecks
*
*/
public List<Card> deal(int numberCards) throws PlayingCardException
{
// implement this method!
//do random selection of total numberCards from the dealDeck.
List<Card> hand= new ArrayList<Card>();
int selectedIndex=0;
if(numberCards> dealDecks.size()){
throw new PlayingCardException("Not enough cards to deal");
}
else{
selectedIndex = dealDecks.size()-1;
for(int i=0; i<numberCards; i++){
hand.add(dealDecks.remove(selectedIndex));
selectedIndex--;
}
}
return hand;
}
/**
* Task: Resets deal deck by getting all cards from the original deck.
*/
public void reset()
{
// implement this method!
Card[] holdcards = new Card[52];
holdcards = originalDecks.toArray(holdcards);
copyDeck(0, holdcards);
}
/**
* Task: Return number of remaining cards in deal deck.
*/
public int remain()
{
return dealDecks.size();
}
/**
* Task: Returns a string representing cards in the deal deck
*/
public String toString()
{
return ""+dealDecks;
}
/* Quick test */
/* */
/* Do not modify these tests */
/* Generate 2 decks of cards */
/* Loop 2 times: */
/* Deal 30 cards for 4 times */
/* Expect exception last time */
/* reset() */
public static void main(String args[]) {
System.out.println("******* Create 2 decks of cards ********* ");
Decks decks = new Decks(2);
for (int j=0; j < 2; j++)
{
System.out.println(" ************************************************ ");
System.out.println("Loop # " + j + " ");
System.out.println("Before shuffle:"+decks.remain()+" cards");
System.out.println(" "+decks);
System.out.println(" ============================================== ");
int numHands = 4;
int cardsPerHand = 30;
for (int i=0; i < numHands; i++)
{
decks.shuffle();
System.out.println("After shuffle:"+decks.remain()+" cards");
System.out.println(" "+decks);
try {
System.out.println(" Hand "+i+":"+cardsPerHand+" cards");
System.out.println(" "+decks.deal(cardsPerHand));
System.out.println(" Remain:"+decks.remain()+" cards");
System.out.println(" "+decks);
System.out.println(" ============================================== ");
}
catch (PlayingCardException e)
{
System.out.println("*** In catch block : PlayingCardException : msg : "+e.getMessage());
}
}
decks.reset();
}
}
}
SimplePoker.java
import java.util.*;
public class SimplePoker {
// default constant values
private static final int startingBalance = 100;
private static final int numberOfCards = 5;
// default constant payout value and currentHand types
private static final int[] multipliers = { 1, 2, 3, 5, 6, 9, 25, 50, 250 };
private static final String[] goodHandTypes = { "Royal 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 static final Decks Decks(1);
// holding current poker 5-card hand, balance, bet
private List<Card> currentHand;
private int balance;
private int bet;
/** default constructor, set balance = startingBalance */
public SimplePoker() {
this(startingBalance);
balance = startingBalance;
currentHand = new ArrayList<Card>(5);
}
/** constructor, set given balance */
public SimplePoker(int balance) {
this.balance = balance;
currentHand = new ArrayList<Card>(5);
}
/**
* This display the payout table based on multipliers and goodHandTypes
* arrays
*/
private void showPayoutTable() {
System.out.println(" ");
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] + " | " + multipliers[i]);
}
System.out.println(" ");
}
/**
* Check current currentHand 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!
int clubsCount = 0;
int diamondsCount = 0;
int heartsCount = 0;
int spadesCount = 0;
boolean handFound = false;
for(int i = 0; i < 5; i++) {
if(currentHand.get(i).getSuit() == 0) {
clubsCount++;
}else if(currentHand.get(i).getSuit() == 1) {
diamondsCount++;
}else if(currentHand.get(i).getSuit() == 2) {
heartsCount++;
}else if(currentHand.get(i).getSuit() == 3) {
spadesCount++;
}
}
int[] rankCounter = new int[14];
for(int c = 0; c < 5; c++) {
for(int r = 1; r < 14; r++) {
if(currentHand.get(c).getRank() == r) {
rankCounter[r]++;
}
}
}
int[] sameRank = new int[4];
for(int s = 1; s < rankCounter.length; s++) {
if(rankCounter[s] > 0) {
sameRank[rankCounter[s] - 1]++;
}
}
if(clubsCount == 5 || diamondsCount == 5 || heartsCount == 5 || spadesCount == 5) {
if (sameRank[0] == 5) {
if(rankCounter[13] == 1 && rankCounter[12] == 1 && rankCounter[11] == 1
&& rankCounter[10] == 1 && rankCounter[1] == 1) {
balance += bet*multipliers[8];
System.out.println("Royal Flush!");
handFound = true;
} else if (sameRank[0] == 5){
for(int i = 13; i > 4; i--) {
if(rankCounter[i] == 1 && rankCounter[i - 1] == 1 && rankCounter[i - 2] == 1
&& rankCounter[i - 3] == 1 && rankCounter[i - 4] == 1) {
balance += bet*multipliers[7];
System.out.println("Straight Flush!");
handFound = true;
}
}
} else{
balance += bet*multipliers[4];
System.out.println("Flush!");
handFound = true;
}
} else {
balance += bet*multipliers[4];
System.out.println("Flush!");
handFound = true;
}
}else if (sameRank[1] == 1) {
if(sameRank[0] == 3) {
if(rankCounter[13] == 2 || rankCounter[12] == 2 || rankCounter[11] == 2 || rankCounter[1] == 2) {
//royal pair!
balance += bet*multipliers[0];
System.out.println("Royal Pair!");
handFound = true;
} else {
System.out.println("Sorry, you lost.");
}
}else if (sameRank[2] == 1) {
balance += bet*multipliers[4];
System.out.println("Full House!");
handFound = true;
}
} else if(sameRank[0] == 1) {
if(sameRank[1] == 2) {
balance += bet*multipliers[1];
System.out.println("Two Pairs!");
handFound = true;
} else if (sameRank[3] == 1) {
balance += bet*multipliers[6];
System.out.println("Four of a Kind!");
handFound = true;
}
} else if (sameRank[2] == 1) {
balance += bet*multipliers[2];
System.out.println("Three of a Kind!");
handFound = true;
} else if (sameRank[0] == 5) {
for(int i = 13; i > 5; i--) {
if(rankCounter[i] == 1 && rankCounter[i - 1] == 1 && rankCounter[i - 2] == 1
&& rankCounter[i - 3] == 1 && rankCounter[i - 4] == 1) {
balance += bet*multipliers[3];
System.out.println("Straight!");
handFound = true;
}
}
if (handFound == false) {
System.out.println("Sorry, you lost.");
}
} else if (handFound == false){
System.out.println("Sorry, you lost.");
}
}
/*************************************************
* add new private methods here ....
*
*************************************************/
public void play() {
// implement this method!
Scanner user = new Scanner(System.in);
Scanner keepScanner = new Scanner(System.in);
keepScanner.useDelimiter(System.getProperty("line.separator"));
Scanner numbers;
showPayoutTable();
String answer;
char wantToPlay = 'y';
balance = startingBalance;
String cardsToKeep;
int[] keepArray = new int[5];
while((!(balance == 0)) && (!(wantToPlay == 'n'))){
currentHand.clear();
oneDeck.reset();
System.out.println("Balance: $" + balance);
do{
System.out.print("Enter Bet: ");
bet = user.nextInt();
if(bet > balance){
System.out.println("insufficient funds!");
}else if(bet < 0) {
System.out.println("Not possible, sir!");
}
}while(bet > balance || bet < 0);
balance = (balance - bet);
oneDeck.shuffle();
try{
currentHand.addAll(0, oneDeck.deal(numberOfCards));
}catch(PlayingCardException e){
System.out.println(e);
}
System.out.println(currentHand);
System.out.print("Enter positions of cards to keep (e.g. 1 4 5 ) or 0 to switch all cards: ");
cardsToKeep = keepScanner.next();
numbers = new Scanner(cardsToKeep);
int k = 0;
while(numbers.hasNextInt()) {
keepArray[k] = numbers.nextInt();
k++;
}
if (keepArray[0] == 0) {
for(int r = 1; r < 6; r++) {
try {
currentHand.set((r-1), oneDeck.deal(1).get(0));
} catch (PlayingCardException e) {
}
}
} else {
boolean replace;
for(int r = 1; r < 6; r++) {
for(int c = 0; c < k; c++) {
replace = true;
if(r == keepArray[c]) {
replace = false;
break;
}
if((c == (k-1)) && (replace)) {
try {
currentHand.set((r-1), oneDeck.deal(1).get(0));
} catch (PlayingCardException e) {
}
}
}
}
}
System.out.println(currentHand);
checkHands();
System.out.println("Your balance is " + balance);
if(balance == 0){
System.out.println("Game Over.");
} else {
System.out.print("one more game (y or n)?");
answer = user.next();
wantToPlay = answer.charAt(0);
if(wantToPlay == 'y'){
System.out.print("Want to see payout table (y or n)");
answer = user.next();
if((answer.charAt(0)) == 'y') {
showPayoutTable();
}
} else if(wantToPlay == 'n') {
System.out.println("Thanks for playing!");
}
}
}
}
/**
* Do not modify this. It is used to test checkHands() method checkHands()
* should print your current hand type
*/
public void testCheckHands() {
try {
currentHand = new ArrayList<Card>();
// set Royal Flush
currentHand.add(new Card(1, 3));
currentHand.add(new Card(10, 3));
currentHand.add(new Card(12, 3));
currentHand.add(new Card(11, 3));
currentHand.add(new Card(13, 3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Straight Flush
currentHand.set(0, new Card(9, 3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Straight
currentHand.set(4, new Card(8, 1));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Flush
currentHand.set(4, new Card(5, 3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// "Royal Pair" , "Two Pairs" , "Three of a Kind", "Straight",
// "Flush ",
// "Full House", "Four of a Kind", "Straight Flush", "Royal Flush"
// };
// set Four of a Kind
currentHand.clear();
currentHand.add(new Card(8, 3));
currentHand.add(new Card(8, 0));
currentHand.add(new Card(12, 3));
currentHand.add(new Card(8, 1));
currentHand.add(new Card(8, 2));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Three of a Kind
currentHand.set(4, new Card(11, 3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Full House
currentHand.set(2, new Card(11, 1));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Two Pairs
currentHand.set(1, new Card(9, 1));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Royal Pair
currentHand.set(0, new Card(3, 1));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// non Royal Pair
currentHand.set(2, new Card(3, 3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/* Quick testCheckHands() */
public static void main(String args[]) {
SimplePoker mypokergame = new SimplePoker();
mypokergame.play();
mypokergame.testCheckHands();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.