Notes for part III, improving the design using

advertisement
AP Computer Science
 Thirteens
• Uses a 10-card board. Ace, 2, … , 10, jack, queen
corresponding to the point values of 1, 2, …, 10,
11, 12.
• Pairs of cards whose point values add up to 13
are selected and removed.
• Kings are selected and removed singly.
• Chances of winning are claimed to be about 1
out of 2.
 Tens
• Uses a 13-card board.
• Pairs of cards whose point values add to 10 are
selected and removed
• Quartets (groups of 4) of kings, queens, jacks,
and tens, all of the same rank (for example, J, J, J,
and J).
• Chances of winning are claimed to be about 1 in
8 games.
 All Boards
• need a deck
• deal from the deck in the same way
 Where should we declare them?
 Only the ElevensBoard needs
• containsJQK
• containsPairSum11
 But what about
• isLegal
• anotherPlayIsPossible
• All Boards need them but they don`t have the same
behaviour
 When
a method needs to be declared but
not defined we use the keyword abstract
 This also means the class itself needs to be
declared abstract
 Abstract Base Classes can never be initiated
• AbstractClass example = new AbstractClass();
 Instead
they can refer to a subclass which is
not abstract
• AbstractClass example = new SubClass();
• Sub classes can still be abstract
 Let`s
have a look at the abstract base
class Board in the skeleton project.
 Board myBoard = new ElevensBoard();
 Methods implemented in ElevensBoard
will
be called over those in Board
 If no method is implemented in
ElevensBoard then it will default to Board
 myBoard.isLegal(list)
• Calls the ElevensBoard class
 myBoard.deal(int)
• Calls the Board class
 Poly = many
 morph = shape
 Interfaces
are Abstract Base Classes with
(usually) all abstract methods
• Declaration, not implementation
 Abstract
Base Classes can share methods
and instance variables through
inheritance
 The
skeleton project now contains the
Board class and a refactored version of
the ElevensBoard
 Complete the skeleton code using the
new and improved design
 Add
a class for both the TensBoard and
ThirteensBoard games
• You can copy and paste the code from your
completed ElevensBoard as a starting point
 Implement
the methods necessary to play
these alterations to Elevens
 To run the games in the GUI you will need to
look at the ElevensGUIRunner class and
decide which (minor) changes to make for
• ThirteensGUIRunner
• TensGUIRunner
 This
is a challenging question to answer
mathematically
 Instead, we can run a large number of
simulations on the Elevens game and see
what the outcome would have been for
each
 This
class will manage the simulation for
you, however there are a few additional
methods we need in our ElevensBoard
• playIfPossible
• A couple helper methods:
 playPairSum11IfPossible
 playJQKIfPossible
Rather than ask if the board contains a pair sum
of 11 and then hunting it down again in the
playPairSum11IfPossible method, we need to
alter our design.
 Change the name of containsPairSum11 to
findPairSum11.

• The new method will return a list with the indexes where
an 11 pair was found
• If no 11 pair was found it will return an empty list
This allows us to call replaceSelectedCards
immediately from the findPairSum11 method
 A similar change is needed for containsJQK 
findJQK

 Change
containsPairSum11 to
findPairSum11
 Change containsJQK to findJQK
 Update isLegal and anotherPlayIsPossible to
use findPairSum11 and findJQK
 Add the playPairSum11IfPossible and
playJQKIfPossible helper (private) methods
 Add the playIfPossible method to the
ElevensBoard
• This calls on the helper methods
playPairSum11IfPossible and playJQKIfPossible
 Examine
the code in the
ElevensSimulation.java and
ElevensBoard.java files
 Alter the code to change
I_AM_DEBUGGIN and GAMES_TO_PLAY
to see how the simulation process works.
 So what percentage of the time would you
expect to win at Elevens?
Download