Elevens Lab part III AP Computer Science Related

Elevens Lab part III AP Computer Science

Related Games �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.

Related Games �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.

A Class Hierarchy

(Re)Design � All Boards • need a deck • deal from the deck in the same way � Where should we declare them? � Only the Elevens. Board needs • contains. JQK • contains. Pair. Sum 11 � But what about • is. Legal • another. Play. Is. Possible • All Boards need them but they don`t have the same behaviour

Abstract Classes � 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 • Abstract. Class example = new Abstract. Class(); � Instead they can refer to a subclass which is not abstract • Abstract. Class example = new Sub. Class(); • Sub classes can still be abstract

Board. java �Let`s have a look at the abstract base class Board in the skeleton project.

Polymorphism � Board my. Board = new Elevens. Board(); � Methods implemented in Elevens. Board will be called over those in Board � If no method is implemented in Elevens. Board then it will default to Board � my. Board. is. Legal(list) • Calls the Elevens. Board class � my. Board. deal(int) • Calls the Board class � Poly = many � morph = shape

Abstract Base Class vs. Interface �Interfaces are Abstract Base Classes with (usually) all abstract methods • Declaration, not implementation �Abstract Base Classes can share methods and instance variables through inheritance

Refactored Elevens �The skeleton project now contains the Board class and a refactored version of the Elevens. Board �Complete the skeleton code using the new and improved design

Relatives of Elevens � Add a class for both the Tens. Board and Thirteens. Board games • You can copy and paste the code from your completed Elevens. Board 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 Elevens. GUIRunner class and decide which (minor) changes to make for • Thirteens. GUIRunner • Tens. GUIRunner

What percentage of Elevens games can be won? �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

Elevens. Simulation �This class will manage the simulation for you, however there a few additional methods we need in our Elevens. Board • play. If. Possible • A couple helper methods: �play. Pair. Sum 11 If. Possible �play. JQKIf. Possible

contains. Pair. Sum 11 find. Pair. Sum 11 � Rather than ask if the board contains a pair sum of 11 and then hunting it down again in the play. Pair. Sum 11 If. Possible method, we need to alter our design. � Change the name of contains. Pair. Sum 11 to find. Pair. Sum 11. • 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 replace. Selected. Cards immediately from the find. Pair. Sum 11 method � A similar change is needed for contains. JQK find. JQK

Tasks � Change contains. Pair. Sum 11 to find. Pair. Sum 11 � Change contains. JQK to find. JQK � Update is. Legal and another. Play. Is. Possible to use find. Pair. Sum 11 and find. JQK � Add the play. Pair. Sum 11 If. Possible and play. JQKIf. Possible helper (private) methods � Add the play. If. Possible method to the Elevens. Board • This calls on the helper methods play. Pair. Sum 11 If. Possible and play. JQKIf. Possible

Elevens. Simulation �Examine the code in the Elevens. Simulation. java and Elevens. Board. 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?
- Slides: 16