Mike's Blackjack ProgramOverviewI wrote (most of) this program for my Introduction to Programming Class (COMP14). It is a simple emulation of the standard Blackjack game. The general objective is to get as close to 21 points as you can without going over, and you can't be sure of which card you will draw next. All numeric cards are worth their face value; Jacks, Queens and Kings are worth 10 points each; Aces can be worth either 1 or 11 points -- whichever benefits the score more. The first applet I compiled is a single-player iteration of the Blackjack scoring rules. You can access it here. The second applet is the full Blackjack game, played against a dealer. Card classThe "card" class in this program defines a card as a Java-based object. The class includes member variables "suit" and "face," the two elements used to describe an individual card. The class has methods to return both the suit and the face, and can return a text representation of a card for debugging purposes. Deck classThe "deck" class creates a virtual "deck" by systematically creating an array of cards. All 13 faces are created once for each of the 4 suits. The deck class also includes a method that essentially shuffles the deck by swapping random members of the array. There are also methods to deal the top card and output a text representation of the deck. Hand classThe "hand" class creates a hand, for either the player or the deck, by assembling piece-by-piece an array composed of card objects obtained from the top of the deck array. The class contains methods to construct the hand, add cards, return a text representation, and reset the hand. CompilationThe "deck," "hand," and "card" classes work together to work as objects governed by the rules of a card game. In this Blackjack game, the class "BlackjackGame" utilizes them by creating a deck and a hands for both the player and dealer. Both the deck and the two hands work with card objects as defined by the "card" class. The deck is composed of card objects; the hands are composed of similar card objects obtained from the "top of the deck" (a changing array index). The outcome of the game, as far as the winner, is the result of a "decideWinner" method comparing the point totals of both hands (after a calcPoints method has determined the totals and adjusted them for aces). If a player busts, the other player wins -- otherwise, the player with the higher score wins. CommentaryI had a pretty good time putting this program together, once I figured out what I was trying to represent. Eventually it hit me that the Card, Deck, and Hand classes were not unique to Blackjack at all -- they could be used for any card game -- and they needed to function independently of the rest of the game. Separating the classes in my mind was a big step and it helped me finish the last part of the program with much less conceptual trouble than I experienced starting off. -- Mike Turner, April 21st, 2005 |