Question

Write a Java program that deals a five-card poker hand and then determines which of the...

Write a Java program that deals a five-card poker hand and then determines which of the following hands are contained in it: high card, pairs, two pair, three of a kind, flush. Test only for those hands. Use the numbers from 0 to 51 to represent the cards of the poker deck.

1. To deal the cards, your main method should call a secondary method that selects a random card. The secondary method should accept 5 integers that represent the cards that have been dealt already, or -1 if a card has not been dealt. It should return an integer for the new card, and it should avoid dealing duplicate cards. You can avoid dealing duplicate cards by first generating a random card, checking if it’s been dealt (i.e. it’s in the input) and then generating a new card. Use a loop to repeat.

2. Write five methods called highCardTest(), pairTest(), twoPairTest() , threeOfaKind(), and flush(), that each check the hand if it contains the corresponding poker hand. In other words, they should accept the 5 cards as input (return an error if not all cards are dealt), and they should return true if the hand can be found in the 5 input cards. They should return false otherwise.

3. Write a sixth method scoreHand() that tests the cards to determine what the highest possible hand is. It accepts all five cards as input. First test the flush. If the flush fails, test the three of a kind, in descending order of poker hand hierarchy. The highest hand that can be found should be found and returned as a string that describes the hand and the output, like “You have a diamond flush”.

