Program 3 - Functions: The game of twenty-one.

For this assignment you will implement a simplified version of the game of 21 or BlackJack. This is a card game based on the standard deck of 52 cards. There are 13 cards (Ace through King) in each of 4 suits (clubs, diamonds, hearts, and spades). You play against a dealer. The object of the game is to get a hand that totals as close to 21 as possible without going over 21. Face cards (Jack, Queen, King) count as 10. The Ace can be either 1 or 11, and all other cards count their numeric value (2 through 10).

It is essential that you make good use of functional abstraction in order to get full credit on this assignment. The 10 points of "Assignment specific correctness criteria" for this assignment will be based upon how well you have broken the problem into smaller subproblems with proper use of parameter passing.

You start with an initial bank roll of $100. The game is then played as follows.

  1. The cards are shuffled before every hand.
  2. You place a bet.
  3. You are dealt 2 cards and the dealer is dealt 2 cards. You only get to see one of the dealers 2 cards. The cards are dealt one at a time, the first card going to the player, the second to the dealer, the third to the player and the fourth to the dealer.
  4. You then can draw additional cards, trying to get as close to 21 as possible without going over. If you go over, you lose.
  5. Once you stop drawing cards, provided you haven't gone over 21, the dealer then draws cards until his/her hand is 17 or more. If counting an ace as 11 gives the dealer a hand between 17 and 21 inclusive the dealer stops. If counting an ace as 11 would cause the dealer to be over 21, then count the ace as 1.
  6. If the dealer goes over 21 you win.
  7. If the dealer's hand is closer to 21 than yours you lose.
  8. If both hands have the same value, it is a tie and you don't win or lose.
  9. If you win, the amount of your bet is added to your bank roll.
  10. If you lose, the amount of your bet is subtracted from your bank roll.
  11. If you tie, your bank roll remains unchanged.
  12. The game repeats (starting with a new bet) until you either run out of money, or decide to stop. You may stop at the end of any hand.

