240 likes | 259 Views
COMP 14 Introduction to Programming. Mr. Joshua Stough March 30, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218. Program 5. Blackjack Solitaire You will allow the user to play a GUI-based Blackjack game alone (with no other players or dealer) Due: Wednesday, April 7 at 11:59pm
E N D
COMP 14Introduction to Programming Mr. Joshua Stough March 30, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218
Program 5 • Blackjack Solitaire • You will allow the user to play a GUI-based Blackjack game alone (with no other players or dealer) • Due: Wednesday, April 7 at 11:59pm • 75 points • 10 points extra credit available
Classes • Complete Code (no changes) • Blackjack - main method • BlackjackUI - user interface • BlackjackApplet - user interface for applet • Card - same as Program 4 • Outlines • Deck • Hand • BlackjackGame • start with Program 4 and make additions
Deck Class • Variables • array of cards • index of top of the deck • Methods • constructor • int cardsLeft() • void shuffle() • void swap (int ind1, int ind2) • Card dealFromTop() • String toString()
Deck Methods • Deck() • instantiate and initialize the deck of cards (create a Card object for each card in the deck) • int cardsLeft() • returns the number of cards left in the deck (uses the top of the deck index) • void shuffle() • shuffle the deck • swap random pairs 1000 times • void swap (int ind1, int ind2) • swap deck[ind1] and deck[ind2] • Card dealFromTop() • return the top card from the deck • String toString() • return a String representation of cards left in the deck
Swapping • Exchanging two values • Requires three assignment statements: temp = first; first = second; second = temp; Note: temp must be the same type as first and second
Array of Objects Card card; card = deck[2]; deck[0] deck[1] deck[2] card
Returning Objects • We can return objects from methods just like passing objects to methods • we're returning the address of the object public Rectangle getElement(int position) { return (rectangleArray[position]); } Assume rectangleArray is an array of Rectangle objects
Cascaded Method Calls • If the result of a method call is an object, we can tack on another method without using a placeholder variable Rectangle temp = getElement(2); int width = temp.getWidth(); Equivalent statments int width = getElement(2).getWidth();
Hand Class • Variables • array of cards • number of cards in the hand • Methods • constructor • void addCard (Card card) • int getNumCards() • void resetHand() • Card getCard(int position) • String toString()
Hand Methods • Hand (int maxCards) • instantiate the array of maxCards Card objects • void addCard (Card card) • add the given card to the hand if there's room • int getNumCards() • return the current number of cards in the hand • void resetHand() • clear the hand (set the number of cards to 0) • Card getCard(int position) • return the card at the given position in the hand • String toString() • return a String representation of the entire hand
Hand Class The array of cards in the Hand class should not be new cards, but should just point to cards from the deck deck[0] deck[1] deck[2] hand[0]
Methods initGame setupNewHand playerCanHit getPlayerHand addPlayerCard calcPoints BlackjackGame ClassAdditions • Everything should be static • Variables • deck of cards (use the Deck class) • player's hand (use the Hand class)
BlackjackGame ClassMethods • initGame • instantiates deck of cards and player's hand • shuffles deck of cards • getPlayerHand • returns the player's hand • setupNewHand • reshuffle the deck (only if needed), reset the player's hand, deal two cards to the player (add cards to the player's hand) • addPlayerCard • add a card from the top of the deck to the player's hand
BlackjackGame ClassMethods • playerCanHit • the player can "hit" if the point total is less than Blackjack and there are less than 5 cards in the hand • calcPoints • overloaded method • new version calculates the points for an entire hand (including adjusting for Aces)
Extra Credit • 10 points available • Make an applet of the Blackjack Solitaire game • Instructions are provided • Put the web address (URL ending in .html) of your applet in the comments section on Blackboard when you turn in Program 5
Programming Tips • Start early! • Start small! • get Deck class working first • get Hand class working next • Test classes without the whole program
Write a Tester Class public class Tester { public static void main(String[] args) { Deck testDeck = new Deck(); System.out.println (testDeck); testDeck.shuffle(); System.out.println ("Shuffled"); System.out.println (testDeck); } } The only classes that have to be working for this program are Tester, Deck, and Card.
Write a Tester Class public class Tester { public static void main(String[] args) { Deck testDeck = new Deck(); Hand testHand = new Hand(5); testHand.addCard(testDeck.dealFromTop()); testHand.addCard(testDeck.dealFromTop()); System.out.println (testHand); System.out.println ("cards left (50): " + testDeck.cardsLeft()); System.out.println (testDeck); } } The only classes that have to be working for this program are Tester, Deck, Hand, and Card.
Program 5 Announcements • Blackjack Class Design Document • You can request a correct version of the Card class and BlackjackGame class from Program 4 by emailing the TA who grades your assignments. • Problems w/Program 4? Come during office hours or set up an appointment to talk to me or TA.
Recitation This Week • No laptops needed • Ask TA questions about object-oriented design, methods, arrays, Program 5, etc.
Dealing from the Deck deal from top topOfDeck 2 0 1 deal from top What card will be dealt next? 0 1 2 deal from top -- return the address of the card at the top of the deck
Next Time in COMP 14 • Searching and Sorting • Read: Ch 10 (pgs. 523-542)
In-Class Programming • DiceGame handouts • URL at bottom of handout if you want to cut-n-paste code outlines