INST2002 Programming 2: Support Sheet for Online Assessment 1
Set by: Luke Dickens
Available from 21st October, 2015 Due before midnight on 31st October, 2015
1 A Simple Blackjack Game
Blackjack (also called twenty-one) is possibly the most widely played card game in the world. It can be played as a gambling game in places like casinos, or you can play it just for fun. For this assessment, you will be completing an implementation of a simplified version of the blackjack game.
1.1 Ob jective
In our version of blackjack, a single player plays against a dealer (who will be controlled by the programme). The game is played with a single deck of 52 cards. The player’s objective is to beat the dealer in one of the following ways:
- For the player to have a final score that is higher than the dealer’s final score without exceeding 21 points; or
- For the player to have a final score below 21, but for the dealer to exceed 21. The player will lose under the following conditions:
- When the dealer’s final score is below 21 point, but is equal to or higher than the player’s final score; or
- When the player’s final score exceeds 21 points, but the dealer’s does not. The game is considered a draw if both the dealer and the player exceed 21 points.
1.2 Game Overview
To begin, the player is first dealt a single card, then the dealer is dealt a single card, then the player is dealt a second card; all three cards are visible to the player. After this, the player plays their hand first, then the dealer plays their hand, and finally the outcome is determined.
1
1.3 Evaluating the Point Value of a Hand
A hand is the collection of cards belonging to either the player or the dealer. Both the player and the dealer evaluate their hands in the same way. A hand’s point value, is a sum of the point-values of the cards it contains. Picture cards (kings, queens, and jacks) are counted as ten points. An ace can count as 1 point or 11 points, whichever is better, but counts as 11 points by default. All other cards are counted as the numeric value shown on the card. To evaluate a hand:
- Start by counting all aces as hard (meaning they are worth 11 points), then adds all the cards’ points together. If this comes to less than 21, then this is the point-value of the hand.
- If at this point, the hand is worth more than 21 points, but there are hard aces in the hand, then one of the aces can be made soft (meaning it is only worth 1 point). The card values are again added together, and if the result is now below 21, then this is the point-value of the hand.
- If the hand still has more than 21 points, but still has hard aces, then these can be made soft, one at a time, until either the hand has a point-value of less than 21, or until there are no more hard aces.
- If after all available aces have been made soft, the point-value is still more than 21, then the hand is bust.
1.4 Playing the Player’s Hand
After receiving their first two cards, the player evaluates the point-value of their hand. The player can then choose to hit or stick. If the player chooses to stick, this means they have finished and it is the dealer’s turn.
Each time the player chooses to hit, they are dealt a new card, which is added to their hand, and they re-evaluate the hand’s point-value. If the hand is not bust, then the player again has the choice between hitting or sticking. The player can choose to hit any number of times, so long as the hand is not bust.
1.5 Playing the Dealer’s Hand
The dealer starts with a single card, and has no choice about how the play progresses. The dealer must hit repeatedly until the hand’s point-value is greater than or equal to 17, or the hand is bust. Therefore, if the hand’s point-value is 17 or more, then the dealer must stick. At this point, the dealer’s play is stopped, and the final outcome determined.
2 Your Task
Your job is to complete a blackjack programme on TestDome. This will involve making changes to the programme, and then clicking the Run button. If the programme fails to compile, then you will get a compiler error, that will help you to locate the problem. If the programme compiles, then TestDome will run a series of tests, that will tell you what
2
parts of the programme now work. You should try to make all the tests pass. If you pass all the tests, you will get a minimum of 90%. The remaining 10% is for good coding style and comments.
3 The Programme
In a text box on your TestDome test, you will be given the programme that you must edit. This contains four classes. The first class Problem is public, and contains the main method and some supporting methods. The next three classes are package visible1, and are called Card, Hand and Deck. You should not edit the Problem class, but you will need to edit some methods in the other three classes, and add one or more fields to the Card and Hand classes. A link to your test will be emailed to you. Before you attempt the test, you should practice on the code provided on moodle (see Section 4).
3.1 The Card class
The card class is a simple immutable class. Each instance, represents a card, and has a face value and a suit. Face values are between 1 and 13, and some face values are presented as letters; these are: A (or ace) for 1, J (or jack) for 11, Q (or queen) for 12 and K (or king) for 13. See the faceValueToString method if you are unsure about this.
The suit can take one of four values. The integer representation for suits are specified by static fields in the Card class. Their string representations (for output) are: ♥ (for hearts), ♣ (for clubs), ♦ (for diamonds), or ♠ (for spades). See the suitToString method in class Card for more information.
You will need to implement the following methods:
- A constructor, Card(int faceValue, int suit), which takes an integer represen- tation of the face-value, and an integer representation of the suit as arguments, and should store this information somewhere. You may have to define some fields for your class to do this.
- The getter method getFaceValue(), which should get the integer representation of the face-value, that will be stored by the constructor.
- The getter method getSuit(), which should get the integer representation of the suit, that will be stored by the constructor.
- The getPointValue method, that takes no arguments, and returns the point-value of the card. By default (i.e. when first created), aces should be treated as hard aces, and should return a point-value of 11. This is described in more detail in Section 1.3.
- The isAce() method that returns a boolean value. This should be true if the card is an ace, and false
1The fact that these classes are package visible should not affect you in any way. However, if you are unsure of the meaning, but interested, then an example programme with package visible classes is given in question 4 on tutorial sheet 2.
3
- The getAllSuits() method. This is a static method. It takes no arguments, and should return an array of four ints. One int for each integer value that the suit can take.
3.2 The Hand class
The Hand class represents a collection of Cards belonging to the player or the dealer. You
should implement the following methods in Hand.
- A constructor, which takes no arguments and constructs an empty Hand, i.e. a Hand
with 0 Cards in it. You will need to add one or more fields to the Hand class for this.
- An addCard method, which takes a Card as input, and adds it to the hand.
- A getCardsInHand, which returns an array of Cards containing all of the cards in the Hand.
- A getHandValue, which returns in integer representing the total point-value of a hand, as described in Section 1.3.
- An isBust method, which returns true if the hand is bust, and false otherwise, see Section 1.3.
- A dealerSticks method, which returns true if the dealer must stick on this hand, and false otherwise, see Section 1.5.
3.3 The Deck class
The Deck class is a collection of 52 Cards, that can be shuffled to put them in random order. The Deck should also be able to deal a Card from the top of the Deck into a Hand. Most of the methods in Deck have already been implemented for you.
You have just one method to implement in the Deck class, the dealToHand method. This takes a Hand object as input, and deals a single Card from the top of the deck into this Hand object. You will need to use the methods: getTopOfDeck from Deck, and addCard in Hand. You will also need to change the topOfDeck field to point to the next Card. You can assume that there will never be more than 52 cards dealt between shuffles.
4 A Local Copy of the Code
If you go to moodle, you can access a local copy of the code. This is the same code that will appear in the text box when you finally log in to TestDome. You can experiment with this local copy to test your ideas, before you log in to TestDome. Try compiling this local code to start with, then running it to see what happens. At this point, try to make changes in line with the description above, and recompile regularly to test your changes. When you have finished editing the code, you should be able to play the simplified blackjack game described in Section 1 by compiling and running the Problem programme.
4