Question
You are required to write a Java application program that requests a number from the end user to decide to play or quit the poker
You are required to write a Java application program that requests a number from the end user to decide to play or quit the poker game and reports how much you win this hand and total winnings. If no input is provided your program should be quit as you input number 2. If the input number is neither 1 nor 2, your program should reports it is a invalid selection. When you finished a hand you can choose whether play again or not.
And the following is the rule which determines how much you can win in the poke game. A Straight Flush (Rank 2) (win: 80c) is five cards of the same suit in sequence, such as 9-8-7-6-5 of hearts. Ranked by the top card, so that A-K-Q-J-10 is the best straight flush, also called a Royal Flush (Rank 1) (win: 90c). Four of a kind (Rank 3) (win: 70c) is four cards of the same rank accompanied by what is termed a kicker, for example, K-K-K-K-2. A Full House (Rank 4) (win: 60c) is three cards of one rank accompanied by two of another, for example, Q-Q-Q-9-9. A Flush (Rank 5) (win: 50c) is five cards of the same suit, for example J-9-8-7-3 of spades. A Straight (Rank 6) (win: 40c) is five cards in sequence. While an ace may play either high or low as in A-K-Q-J-10 and 5-4-3-2-A, respectively. You may assume that the ace only plays high. Around the corner straights as in 3-2-A-K-Q are not permitted. Three of a kind (Rank 7) (win: 30c) is three cards of the same rank and two kickers of different ranks, for example J-J-J-5-2. Two pair (Rank 8) (win: 20c) is two cards of one rank, two cards of another rank and one kicker of another rank, for example, J-J-4-4-2. One pair (Rank 9) (win: 10c) is two cards of one rank accompanied by three kickers of different ranks, for example, Q-Q-9-8-2.
a) Extend the Card class by creating a constructor method without arguments but initialize the attributes value with 0,suit with 0, name with empty string. public class Card { private int value; private int suit; private String name; } b) Based on this question, you should identify and complete the code for any other methods the card class should contain. c) Create a second class named OOPoker. In this second class you should create an array of objects of type card. For the purpose of this question you are required to instantiate 5 such objects in your main() method. d) Extend the OOPoker class by creating 9 methods to realize the game and call these methods from your main() method.
The gaps are required to be filled:
public class Card { private int value; private int suit; private String name; Card() { ____(1)______; _____(2)_____; _____(3)_____; } public void setValue(int aValue) { value =___(4)_____; } public void setSuit(int aSuit) { suit = __(5)_____; } public void setName(String __(6)_______) { name = aName; } public int getValue() { return ___(7)____; } public int getSuit() { return _(8)_____; } public String getName() { return ___(9)______; } } import java.text.*; import javax.swing.JOptionPane; public class OOPoker { public static String userMessage = ""; public static double totalWinnings = 0.0; public static void main(String[] args) { int handSize = 5, winType = 0; int[] uniqueNumbers = new____(10)_______; Card ___(11)_______ = new Card[handSize]; for (int count = 0; count < Cards.length; count++) Cards[count] = new ___(12)______; String selection = "", playAgain = ""; do { while (!(selection.equals("1")) && !(selection.equals("2"))) { selection = JOptionPane.showInputDialog(null, "1. Play Poker " + "2. Quit " + "Please enter your selection"); if (selection == null) selection = "2"; else if (!(selection.equals("1")) && !(selection.equals("2"))) JOptionPane.showMessageDialog(null, "Invalid selection - please try again "); } if (selection.equals("1")) { userMessage = ""; generateUniqueHand(uniqueNumbers); determineSuitsAndValuesOfCards(uniqueNumbers, Cards); assignNamesToCards(__(13)_______); bubbleSort(Cards); displayCardsToEndUser(Cards); winType = evaluateHandOfCards(Cards); displayTypeOfWinIfAny(winType); displayAmountWonIfAnything(winType); JOptionPane.showMessageDialog(null,userMessage); do { playAgain = JOptionPane.showInputDialog(null, "Would you like to play another game? (y/n)"); if (playAgain == null) playAgain = "N"; else playAgain = playAgain.toUpperCase(); } while (!(playAgain.equals("Y")) && !(playAgain.equals("N"))); } if (selection.equals("2")) playAgain = "N"; } while (playAgain.equals("Y")); } public static void generateUniqueHand(int[] cards) { int uniqueNumbersRequired = cards.length, aRandomNumber; int index = 0, duplicateIndex, deckSize = 52; while (index < uniqueNumbersRequired) { aRandomNumber = (int) (Math.random() * _(14)________);cards[index] = aRandomNumber; duplicateIndex = 0; while (cards[duplicateIndex] != aRandomNumber) duplicateIndex++; if (index == duplicateIndex) ___(15)____________; } } public static void determineSuitsAndValuesOfCards(int[] numbers, Card[] Cards) { for (int count = 0; count < ___(16)_____; count++) { Cards[count].setSuit((numbers[count] / 13)); Cards[count].setValue((numbers[count] % 13)); } } public static void assignNamesToCards(Card[] __(17)____) { String cardName = ""; for (int i = 0; i < Cards.length; i++) { switch(Cards[i].getValue()) { case 0: cardName += "Two of "; break; case 1: cardName += "Three of "; break; case 2: cardName += "Four of "; break; case 3: cardName += "Five of "; break; case 4: cardName += "Six of "; break; case 5: cardName += "Seven of "; break; case 6: cardName += "Eight of "; break; case 7: cardName += "Nine of "; break; case 8: cardName += "Ten of "; break; case 9: cardName += "Jack of "; break; case 10: cardName += "Queen of "; break; case 11: cardName += "King of "; break; case 12: cardName += "Ace of "; } switch(Cards[i].getSuit()) { case 0: cardName += "Clubs"; break; case 1: cardName += "Diamonds"; break; case 2: cardName += "Hearts"; break; case 3: cardName += "Spades"; } Cards[i].setName(cardName); cardName = ""; } } public static void bubbleSort(____(18)__________) { int pass, comparison; Card ___(19)_________ = new Card(); for (pass = 1; pass <= Cards.length - 1; pass++) { for (comparison = 1; comparison <= Cards.length - pass; comparison++) { if (Cards[comparison - 1].getValue() < ______(20)___________) { SwapCard = Cards[comparison - 1]; Cards[comparison - 1] = Cards[comparison]; Cards[comparison] = SwapCard; } } } } public static void displayCardsToEndUser(Card[] Cards) { userMessage += "Your hand of cards is: "; _________(21)______________ __________(22)______________; } public static boolean cardsOfSameSuit(Card[] Cards) { boolean sameSuit = true; for (int i = 0; (i < Cards.length - 1) && sameSuit; i++) if (Cards[i].___(23)______!= Cards[i + 1]._(24)_________) sameSuit = false; return __(25)_________; } public static boolean cardsInConsecutiveDescendingSequence(Card[] Cards) { boolean consecutiveCards = true; for (int i = 0; i < Cards.length - 1 && consecutiveCards; i++) if (Cards[i]._(26)____________!= Cards[i + 1].__(27)______ + 1) consecutiveCards = false; return consecutiveCards; } public static int checkOtherPossibleCombinations(__(28)___________) { boolean continueCardComparison; int sameKind = 0; for (int i = 0; (i < __(29)________); i++) { continueCardComparison = true; for (int j = i + 1; j < Cards.length && continueCardComparison; j++) { if (Cards[i].getValue() == Cards[j].getValue()) sameKind++; else continueCardComparison = false; } } return ___(30)_________; } public static int evaluateHandOfCards(Card[] Cards) { int winType = 0; if (cardsOfSameSuit(Cards)) { if (_____(31)______________________) { if (Cards[0].getValue() == 12) winType = 9; else winType = 8; } else winType = 7; } else { if (cardsInConsecutiveDescendingSequence(Cards)) winType = 5; else winType = checkOtherPossibleCombinations(Cards); } return winType; } public static void displayTypeOfWinIfAny(int __(32)____________) { switch(winType) { case 0: userMessage += " Not a winning hand "; break; case 1: userMessage += " One pair "; break; case 2: userMessage += " Two pair "; break; case 3: userMessage += " Three of a kind "; break; case 4: userMessage += " Full house "; break; case 5: userMessage += " Straight "; break; case 6: userMessage += " Four of a kind "; break; case 7: userMessage += " Flush "; break; case 8: userMessage += " Straight flush ";___(33)______; case 9: userMessage += " Royal flush "; } } public static void displayAmountWonIfAnything(int winType) { double[] money = {0.0, 0.1, 0.2, 0.3, 0.6, 0.4, 0.7, 0.5, 0.8, 0.9}; NumberFormat aFormatter = NumberFormat.getCurrencyInstance(); userMessage += " For this hand you win: " + aFormatter.format(money[winType]); totalWinnings += money[winType]; userMessage += " Total winnings to date are: " + aFormatter.format(totalWinnings); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started