Download presentation
Presentation is loading. Please wait.
1
Java Coding 8 David Davenport Computer Eng. Dept.,
Object-Oriented Design Example - The Card Game - David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Updated: 14/02/2017 ~ not much so far! Previous: 21/2/2015 – removed old irrelevant slides, changed CleverGame to CardGame. Previous: 13/2/13 – minor change to Cards class, etc. to aid classroom flow… Previous: 10/2/10 – now uses interactive style for game class!
2
IMPORTANT… Students… Instructors…
This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.
3
As an example of Object Oriented Design
Card Games As an example of Object Oriented Design
4
The Problem… Design & implement a program to play a simple game of cards between four players. To begin, a full pack of cards are shuffled and dealt face-down to the players. The game then proceeds in rounds. For each round, players play the top card from their hand and add it face-up to a pile on the table in front of them. The player who plays the highest value card is the winner of the round and their score is incremented by one. When all of the cards have been played the player with the highest score is declared the winner of the game. Make sure you understand exactly what is required. Is there anything that needs clarification? For example, - what happens if two or more players have the same value cards? (no one wins, everyone wins, highest value suit wins…) - can there be more than one overall winner of the game? It is quite often necessary to negotiate with the “customer” as to their exact requirements.
5
The Problem… Design & implement a program to play a simple game of cards between four players. To begin, a full pack of cards are shuffled and dealt face-down to the players. The game then proceeds in rounds. For each round, players play the top card from their hand and add it face-up to a pile on the table in front of them. The player who plays the highest value card is the winner of the round and their score is incremented by one. When all of the cards have been played the player with the highest score is declared the winner of the game. Isolate the nouns in the problem statement. These will probably correspond to objects you need in your design. You might also try visualising it (since a picture is worth a thousand words…)
6
Picture it… Objects the Game? Pack of cards Players 1, 2, 3 & 4
score Table Pack of cards Objects the Game? Pack of cards Players 1, 2, 3 & 4 the Table? 4 chairs? Score card? (Player 1, 2, 3 & 4 scores) Cards on table for players 1, 2, 3 & 4 Cards in hand for players 1, 2, 3 & 4 The table is irrelevant as such (unless we want to include the 4 chairs & perhaps a waitress too!) Our simple game thus comprises; a pack of cards, 4 players, 4 piles of cards on the table and 4 scores. We may chose to collect the 4 scores together on a scoresheet. We could actually ignore the piles of the table too. It all depends on how detailed you want to make it!
7
Classes Player CardGame Name Pack of cards Set of cards in hand
Collection of cards Card Face value Suit CardGame Pack of cards 4 Players Scorecard (with 4 scores on) 4 Piles of cards on table ScoreCard Set of scores (one for each player) Knowing objects we need, we can determine classes (ie. categorize them!), - players (obviously), - sets of cards (for the full pack, the piles on the table & in the players hands) - and, of course, the individual cards in the sets! We may also consider not restricting our implementation to just 4 players, but rather make it more general. Notice the top-down like structure here. The game is composed of cards and players. Players are composed of cards too. And each are composed of individual cards. We now need to specify these classes in more detail (properties, constructors, methods)
8
The Problem… Design & implement a program to play a simple game of cards between four players. To begin, a full pack of cards are shuffled and dealt face-down to the players. The game then proceeds in rounds. For each round, players play the top card from their hand and add it face-up to a pile on the table in front of them. The player who plays the highest value card is the winner of the round and their score is incremented by one. When all of the cards have been played the player with the highest score is declared the winner of the game. To determine possible methods look at what verbs are applied to what nouns in the original problem statement. - play (game) - shuffle & deal –remove top card from- (pack of cards) - play top card from hand (player) & add dealt card to hand (player) - add card to (pile of cards on table) - increment (score) ??? Another idea is to play-act the game. A person is assigned to act out the role of each of the objects together they attempt to play the game, noting down what interactions, etc. need to take place. And, perhaps, consider what would be needed for a display (view class) Now detail the classes one-by-one…
9
Represents a single card game played by 4 players
CardGame class properties Pack of cards, 4 Players ScoreCard, 4 Piles of cards on table constructor ( 4 players) creates the game with the given players Methods + playTurn(Player, Card) : boolean + isTurnOf(Player) : boolean + isGameOver() : boolean + getScore( playerNumber) : int + getName( playerNumber) : String + getRoundNo() : int + getTurnOfPlayerNo() : int + getWinners() : set of Player Represents a single card game played by 4 players What services must this class offer? Imagine you have a room full of such “games”. The players may come in later –have an addPlayer method? And removePlayer? Then may want a way to say “startGame” which would return true only if sufficient players where there And a way to terminate the game before completion? ScoreCard - may include player names as well as scores (need getScores() to be able to show scores at any time, e.g. use ScoreCard’s toString ) Note: Only public methods are listed here. The properties will almost certainly be protected or even private, but not public! To easily support isGameOver, getRoundNo, getTurnOfPlayerNo & isTurnOf … add roundNo & turnOf properties.
10
Represents a single player for a card game
Player class properties name set of cards in hand constructor ( name) creates player with name & empty hand methods getName() returns players name add( Card) add the card to players hand playCard() removes and returns the top card from the players hand Represents a single player for a card game Think about get & set methods for each property. Players never change name! Already know we need to add cards to players hand when they are dealt out & player must play top card during each round of game (this removes it from their hand!) Later on we can change playCard for something more “intelligent”.
11
Represents a ScoreCard for a card game
ScoreCard class properties Set of scores constructor ( noOfPlayers) initialises scorecard with noOfPlayers entries all set to zero methods + getScore( playerNo) : int returns score for specified player + update( playerNo, amount) add amount to playerNo’s score + getWinners() : ?? + toString() : String Represents a ScoreCard for a card game May also want to include the corresponding player names. ?divide into scoreCardEntry (name & score)? Then need set of playernames in construtor too. & provide update( playerName, amount) & getScore( playerName) >>>> Maybe ask students what changes they would make to the basic ScoreCard class if we wanted names too? >>>> it is important they understand the need to have a coherent set of methods
12
Represents a set of zero or more playing cards
Cards class properties Collection of cards constructor (fullPack) creates a variable sized collection of cards with no cards in it if fullPack is false or a full pack if true! methods + getTopCard() removes & returns top card from collection + addTopCard( Card) adds the card to the collection - createFullPackOfCards() + shuffle() randomises order of cards in collection Represents a set of zero or more playing cards Whilst these may form a minimum set, we may consider some other useful functions, - getBottomCard (useful for cheats?) - addBottomCard( card) - addCard( Card, position) - removeCard( position) - peekAtTopCard() useful for cheats and to find who has highest value card without removing then adding - createFullPackOfCards() {add constructor with boolean parameter to create empty set or full pack} - count() return number of cards in collection - orderBySuit() - orderByFaceValue( ascending) {generalise these by having general sort & passing different compareTo methods?} - toString() - iterator?? Notice that we have not yet committed to a particular implementation for the collection (array, ArrayList, Vector…) Important to realise the analogy to the IntBag class they are doing in the lab!
13
Represents a single playing card
Card class properties faceValue suit constructor ( faceValue, suit) creates card with given face value & suit methods getFaceValue() returns faceValue getSuit() returns suit toString() Represents a single playing card : int : int constructor ( cardNumber) creates card with given position number in ordered pack! Need to be able to create specific card and get its face value (to decide winner) Since it may be useful later, we include suit too. {may use to break round tie, for example.} Alternative constructors may also prove useful one day! Have we missed anything? - face up/down?? – images on front & back?? Again implementation left for later. Writing a toString() method is always a good idea (for testing, at least!) - define public final static String[] SUITS = { “Hearts”, “Diamonds”, “Clubs”, “Spades”}; plus FACES for use in toString and possibly other conversion routines. Assume int properties & write code Ask for .equals & .compareTo as well, perhaps? Change represenation – use cardNo for property & change methods accordingly Note: as of Java 5.0 could also use enumerations Extra: The Unicode standard now includes playing cards! see How do you output such Unicode characters in Java? : int : int : String
14
ToDo… Decide on implementation details Design other algorithms
For card, cards collection, … Design other algorithms Shuffle, deal, … Build & test Design is top-down Implementation & Test is bottom-up! Extend Add GUI (Graphical User Interface) Make clever games, playable over Internet & on mobile devices! How to test? Create class with main method & in it put… Player p; Card c; p = new Player( “David”); c = new Card(0); // Ace of Hearts! p.add(c); p.add( new Card(1) ); // Two of Hearts System.out.println( p); // assuming wrote toString() in Person class! Draw picture of data structure during execution.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.