Blackjack is a popular card game that is simple to play. The object of the game is to beat the dealer in one of the following ways:
The player and the dealer receive two cards at the start and one at a time afterwards. The player decides whether to receive another card based on the value of his hand. Each face card has a value of 10, an ace may be counted as 1 or 11, and the other cards have values equal to their number. The player or dealer whose hand is closest to 21 wins the game. If a player goes over 21, he/she automatically loses (called a "bust"). If the player chooses to stand, the player is finished playing and waits for the dealer to play until standing or busting. If the player and dealer have equal points, then the game is called a push, and no one wins.
For example, consider the following hands: see image.
Player's hand has a value of 3 + 2 + 1 + 10 = 16. Note that the ace cannot be counted as 11 or the value would exceed 21. Dealers hand has the value 10 + 5 + 5 = 20. Dealer wins the game.
For this project, you must simulate a casino-style two-player game of Blackjack, pitting a player against a dealer. The player will draw cards first. If the player busts, the dealer automatically wins the game without receiving the cards. Otherwise, the dealer draws cards and the points of the player and dealer are compared.
Below is an initial view of the GUI displaying the game board: see image.
Initially the two Buttons "Hit" and Stand are disabled.
You can enable/disable Button object using the setDisable(Boolean) method.
The "Blackjack" table shows cards on both players' hands during gameplay.
When the player clicks "Start Game" Button, start a new game with the following initializations:
The GUI will look like this (the first card may be different each time obviously): see image.
When the player clicks the "Hit" button:
If the hit did not bust the player, it will look something like this: see image.
Clicking Hit again: see image.
If the player has not busted and clicks "Stand", the dealer begins playing the game.
To end the game, the victor or a tie must be decided. The game result can be determined based on the following:
After determining the winner, display the result and update the player or dealer score accordingly. Return the game buttons to their initial state Here are screenshots of each end game result:
Player Bust: see image.
Dealer Bust: see image.
Player Wins, no Bust: see image.
Dealer Wins, no Bust: see image.
Push (Tie): see image.
The following class relationship diagram shows the required classes and how they interact. see image.
The BlackjackTableGUI class creates and manages the GUI and game using event-driven programming. The Card class represents a single playing card as defined by its face (All cards are spades for simplicity). It extends the ImageView class from JavaFX to store the card and its image in one convenient class. The Deck class represents a common deck of 52 playing cards (4 copies of each face Ace-2-...-Queen-King). Player is a class that encapsulates the common attributes of a player such as the player and dealer. Player and dealer are dealt cards from the same deck stored statically in Player class.
All methods must be public visibility. Use responsible access modifiers for all other fields.
class Card | |
CLASS DATA FIELDS | |
static String[] FACES | Holds possible values of cards: "A", "2", "3", ..., "10", "J", "K", "Q" |
static int HEIGHT | Height of image = 130 |
INSTANCE DATA FIELDS | |
String face | Holds the face of the card |
CONSTRUCTOR | |
Card (String face) | Creates an instance of Card with the given face Sets the image for the ImageView Sets fit height to HEIGHT Sets preserve ratio to true |
METHODS | |
String getFace() | Getter |
int valueOf() | Returns the point value of the card. 2-9 are worth 2-9, Jack-Queen-King are worth 10, Ace is worth 11 (handle Aces as possible value of 1 in Player class) |
class Deck | |
INSTANCE DATA FIELDS | |
ArrayList < Card > cards | Stores the deck of 52 Cards. Properly define other instance data fields. |
CONSTRUCTOR | |
Deck() | Initializes the deck of Cards (using reset method) |
METHODS | |
void reset() | Clears all existing cards from the deck and remakes the deck with 52 cards (4 of each face) |
Card dealCard() | Removes and returns a random Card from the deck. |
class Player | |
INSTANCE DATA FIELDS | |
ArrayList< Card > hand | Holds a maximum of 12 cards. |
static Deck | Holds and deals Cards for all Players. |
CONSTRUCTOR | |
Player() | Instantiate the player's hand |
METHODS | |
Properly defines constructors, accessor/modifiers, and other methods. | |
getHand() | Getter |
int valueOfHand() | Returns the point value of the hand. If the value exceeds 21 and there is one or more Aces in the hand use them to achieve the best hand value without exceeding 21. |
void clearHand() | Remove all Cards from the hand |
boolean stand(int otherPlayerValue) | Determine whether to stand(for non-user players). Always stand when the hand value is higher than the other player. If not, then when the hand value is 16 or higher, stand 50% of the time. When the hand value is 19 or higher, always stand. Otherwise do not stand. |
void hit() | Add a card to the hand from the Deck |
boolean bust() | If the hand value is greater than 21, it is a bust. |
class BlackJackTableGUI | |
You must implement the Blackjack GUI using your own interpretation of the screenshots provided. Make your GUI look as similar as possible. Below are several styling numbers that I used to organize the UI: - Space between the Player/Dealer title and Hand Value Labels: 100 - Preferred size of the Player/Dealer Card area: 600 width, 150 height - Preferred width of the Player/Dealer Score Label: 150 - Space between the Player/Dealer Score and the game result: 280 - Space between the Buttons: 10 - Space between each section (Player display, Dealer display, Buttons, Game Stats): 10 - Background color: forestgreen - Label text style: 14 font size, white color, bold The following methods are also required (in addition to the standard GUI and event handling methods): | |
void startGame() | - Reset the game Deck - Clear the player and dealer hands and hand values - Give the dealer one Card - Disable Start button, enable Hit and Stand Buttons - Clear previous game result |
void updateHand(Player p, HBox box, Label handValue) | - For a given Player, add the Card to the Player's hand display - Update the hand value Label. |
void endGame() | - Determine the winner of the game, if there is one. - Display game result and update player or dealer score appropriately. - Enable the Start Button and disable the Hit and Stand Buttons. |