Monte-Carlo Simulation: Blackjack
The main purpose of this lab is to give you practice building classes in Java. To do this, we'll simulate a simple version of the card game Blackjack. The objects in a card game include: a Card, a Deck, a Hand, and Game. You'll make a class for each one, connecting them together as appropriate. We'll also start to make use of a few of the many Java library classes.
Documentation for Java 1.5 is located at: Java 1.5 SE API.
Documentation for Java 1.6 is located at: Java 1.6 SE API
If using a terminal is new to you, or you need a refresher, please check out the Terminal tutorial.
Before you start writing code, please take a look at the coding style guide.
You probably want to keep your code on your network directory. If you are on a terminal on a mac, you can get to the working directory using
cd /Volumes/Personal/<your user folder name here>
For this assignment you will implement classes to represent a card, a hand, a deck, and the game Blackjack. When you write a class, always include a main function for the class that tests out its capabilities. For example, the card class should try creating a card object, assigning a value to it and then accessing and printing out the value. Ideally, a test function should try out all of the class methods and access all of the class variables.
BasicCard - create a java class called BasicCard, which should hold
all information unique to the card. For this assignment, it needs only
to hold the value of the card, which must be in the range 1-10. The
BasicCard class should have the following methods. In addition, make a
main method that uses each of the class methods.
- public BasicCard() - a constructor with no arguments.
- public BasicCard(int v) - a constructor with the value of the card.
- public void set(int v) - set the value of the card to the argument, possibly doing range checking.
- public int value() - return the numeric value of the card.
BasicHand - create a java class called BasicHand, which should hold a
set of cards. You can use an ArrayList (import java.util.ArrayList)
to hold the BasicCard objects. The class should have at least the
- public BasicHand() - initialize the ArrayList.
- public void add( BasicCard card ) - add the card object to the hand.
- public int size() - returns the number of cards in the hand.
- public BasicCard get( int i ) - returns the card with index i.
- public int value() - returns the sum of the values of the cards in the hand.
BasicDeck - create a java class called BasicDeck, which should hold a
set of cards and be able to shuffle and deal the cards. You should
use an ArrayList to hold the cards. The class should support the
- public BasicDeck() - builds a deck of 52 cards, 4 each of cards with values 1-9 and 16 cards with the value 10. Note, you probably want the constructor to call the build method.
- public void build() - builds a deck of 52 cards, 4 each of cards with values 1-9 and 16 cards with the value 10.
- public BasicCard deal() - returns the top card (position zero) and removes it from the deck.
- public BasicCard pick( int i) - returns the card as position i and removes it from the deck.
- public void shuffle() - shuffles the deck. This method should put the deck in random order. One way to do it is to build a fresh second deck and then make 52 random picks from it. Note that the first pick should be in the range [0, 51], the second pick should be in the range [0, 50] and so on.
- public void print() - prints out the deck in some reasonable way so that you can see the ordering of the card values.
Blackjack - create a class called Blackjack that implements a simple
version of the card game. The class will need to have a BasicDeck, a
BasicHand for the player, a BasicHand for the dealer, and scores for
both the dealer and player. The main function for the Blackjack class
should implement one complete game.
- public Blackjack() - should set up and reset the game (call reset).
- public reset() - should set up and reset the game.
- public void deal() - should deal out two cards to both players.
- public void print() - should print out the state of the game.
- public boolean playerTurn - have the player draw cards until the total value of the player's hand is equal to or above 16. The function should return True if the player goes over 21.
- public boolean dealerTurn - have the dealer draw cards until the total of the dealer's hand is equal to or above 17. The function should return True if the dealer goes over 21.
- static public void main( String args ) - the main function should deal, print the state, then have the player go, then have the dealer go. If the player goes bust, the game is over and the dealer wins. If the player does not go bust, but the dealer goes bust, then the game is over and the player wins. Otherwise, the player with the largest hand wins. In the case of a tie, the result is a push.
Once you have completed the above four classes, generate a printout of
three different games. You can send output to a file using the greater
than symbol on the command line. For example, the command
java -cp . Blackjack > mygames.txt
would play a game and send it to the file mygames.txt.
- Add another function to the Blackjack class that plays a game and returns 1 if the player wins, 0 if it is a push, and -1 if the dealer wins.
- Make one more simple class called Simulation. This class should have only a main function that executes 1000 games of Blackjack. It should keep track of how many games were player wins, dealer wins, and pushes. Print out the total in the end both as raw numbers and as percentages.
- Have the dealer take only one card to start, then make the game interactive so that you can decide whether the player takes another card or not.
- Add in more of the rules for Blackjack. For example, an Ace (value 1 in the default case above) can have the value 1 or 11, whichever is more advantageous. If you add in the Ace rule, then you will also want to take into account a real Blackjack (2 cards totalling 21) when evaluating the winning hand. A Blackjack beats a 21 with more than 2 cards.
- Run the simulation with different rules for the player and see how it affects the outcome percentages over many games (>= 1000).
Make your writeup for the project a wiki page in your personal space. If you have questions about making a wiki page, stop by my office or ask in lab.
Your writeup should have a simple format.
- A brief description of the overall task, in your own words.
- As explanation of your solution, focusing on the interesting bits. In this assignment, for example, shuffling the deck and doing the overall simulation are the interesting bits. You can include code snippets in your writeup to help describe your solution.
- Printouts, pictures, or results to show what you did. For this assignment, you should include the results of the overall simulation.
- Other results to demonstrate extensions you undertook. If you tried different player strategies, for example, show how those affected the overall simulation results.
- A brief conclusion and description of what you learned.
Once you have written up your assignment, give the page the label:
You can give any page a label when you're editing it using the label field at the bottom of the page.
Do not put code on your writeup page or anywhere it can be publicly accessed. To hand in code, attach it to an email and send it to the prof. Hopefully, you'll have a handin folder on the Academics file server.