In java Extra Credit Video Poker Objective: Create a game of video poker with a
ID: 3719720 • Letter: I
Question
In java
Extra Credit
Video Poker
Objective:
Create a game of video poker with a graphical user interface (GUI). The player should start with $100, and then the system deals out 5 playing cards. After the player sees the cards they are then asked to wager $10, $20, $50, or $100. Next the user picks which cards they wish to throw away, and then the system deals more cards in their place. Once this has concluded money are awarded by these criteria:
Nothing – Player loses their wager
Pair – Player gets back their wager
Two Pair – Player wins wager x 2
3 of a kind – Payer wins wager x 3
Full House – Player wins wager x 6
Flush – Player wins wager x 10
Straight – Player wins wager x 15
4 of a kind – Player wins wager x 20
Royal Flush – Player wins wager x 100
If the player’s cash is less than or equal to 0 the game is over, and they restart with $100. Also once they close the program it should store the score in a file that once they restart it they begin with the score they once had.
Explanation / Answer
As per your requirement I have written code which fulfill all your requirements please follow it step by step and i have included comments for better understanding
/**
* PokerhandObject.java
* here below one is code for a class that represents a Poker handObject. It is mainly
* useful for a draw poker game in which the hand is made up of
* 5 Cards. The ranks are as follows: 9 - Royal Flush; 8 - Straight
* Flush; 7 - Four of a Kind; 6 - Full House; 5 - Flush; 4 - Straight;
* 3 - Three of a Kind; 2 - Two Pair; 1 - Pair; 0 - Bust (nothing)
*/
public class PokerhandObject {
private CardObject [] handObject;
private int rank;
/**
* PokerhandObject(CardObject cElement1, CardObject cElement2, CardObject cElement3, CardObject cElement4, CardObject cElement5)
* Input: 5 CardObject objects that will make up the poker hand
* Output: none
* Description: here we are going to Creates a PokerhandObject object with the 5 Cards
* passed in as parameters. It uses the evalRank() method
* to give assign a rank to the handObject.
*/
public PokerhandObject(CardObject cElement1, CardObject cElement2, CardObject cElement3, CardObject cElement4, CardObject cElement5) {
handObject = new CardObject[5];
handObject[0] = cElement1;
handObject[1] = cElement2;
handObject[2] = cElement3;
handObject[3] = cElement4;
handObject[4] = cElement5;
evalRank();
}//end Constructor
/**
* PokerhandObject()
* Input: none
* Output: none
* Description: Creates a random poker handObject through a call to the
* other poker handObject constructor and the random CardObject constructor.
*/
public PokerhandObject() {
this(new CardObject(), new CardObject(), new CardObject(), new CardObject(), new CardObject());
}//end Constructor
/**
* getRank()
* Input: none
* Output: the output will be an integer representing the rank of the CardObject
*/
public int getRank() {
return rank;
}//end getRank
/**
* hasAce()
* Input: none
* Ouptut: boolean - true if hand has an ace, false otherwise
* Description: A simple utility method used to determine whether
* the handObject has an ace or not
*/
public boolean hasAce() {
for(int i = 0; i < 5; i++)
if(handObject[i].getRank() == 14)
return true;
return false;
}//end hasAce
/**
* gameToString()
* Input: none
* Output: a String representation of the handObject
* Description: here A method that is useful in the DrawPoker
* game - it will prints out the handObject numbered without printing
* out the rank (the user should figure that out!)
*/
public String gameToString() {
String result = new String("");
for(int i = 0; i < handObject.length; i++)
result += "(" + (i+1) + ") " + handObject[i] + " ";
return result;
}//end gameToString
/**
* rankToString(int rank)
* Input: an integer representing the rank
* Output: a string representation of the corresponding rank
* Description: A static method that might be useful for printing
* out the rank.
*/
public static String rankToString(int rank) {
switch(rank) {
case 0: return "Bust ";
case 1: return "Pair ";
case 2: return "Two Pair ";
case 3: return "3 of a Kind ";
case 4: return "Straight ";
case 5: return "Flush ";
case 6: return "Full House ";
case 7: return "4 of a Kind ";
case 8: return "Straight Flush";
case 9: return "Royal Flush ";
default: return "Error ";
}
}//end rankToString
/**
* draw(int index, CardObject c)
* Input: An integer representing the index of the
* CardObject to be disCardObjected and the CardObject with which
* it should be replaced.
* Output: none
* Description: Replaces the CardObject at the corresponding index
* with a new CardObject.
*/
public void draw(int index, CardObject c) {
if(0 <= index && index <= 4) {
handObject[index] = c;
}
evalRank();
}//end draw
/**
* hasGoodPair()
* Input: none
* Output: output will be boolean which is true if the handObject
* is a pair of Jacks or better
*/
public boolean hasGoodPair() {
boolean result;
result = false;
if(rank == 1) {
for(int i = 0; i < 5; i++) {
for(int j = i+1; j < 5; j++)
if(handObject[i].getRank() == handObject[j].getRank() && handObject[i].getRank() > 10)
result = true;
}
}
return result;
}//end hasGoodPair
/**
* toString()
* Input: none
* Output: String representing the handObject
* Description: A method that returns the String representation
* of this CardObject handObject - including the rank.
*/
public String toString() {
evalRank();
String result = "";
switch(rank) {
case 0:
result += "Bust";
break;
case 1:
result += "Pair";
break;
case 2:
result += "Two Pair";
break;
case 3:
result += "Three of a Kind";
break;
case 4:
result += "Straight";
break;
case 5:
result += "Flush";
break;
case 6:
result += "Full House";
break;
case 7:
result += "Four of a Kind";
break;
case 8:
result += "Straight Flush";
break;
case 9:
result += "Royal Flush";
break;
default:
result += "Nothing";
break;
}
result += ": " + handObject[0] + ", " + handObject[1] + ", " + handObject[2] + ", " + handObject[3] + ", " + handObject[4] +" ";
return result;
}//end toString
/**
* evalRank()
* Input: none
* Output: none
* Description: A method that computes the rank of the handObject.
*/
private void evalRank() {
CardObject [] sortedhandObject = new CardObject[5];
for(int i = 0; i < 5; i++)
sortedhandObject[i] = handObject[i];
this.sort(sortedhandObject);
int pairIndex = -1;
rank = 0; //assume its a BUST
//check for pair
for(int i = 0; i < 4; i++)
if(sortedhandObject[i].getRank() == sortedhandObject[i+1].getRank()) {
pairIndex = i;
rank = 1;
i = 4;
}
//check for 2 pair
if(rank == 1) {
for(int i = pairIndex + 2; i < 4; i++)
if(sortedhandObject[i].getRank() == sortedhandObject[i+1].getRank())
rank = 2;
}
//check for 3 of a kind or full house
for(int i = 0; i < 3; i++)
if(sortedhandObject[i].getRank() == sortedhandObject[i+1].getRank() && sortedhandObject[i+1].getRank() == sortedhandObject[i+2].getRank()) {
rank = 3;
if(i==0 && sortedhandObject[3].getRank()==sortedhandObject[4].getRank() || i==2 && sortedhandObject[0].getRank() == sortedhandObject[1].getRank())
rank = 6;
}
//check for 4 of a kind
for(int i = 0; i < 2; i++)
if(sortedhandObject[i].getRank() == sortedhandObject[i+1].getRank() && sortedhandObject[i+1].getRank() == sortedhandObject[i+2].getRank() &&
sortedhandObject[i+2].getRank() == sortedhandObject[i+3].getRank()) {
rank = 7;
}
//check for straight (if we haven't already found any pairs)
if(rank == 0)
if((sortedhandObject[4].getRank() - sortedhandObject[0].getRank() == 4) ||
(sortedhandObject[3].getRank() - sortedhandObject[0].getRank() == 3 && sortedhandObject[4].getRank() == 14 && sortedhandObject[0].getRank() == 2)) {
rank = 4;
}
//check for flush (if we haven't already found any pairs)
boolean flush;
if(rank == 0 || rank == 4) {
flush = true;
for(int i = 0; i < 4; i++)
if(sortedhandObject[i].getSuit() != sortedhandObject[i+1].getSuit())
flush = false;
if(flush && rank == 4)
rank = 8; //straight flush!
else if(flush)
rank = 5;
}
//here we will check for royal flush (if it's a straight flush)
if(rank == 8 && sortedhandObject[4].getRank() == 14 && sortedhandObject[0].getRank() == 10)
rank = 9; //royal flush!
}//end evalRank
/**
* sort(CardObject [] a)
* Input: A CardObject array
* Output: none
* Description: Sorts an array of CardObjects using a selection sort
* algorithm. Used by the evalRank() method.
*/
private void sort(CardObject [] a) {
CardObject temporaryElement;
int minindexElement;
for(int i = 0; i < a.length; i++) {
minindexElement = i;
for(int j = i; j < a.length; j++) {
if(a[minindexElement].isHigher(a[j]))
minindexElement = j;
}
//below one is code for swap the elements at i and j
temporaryElement = a[minindexElement];
a[minindexElement] = a[i];
a[i] = temporaryElement;
}
}//end sort
}//end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.