Programming Assignment 1 – Five Card Draw Dr. William Kreahling February 19, 2020 Overview For this assignment you are writing a program that plays a hand of five card draw between one computer player...

Programming Assignment 1 – Five Card Draw

Dr. William Kreahling


February 19, 2020


Overview


For this assignment you are writing a program that plays a hand of five card draw between one computer player and the computer. Your program should use the following rules: • When the hands are first dealt you see your cards, but not the computers. After the discard, you should see both hands. • After the discards, display both hands, print out who won the game, what hand they had and exit. • It is imperative to note I have loosened up the rules to make the game easier to code. Make note of the places where my rules are different that the traditional rules of five card draw. In several cases you may follow the traditional rules for bonus points. These cases are clearly stated.


Information


You will need the following enumerations:


1. Suit - model the suit of a card. Must work with the supplied Card class.


2. 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). The values of the cards are their numerical face value with ace being 1, and king, queen, and jack all being worth 10.


3. WinHand - models the winning hands of poker as well as an invalid state. • Straight flush • Four of a kind • Full house • Flush • Straight • Three of a kind • Two pair • Pair • HighCard (in the absence of all other hands, highcard wins) You will need the following Classes: You need to have a good reason why classes are public, private, protected, or package access.


1. Card - models a playing card. (This class is supplied).


2. Hand - models a hand of five card draw. • Fields needed to represent a single hand of cards. You must use an array. All fields must be private.


1


CS 151 Spring 2020


• Constructor that accepts the number of cards in a hand and creates a hand capable of holding that many Cards. • A default constructor that creates a hand capable of holding 5 cards. This constructor must call the other constructor to accomplish its goal. • setCard - sets a card in the hand (replaces a single Card in the hand with another). • getPair - returns an int representing the rank of the cards comprising the pair in this hand. • 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) • getHandType returns a reference to a WinHand enumeration representing the type of hand this is. (public method) • 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. • 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. • flush - helper method that returns true if the hand contains a flush. • toString - returns a string indicating each cards 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;


3. 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! • Default constructor - initialized all the fields. • shuffle - the Fisher-Yates Shuffle. • displayDeck - prints out a deck of cards in a nicely formatted fashion. • deal - method that deals two hands of cards. • dealOne - deals one cards to a specific location in a hand. 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.


2


CS 151 Spring 2020


• There is no betting for this game. • To simplify things, the computer never asks to discard. If you want to add computer logic, for up to 5 bonus points use the following rules:


– Computer keeps a straight, flush, full house, and four of a kind.


– nothing – keeps only King, queen, and jack.


– pair,two pair – replace all non-pair cards


– 3 of a kind – replace non pair cards.


– You can make the computer smarter if you know who to play five card draw better than the above stated rules. • If the event of a tie, the player with the highest card in the entire hand wins. For up to five bonus points, the tie breaker is the highest card in the pair, three of a kind/ four, of a kind, flush, etc. In the case of both players having a pair of two’s the suits are used to break the tie.


Sample run:


> java Game


Player : 0: Ace of Clubs 1: Three of Diamonds 2: Jack of Hearts 3: Queen of Diamonds 4: King of Hearts


How many cards to you want to discard >2 Which 2 would you like to swap >0 1


Computer : 0: Three of Clubs 1: Five of Clubs 2: Nine of Clubs 3: Ten of Spades 4: Queen of Spades


Player : 0: Ace of Spades 1: Nine of Spades 2: Jack of Hearts 3: Queen of Diamonds 4: King of Hearts


Player wins with a High card


> java Game


Player : 0: Four of Clubs


3


CS 151 Spring 2020


1: Five of Diamonds 2: Five of Clubs 3: Eight of Clubs 4: Jack of Diamonds


How many cards to you want to discard >3 Which 3 would you like to swap >0 3 4


Computer : 0: Ace of Diamonds 1: Deuce of Clubs 2: Six of Clubs 3: Seven of Diamonds 4: King of Spades


Player : 0: Ace of Hearts 1: Five of Diamonds 2: Five of Clubs 3: Six of Diamonds 4: Eight of Diamonds


Player wins with a Pair


> java Game


Player : 0: Deuce of Hearts 1: Four of Diamonds 2: Eight of Clubs 3: Nine of Spades 4: Jack of Spades


How many cards to you want to discard >2 Which 2 would you like to swap >0 1


Computer : 0: Four of Clubs 1: Five of Diamonds 2: Five of Clubs 3: Seven of Hearts 4: Nine of Clubs


Player : 0: Four of Hearts 1: Four of Spades 2: Eight of Clubs 3: Nine of Spades 4: Jack of Spades


Computer wins with a Pair


4


CS 151 Spring 2020


> java Game


Player : 0: Ace of Clubs 1: Four of Spades 2: Six of Diamonds 3: Eight of Spades 4: Jack of Hearts


How many cards to you want to discard >4 Which 4 would you like to swap >0 1 2 3


Computer : 0: Five of Hearts 1: Seven of Hearts 2: Nine of Hearts 3: Jack of Spades 4: King of Hearts


Player : 0: Three of Diamonds 1: Three of Hearts 2: Ten of Clubs 3: Ten of Hearts 4: Jack of Hearts


Player wins with a Two pair


> java Game


Player : 0: Four of Diamonds 1: Nine of Spades 2: Ten of Clubs 3: Jack of Spades 4: Queen of Spades


How many cards to you want to discard >1 Which 1 would you like to swap >0


Computer : 0: Deuce of Spades 1: Three of Diamonds 2: Three of Clubs 3: Seven of Spades 4: Queen of Clubs


Player : 0: Nine of Spades 1: Ten of Clubs 2: Jack of Spades 3: Queen of Spades


5


CS 151 Spring 2020


4: King of Diamonds


Player wins with a Flush


Submit:


Due Monday February 24th at 11:59 PM


Use handin to submit. The command for handing is:


handin.151.2 Thus for this assignment it would be: handin.151.2 1 *.java *.txt or even better:


tar -cvjf prog1.tbz progam1 handin.151.2 1 prog1.tbz


Where program1 is the name of the directory where your files are stored.


6


May 18, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here