Question
Overview Write a program that plays a hand of five-card draw (a basic Poker variant) between one player and the computer. Your program should use
Overview
Write a program that plays a hand of five-card draw (a basic Poker variant) between one player and the computer. Your program should use the following rules:
- When the hands are first dealt display player cards, but not the computer cards. After the discard, display both hands.
- After the discards, display both hands, print the game-winner and the winning hand.
- Supplied are the card class and Tester class.
Information
You will need the following enumerations:
- Suit - model the suit of a card. Must work with the supplied Card class.
- Face - models the face value of a card. Should contain the name of the card, the points, and the rank of the card. We will rank the cards from 1 to 13 (Ace to King) must work with the Card class. When you built the card showdown lab, the face enumeration did not include the rank, it should now.
All card points are the face value of the card where Ace is 1 and Ten of clubs is a 10. All picture cards are worth 10 points.
- WinHand - models the winning hands of poker as well as an invalid state. It must detect the following hands (shown in order of rank). Below is a picture of the different hand combinations.
- Royal Flush
- Straight flush
- Four of a kind
- Full house
- Straight
- Flush
- Three of a kind
- Two pair
- Pair
- HighCard (in the absence of all other hands, high card wins)
You will need the following Classes:
- Card - models a playing card. (This class is supplied).
- Hand - models a hand of five-card draw. (This class is partly supplied).
- Fields: Whatever fields are needed to represent a single hand of cards for one player. You must use an array for your Cards. All fields must be private.
- Constructor that accepts the number of cards in a hand.
- A no-parameter constructor that creates an array that can hold 5 cards. This constructor must call the other constructor to accomplish its goal.
- setCard - sets a single card in the hand, (you can think of it as replace). The method has two parameters the first the index of the card to replace, second the new Card (in that order please).
- getHandType returns a reference to a WinHand enumeration representing the type of hand this is. To do this it has to call several helper methods outlined below. It is a public method.
- The following is a set of methods for detecting each type of win. For the methods that return an int representing a pair three or four of a kind, return 0 when no such situation occurs.
- getPair - returns an int representing the rank of the cards comprising the pair in this hand. If there are two pairs return the highest one.
- getThreeKind - returns an int representing the rank of the cards comprising the three of a kind in this hand.
- getFourKind - returns an int representing the rank of the cards comprising the four of a kind in this hand.
- getHighestCard - returns the highest Card in this hand. (public method)
- royalFlush A helper method that returns true if the hand contains a royal flush
- straightFlush - helper method that returns true if the hand contains a straight flush.
- straight - helper method that returns true if the hand contains a straight.
- flush A helper method that returns true if all cards are of the same suit.
- fourOfAKind - helper method that returns true if the hand contains a four of a kind.
- threeOfAKind - helper method that returns true if the hand contains a three of a kind.
- pair - helper method that returns true if the hand contains a pair.
- twoPair - helper method that returns true if the hand contains two pairs.
- fullHouse - helper method that returns true if the hand contains a full house.
- toString - returns a string indicating each card as well as a number the user can press to indicate they wish to discard this card. see sample run for an example.
- sort - sorts the cards in order. This will make your life easier!. Use the following code: Note this only works in Java 8:
Arrays.sort(cards, (Card u1, Card u2) -> u1.compareTo(u2));
You will also need to import java.util.Arrays;
- Game - models one play of five-card draw. You should determine which methods need to be public and which private! You may add other fields and methods if needed.
- Fields: a deck of cards, a player, and a computer Hand. Constants needed to avoid magic numbers, and other fields you deem necessary!
- No parameter constructor - initialized all the fields.
- shuffle - the Fisher-Yates Shuffle.
- displayHand - prints out a players (computer or player) hand of cards in a nicely formatted fashion.
- deal - a method that deals two hands of cards.
- dealOne - deals one card to a specific location in a hand of a player (computer or player). Any card that was in the location before this new card is dealt is no longer in the hand.
- go - the main game logic goes here!
- printWinner - prints our who won the game and what hand they had
- main - should create a Game object and call the go method.
- There is no betting for this game.
- To simplify things, the computer never asks to discard. Optionally, to model this add computer logic using the following rules:
- Computer keeps a straight, flush, full house, and four of a kind.
- nothing keeps only Ace, King, queen, and jack.
- pair, two pair replace all non-pair cards
- 3 of a kind replace the other two cards.
In the event of a tie, the player with the highest card in the entire hand wins. The tiebreaker is the highest card in the pair, three of a kind/ four, of a kind, flush, etc. In the case of both players having an equivalent pair, the suits are used to break the tie.
public class Tester { private Hand one; private Hand two;
public Tester() { one = new Hand(); two = new Hand(5); }
public void makeSF(Hand hand) { hand.setCard(4, new Card(Face.KING, Suit.DIAMONDS)); hand.setCard(3, new Card(Face.QUEEN, Suit.DIAMONDS)); hand.setCard(2, new Card(Face.JACK, Suit.DIAMONDS)); hand.setCard(1, new Card(Face.TEN, Suit.DIAMONDS)); hand.setCard(0, new Card(Face.NINE, Suit.DIAMONDS)); }
public void make4K(Hand hand) { hand.setCard(0, new Card(Face.FIVE, Suit.DIAMONDS)); hand.setCard(1, new Card(Face.FIVE, Suit.SPADES)); hand.setCard(2, new Card(Face.FIVE, Suit.CLUBS)); hand.setCard(3, new Card(Face.FIVE, Suit.HEARTS)); hand.setCard(4, new Card(Face.THREE, Suit.DIAMONDS)); } public void make3K(Hand hand) { hand.setCard(0, new Card(Face.ACE, Suit.SPADES)); hand.setCard(1, new Card(Face.EIGHT, Suit.CLUBS)); hand.setCard(2, new Card(Face.EIGHT, Suit.HEARTS)); hand.setCard(3, new Card(Face.EIGHT, Suit.DIAMONDS)); hand.setCard(4, new Card(Face.KING, Suit.DIAMONDS)); } public void make3K2(Hand hand) { hand.setCard(0, new Card(Face.THREE, Suit.DIAMONDS)); hand.setCard(1, new Card(Face.THREE, Suit.SPADES)); hand.setCard(2, new Card(Face.THREE, Suit.CLUBS)); hand.setCard(3, new Card(Face.FOUR, Suit.HEARTS)); hand.setCard(4, new Card(Face.QUEEN, Suit.DIAMONDS)); } public void makeFH(Hand hand) { hand.setCard(0, new Card(Face.THREE, Suit.DIAMONDS)); hand.setCard(1, new Card(Face.THREE, Suit.SPADES)); hand.setCard(2, new Card(Face.THREE, Suit.CLUBS)); hand.setCard(3, new Card(Face.QUEEN, Suit.HEARTS)); hand.setCard(4, new Card(Face.QUEEN, Suit.DIAMONDS)); } public void makeS(Hand hand) { hand.setCard(0, new Card(Face.THREE, Suit.DIAMONDS)); hand.setCard(1, new Card(Face.FOUR, Suit.SPADES)); hand.setCard(2, new Card(Face.FIVE, Suit.CLUBS)); hand.setCard(3, new Card(Face.SIX, Suit.HEARTS)); hand.setCard(4, new Card(Face.SEVEN, Suit.DIAMONDS)); } public void makeS2(Hand hand) { hand.setCard(0, new Card(Face.FIVE, Suit.DIAMONDS)); hand.setCard(1, new Card(Face.SIX, Suit.SPADES)); hand.setCard(2, new Card(Face.SEVEN, Suit.CLUBS)); hand.setCard(3, new Card(Face.EIGHT, Suit.HEARTS)); hand.setCard(4, new Card(Face.NINE, Suit.DIAMONDS)); }
public void make2P(Hand hand) { hand.setCard(0, new Card(Face.ACE, Suit.DIAMONDS)); hand.setCard(1, new Card(Face.FOUR, Suit.SPADES)); hand.setCard(2, new Card(Face.FOUR, Suit.CLUBS)); hand.setCard(3, new Card(Face.TEN, Suit.HEARTS)); hand.setCard(4, new Card(Face.TEN, Suit.DIAMONDS)); } public void makeP(Hand hand) { hand.setCard(0, new Card(Face.ACE, Suit.DIAMONDS)); hand.setCard(1, new Card(Face.THREE, Suit.SPADES)); hand.setCard(2, new Card(Face.FOUR, Suit.CLUBS)); hand.setCard(3, new Card(Face.TEN, Suit.CLUBS)); hand.setCard(4, new Card(Face.TEN, Suit.SPADES)); } public void makeH(Hand hand) { hand.setCard(0, new Card(Face.ACE, Suit.DIAMONDS)); hand.setCard(1, new Card(Face.THREE, Suit.SPADES)); hand.setCard(2, new Card(Face.FOUR, Suit.CLUBS)); hand.setCard(3, new Card(Face.SEVEN, Suit.CLUBS)); hand.setCard(4, new Card(Face.TEN, Suit.SPADES)); }
public void go() {
// Create a new array that holds 8 hands // Loop through the array and fill it with a new hand (no parameters)
// Call makeSF with a hand index of 0 // Call make4K with a hand index of 1 // Call make3K with a hand index of 2 // Call makeFH with a hand index of 3 // Call makeS with a hand index of 4 // Call make2P with a hand index of 5 // Call makeP with a hand index of 6 // Call makeH with a hand index of 7
// Loop through the hand array and call the sort method for the hand // at each index // Finish the following statements so that the appropriate hand type is // printed for each index of hand System.out.println("SF: "); System.out.println("4K: "); System.out.println("3K: "); System.out.println("FH: "); System.out.println("S: "); System.out.println("2P: "); System.out.println("P: "); System.out.println("H: ");
}
public static void main(String[] args) { Tester testerObject = new Tester(); testerObject.go(); } }
*Face & Suit are type int
import java.util.Arrays; public class Card { private Face name; private Suit suit; public class Hand { //Fields public Card(Face name, Suit suit) { this.name = name; this.suit - suit; } public Face getName() { return name; } * This method will sort a hand into ascending order. * Will only work in Java 8 or higher. * Makes the assumption that you have an array of N Cards named 'cards'; cards * is a field within this class. */ public void sort() { Arrays.sort(cards, (Card ul, Card u2) -> ul.compareTo(u2)); } public Suit getSuit() { return suit; } } public int rank() { return name.getRank(); } public int value() { return name.value(); } public String toString() { return String.format("%5s of %s", name.toString(), suit.toString(); } * Compares the ranks of two cards. * @return a value less than if the rank of 'this' card is less than the the ranks of the cards are equal and a value greater than o if the rank greater than the rank of the one passed in. * Note: This method works ONLY if another Card object is passed in as a pa */ public int compareTo (Object other) { return this.name.getRank() - ((Card) other).name.getRank(); } public static void main(String args[]) { Card one = new Card(Face. QUEEN, Suit.HEARTS); Card two = new Card(Face.JACK, Suit.DIAMONDS); if (one.compareTo(two) 0) { System.out.println(one + " \tis greater than " + two); } else { System.out.println(one + " and " + two + " ARE EQUAL");; } } import java.util.Arrays; public class Card { private Face name; private Suit suit; public class Hand { //Fields public Card(Face name, Suit suit) { this.name = name; this.suit - suit; } public Face getName() { return name; } * This method will sort a hand into ascending order. * Will only work in Java 8 or higher. * Makes the assumption that you have an array of N Cards named 'cards'; cards * is a field within this class. */ public void sort() { Arrays.sort(cards, (Card ul, Card u2) -> ul.compareTo(u2)); } public Suit getSuit() { return suit; } } public int rank() { return name.getRank(); } public int value() { return name.value(); } public String toString() { return String.format("%5s of %s", name.toString(), suit.toString(); } * Compares the ranks of two cards. * @return a value less than if the rank of 'this' card is less than the the ranks of the cards are equal and a value greater than o if the rank greater than the rank of the one passed in. * Note: This method works ONLY if another Card object is passed in as a pa */ public int compareTo (Object other) { return this.name.getRank() - ((Card) other).name.getRank(); } public static void main(String args[]) { Card one = new Card(Face. QUEEN, Suit.HEARTS); Card two = new Card(Face.JACK, Suit.DIAMONDS); if (one.compareTo(two) 0) { System.out.println(one + " \tis greater than " + two); } else { System.out.println(one + " and " + two + " ARE EQUAL");; } }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