I have provided a package "cards" that you must use to complete this assignment. The package cards has been placed in the same location as tio on the cats computers, so if you can use tio, then you should be able to use cards without any further action. To work with cards at home, you can install cards.jar just like you did tio.jar (be sure and right click the link in this sentence to save a copy of jar - left clicking won't work). Some of the classes in "cards" are similar to those in section 6.13 of the text. Everything you need related to the cards package for this assignment is shown in the following example. This example can also be found in /afs/cats/courses/cmps012a-cm/prog3/CardTest.java.

import cards.*;

class CardTest {
  // args[0] is the seed for the random number generator.
  // If there are no args, then a random seed is used.
  public static void main(String[] args) {

    // The deck is initialized with a random seed to randomize the cards.
    // You only create the deck ONCE.
      Deck deck;
      if(args.length == 0)
          deck = new Deck(); // just use a random seed for shuffling
      else
          deck = new Deck(Integer.parseInt(args[0])); // get seed from command line

    // be sure and shuffle the deck each new hand
    deck.shuffle();

    Hand hand = new Hand();
    // You add cards to the hand by drawing them from the deck.
    hand.add(deck.draw());
    System.out.println("The hand is " + hand);
    hand.add(deck.draw());
    System.out.println("The hand is " + hand);

    // For the dealer hands you just show one card.
    System.out.println("Showing only one card " + hand.showOne());

    // You can determine the value of the hand treating Ace as one.
    System.out.println("The hand total is " + hand.valueAce1()); 

    // You can determine the value of the hand treating Ace as 11.
    System.out.println("The hand total is " + hand.valueAce11()); 
  }
}

Sample Solution

You may try out my solution to see how it behaves. Your program should generate the exact same output. My solution is in /afs/cats/courses/cmps012a-cm/prog3/TwentyOne.class
java TwentyOne 123
will run it with the seed 123 (you will get the same sequence of card if you run it again).
java TwentyOne
will give a different card sequence every time.

Sample Execution

Here is a (rather long) sample run. It includes some ties, a 21 for the user, some wins and some loses. If your program can duplicate this sequence then you should be in good shape. In order for your program to replicate this you must shuffle before every hand, deal the first and third cards to the user, and the second and fourth cards to the dealer. The user then draws, and finally the dealer draws provided the player did not go over 21.
os-prompt>java TentyOne 9
Let's play twenty-one.
You have $100. How much do you want to bet? Bet 0 to quit.
10
You have: 2:clubs  4:diamonds  
The dealer is showing: Ace:diamonds 
Do you want another card?[y/n]
y
Your hand is now: 2:clubs  4:diamonds  Jack:diamonds  
Do you want another card?[y/n]
n
Dealer has: Ace:diamonds  8:diamonds  
Better luck next time.
You have $90. How much do you want to bet? Bet 0 to quit.
10
You have: 6:spades  2:hearts  
The dealer is showing: 6:hearts 
Do you want another card?[y/n]
y
Your hand is now: 6:spades  2:hearts  Ace:hearts  
Do you want another card?[y/n]
n
Dealer has: 6:hearts  Ace:spades  
You win.
You have $100. How much do you want to bet? Bet 0 to quit.
10
You have: Queen:hearts  8:spades  
The dealer is showing: 6:diamonds 
Do you want another card?[y/n]
n
Dealer has: 6:diamonds  7:spades  
Dealer takes another card.
Dealer now has: 6:diamonds  7:spades  Queen:diamonds  
You win.
You have $110. How much do you want to bet? Bet 0 to quit.
10
You have: 10:clubs  King:spades  
The dealer is showing: Queen:diamonds 
Do you want another card?[y/n]
n
Dealer has: Queen:diamonds  King:hearts  
That's a push.
You have $110. How much do you want to bet? Bet 0 to quit.
10
You have: 10:spades  Jack:hearts  
The dealer is showing: Ace:diamonds 
Do you want another card?[y/n]
n
Dealer has: Ace:diamonds  9:hearts  
That's a push.
You have $110. How much do you want to bet? Bet 0 to quit.
10
You have: King:clubs  Jack:hearts  
The dealer is showing: 7:spades 
Do you want another card?[y/n]
n
Dealer has: 7:spades  10:clubs  
You win.
You have $120. How much do you want to bet? Bet 0 to quit.
10
You have: 3:spades  4:clubs  
The dealer is showing: 4:spades 
Do you want another card?[y/n]
y
Your hand is now: 3:spades  4:clubs  Jack:diamonds  
Do you want another card?[y/n]
n
Dealer has: 4:spades  King:diamonds  
Dealer takes another card.
Dealer now has: 4:spades  King:diamonds  2:clubs  
Dealer takes another card.
Dealer now has: 4:spades  King:diamonds  2:clubs  3:clubs  
Better luck next time.
You have $110. How much do you want to bet? Bet 0 to quit.
10
You have: 10:spades  Ace:hearts  
The dealer is showing: King:diamonds 
Do you want another card?[y/n]
n
Dealer has: King:diamonds  10:hearts  
You win.
You have $120. How much do you want to bet? Bet 0 to quit.
10
You have: 3:diamonds  2:hearts  
The dealer is showing: 10:clubs 
Do you want another card?[y/n]
y
Your hand is now: 3:diamonds  2:hearts  Queen:spades  
Do you want another card?[y/n]
y
Your hand is now: 3:diamonds  2:hearts  Queen:spades  7:clubs  
Dealer has: 10:clubs  King:hearts  
Better luck next time.
You have $110. How much do you want to bet? Bet 0 to quit.
0
os-prompt>
Here is the sequence of input values used for the above run:
10
y
n
10
y
n
10
n
10
n
10
n
10
n
10
y
n
10
n
10
y
y
0
The above intput sequence is also in /afs/cats/courses/cmps012a-cm/prog3/test1in.

If you run your program (or my sample solution) redirecting standard input to read from test1in and save the output in test2out, you should get the exact same output as I have placed in /afs/cats/courses/cmps012a-cm/prog3/test1out.

os-prompt:java TwentyOne < test1in  > myoutput
os-prompt:diff myoutput test1out
The above sequence should show no differences. Of course such file redirection is only for testing, wouldn't actually play the game that way.
[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

This page maintained by Charlie McDowell. Email regarding this site.