1 / 24

COMP 14 Introduction to Programming

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

Download Presentation

COMP 14 Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 14Introduction to Programming Mr. Joshua Stough March 30, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

  2. 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

  3. 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

  4. 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()

  5. 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

  6. 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

  7. Array of Objects Card card; card = deck[2]; deck[0] deck[1] deck[2] card

  8. 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

  9. 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();

  10. 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()

  11. 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

  12. 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]

  13. 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)

  14. 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

  15. 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)

  16. 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

  17. Programming Tips • Start early! • Start small! • get Deck class working first • get Hand class working next • Test classes without the whole program

  18. 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.

  19. 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.

  20. 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.

  21. Recitation This Week • No laptops needed • Ask TA questions about object-oriented design, methods, arrays, Program 5, etc.

  22. 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

  23. Next Time in COMP 14 • Searching and Sorting • Read: Ch 10 (pgs. 523-542)

  24. In-Class Programming • DiceGame handouts • URL at bottom of handout if you want to cut-n-paste code outlines

More Related