COMP 14 Introduction to Programming - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

COMP 14 Introduction to Programming

Description:

... to Programming. Jingyu Yan. March 30, 2005. Program 5. Blackjack Solitaire ... Make an applet of the Blackjack Solitaire game. Instructions are provided ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 25
Provided by: michele9
Category:

less

Transcript and Presenter's Notes

Title: COMP 14 Introduction to Programming


1
COMP 14Introduction to Programming
  • Jingyu Yan
  • March 30, 2005

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 8 at 1159pm
  • 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 deckind1 and deckind2
  • 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 deck2
deck0
deck1

deck2
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 (rectangleArrayposition)
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
deck0
deck1

deck2
hand0
13
BlackjackGame ClassAdditions
  • Everything should be static
  • Variables
  • deck of cards (use the Deck class)
  • player's hand (use the Hand class)
  • Methods
  • initGame
  • setupNewHand
  • playerCanHit
  • getPlayerHand
  • addPlayerCard
  • calcPoints

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) testDe
ck.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.a
ddCard(testDeck.dealFromTop()) testHand.addCard(
testDeck.dealFromTop()) System.out.println
(testHand) System.out.println ("cards left
(50) " testDeck.cardsLeft()) System.out.pr
intln (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 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
Write a Comment
User Comments (0)
About PowerShow.com