16
Elevens Lab part III AP Computer Science

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

Embed Size (px)

Citation preview

Page 1: 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

Elevens Lab part IIIAP Computer Science

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

A Class Hierarchy

Page 5: 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

(Re)Design 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

Page 6: 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

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• 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

Page 7: 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

Board.java Let`s have a look at the abstract

base class Board in the skeleton project.

Page 8: 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

Polymorphism 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

Page 9: 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

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

Page 10: 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

Refactored Elevens 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

Page 11: 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

Relatives of Elevens 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

Page 12: 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

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

Page 13: 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

ElevensSimulation 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

Page 14: 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

containsPairSum11 findPairSum11

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

Page 15: 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

Tasks 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

Page 16: 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

ElevensSimulation 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?