4. Call the method scoreHand() in the main method, then ask the user if they’d like to score another random hand. Loop if the user answers “y”, quit if they answer “n”, and ask again if they answer anything else.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 import java.util.Arrays; import java.util.Scanner; public class Poker { private final static int NO_OF_CARDS = 5; /** * Counts the number of times card appears in the hand and returns it * Pre-condition : hand should be sorted * param hand * param card * return */ public static int countCard(int[] hand, int card) { int count = 0; for(int i = 0 ; i < NO_OF_CARDS ; i++) { if(hand[i] == card) count += 1; else { if(count > 0) //If a match is already found and the card in hand does not match the card break break; //because the hand array is sorted and all cards with same value will be in the order } } return count; } /** * Checks if a hand contains a pair * Pre-condition : hand should be sorted * param hand * return */ public static boolean containsPair(int hand[]) { for(int i = 0 ; i < NO_OF_CARDS ; i++) { if(countCard(hand, hand[i]) == 2) return true; } return false; } /** * Checks if a hand contains two pair * Pre-condition : hand should be sorted * param hand * return */ public static boolean containsTwoPair(int hand[]) { boolean pairOne = false; boolean pairTwo = false; for(int i = 0 ; i < NO_OF_CARDS ; i++) { if(pairOne && pairTwo) //If both pairs are found, return true return true; if(countCard(hand, hand[i]) == 2) { if(!pairOne) pairOne = true; else if(!pairTwo) pairTwo = true; } } return false; } /** * Checks if a hand contains 3 matching cards * Pre-condition : hand should be sorted * param hand * return */ public static boolean containsThreeOfaKind(int hand[]) { for(int i = 0 ; i < NO_OF_CARDS ; i++) { if(countCard(hand, hand[i]) == 3) return true; } return false; } /** * Checks if a hand contains a pair and 3 matching cards * Pre-condition : hand should be sorted * param hand * return */ public static boolean containsFullHouse(int hand[]) { boolean pair = false; boolean threeCards = false; for(int i = 0 ; i < NO_OF_CARDS ; i++) { if(pair && threeCards) //If a pair and 3 matching cards are found, return true return true; if(countCard(hand, hand[i]) == 2) pair = true; if(countCard(hand, hand[i]) == 3) threeCards = true; } return false; } /** * Checks if a hand contains 4 matching cards * Pre-condition : hand should be sorted * param hand * return */ public static boolean containsFourOfaKind(int hand[]) { for(int i = 0 ; i < NO_OF_CARDS ; i++) { if(countCard(hand, hand[i]) == 4) return true; } return false; } /** * Checks if a hand contains card values in order * Pre-condition : hand should be sorted * param hand * return */ public static boolean containsStraight(int hand[]) { for(int i = 1 ; i < NO_OF_CARDS ; i++) { if((hand[i] - hand[i - 1]) != 1) return false; } return true; } public static void main(String[] args) { int[] hand = new int[NO_OF_CARDS]; //Array to hold cards Scanner input = new Scanner(System.in); //Read input from keyboard System.out.println("Enter five numeric cards, no face cards. Use 2 - 9."); for(int i = 0 ; i < NO_OF_CARDS ; i++) { while(true) { System.out.println("Card " + (i + 1) + ": "); hand[i] = input.nextInt(); //Clear keyboard buffer input.nextLine(); if((1 < hand[i]) && (hand[i] < 10)) break; else System.out.println("Invalid input. Please try again."); } } //Close scanner input.close(); //Sort the input array Arrays.sort(hand); //Check poker hand if(containsStraight(hand)) //STRAIGHT System.out.println("Straight!"); else { if(containsFourOfaKind(hand)) //FOUR OF A KIND System.out.println("Four of a kind!"); else { if(containsFullHouse(hand)) //FULL HOUSE System.out.println("Full House!"); else { if(containsThreeOfaKind(hand)) //THREE OF A KIND System.out.println("Three of a kind!"); else { if(containsTwoPair(hand)) //TWO PAIR System.out.println("Two pair!"); else { if(containsPair(hand)) //PAIR System.out.println("Pair!"); else //HIGH CARD System.out.println("High Card!"); } } } } } } } /*SAMPLE OUTPUT: Enter five numeric cards, no face cards. Use 2 - 9. Card 1: 3 Card 2: 4 Card 3: 3 Card 4: 4 Card 5: 3 Full House!*/

you can put it in a loop to continusly take a input and to produce output.

Hope this helps...!

Add a comment
Know the answer?
Add Answer to:
Write a Java program that deals a five-card poker hand and then determines which of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card...

    7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains a pair. b) Determinewhetherthehandcontainstwopairs. c) Determine whether the hand contains three of a kind (e.g., three jacks). d) Determinewhetherthehandcontainsfourofakind(e.g.,fouraces). e) Determine whether the hand contains a flush (i.e., all five cards of the same suit). f) Determine whether the hand contains a straight (i.e., five cards of...

  • Need help writing this program! Thanks in advance. Project4 Specifications: Determine Poker Hand A poker hand...

    Need help writing this program! Thanks in advance. Project4 Specifications: Determine Poker Hand A poker hand can be stored in a two-dimensional array. The statement: Dim aryintCards(4,14) as Integer (See AryCardExample.xlsx excel spreadsheet) declares an array which the first subscript 1-4 are the four suits and the second subscript ranges over the card denominations: 1-14: Ace, 2, ..., 10, Jack, Queen, King, Ace (repeated). First subscript of 0 is Total of each denomination. Second subscript of 0 is Total of...

  • 5. In a poker game, 5 cards are dealt from a standard 52 card deck that...

    5. In a poker game, 5 cards are dealt from a standard 52 card deck that has been well shuffled. You are the only player in this scenario. (Note: if you are not familiar with poker hands, you may want to look up what some of these are online-also check out Chapter 23 in the textbook.) a) How many 5 card hands are possible? b) What is the probability that you are dealt two pairs? c) What is the probability...

  • 2. A hand of 5-card draw poker is a simple random sample from the standard deck...

    2. A hand of 5-card draw poker is a simple random sample from the standard deck of 52 cards. How many 5 draw poker hands are there? In 5-card stud poker, the cards are dealt sequentially and the order of appearance is important. How many 5-stud poker hands are there? 3. How many hands of 5-draw poker contain the ace of hearts? What is the probability that a 5-card draw hand contains the ace of hearts?

  • 2. A hand of 5-card draw poker is a simple random sample from the standard deck...

    2. A hand of 5-card draw poker is a simple random sample from the standard deck of 52 cards. How many 5 draw poker hands are there? In 5-card stud poker, the cards are dealt sequentially and the order of appearance is important. How many 5-stud poker hands are there? 3. How many hands of 5-draw poker contain the ace of hearts? What is the probability that a 5-card draw hand contains the ace of hearts?

  • 1. In a standard five-card draw poker, each player receives 5 cards from a standard deck...

    1. In a standard five-card draw poker, each player receives 5 cards from a standard deck of 52 cards. Some possible hands in this game are su d in Table 1 below1 (a) Create a sample space to describe all possible hands Bill can play with Let E be the event that the hand contains at least two cards of the same rank and F be the event that the hand contains at least three cards of the same rank...

  • (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card...

    (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains two pairs b) Determine whether the hand contains a full house (i.e., three of a kind with pair). c) Determinewhetherthehandcontainsastraight flush (i.e.,fivecardsofconsecutivefacevalues). d) Determine whether the hand contains a flush (i.e., five of the same suit) #include <stdio.h> #include <stdlib.h> #include <time.h> #define SUITS 4 #define FACES 13 #define CARDS...

  • python . Write the function poker_hand that takes a list of exactly five distinct Card objects...

    python . Write the function poker_hand that takes a list of exactly five distinct Card objects as an argument, analyzes the list, and returns one of the following strings that describes the hand: "Four of a kind' (four cards of the same rank) 'Full house' (three cards of one rank, and two cards of a different rank) . 'Flush' (five cards of the same suit) 'Three of a kind' (exactly three cards of the same rank) • 'One pair' (at...

  • I need to write a vb program for a poker game that uses a two-dimensional array......

    I need to write a vb program for a poker game that uses a two-dimensional array... Here is the problem: A poker hand can be stored in a two-dimensional array. The statement Dim hand(3, 12) As Integer declares an array with 52 elements, where the first subscript ranges over the four suits and the second subscript ranges over the thirteen denominations. A poker hand is specified by placing 1%u2019s in the elements corresponding to the cards in the hand. Write...

  • please do all steps clearly!!! A five-card hand is random dealt from a standard deck. We...

    please do all steps clearly!!! A five-card hand is random dealt from a standard deck. We can classify a five-card hand into following possibilities: 1. Calculate the number possible five-card hands that are: Two denominations o Four of a kind: four cards from one denomination, one from another denomination o Full house: three cards from one denomination, two from another denomination . Three denominations o Three of a kind: three cards from one denomination, the other two from two different...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT