java coding 8 david davenport computer eng. dept., bilkent university ankara - turkey. email:...
TRANSCRIPT
![Page 1: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/1.jpg)
Java Coding 8
David Davenport
Computer Eng. Dept.,Bilkent UniversityAnkara - Turkey.
email: [email protected]
Object-Oriented Design Example- The Card Game -
![Page 2: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/2.jpg)
IMPORTANT… Students…
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 email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it.
thank you, David.
![Page 3: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/3.jpg)
Card Games
As an example of
Object Oriented
Design
![Page 4: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/4.jpg)
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.
![Page 5: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/5.jpg)
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.
![Page 6: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/6.jpg)
Picture it…
Objects the Game? Pack of cards Players 1, 2, 3 & 4 the Table? Score card?
(Player 1, 2, 3 & 4 scores)
Players 1, 2, 3 & 4 Cards on table
Players 1, 2, 3 & 4 Cards in hand
player
score
score
score
score
Table
player
player
player
Pack of cards
![Page 7: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/7.jpg)
Classes
CleverGame Pack of cards 4 Players Scorecard
(with 4 scores on) 4 Piles of cards
on table
ScoreCard Set of scores
Player Name Set of cards in hand
Cards Collection of cards
Card Face value Suit
![Page 8: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/8.jpg)
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.
![Page 9: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/9.jpg)
CleverGame 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
![Page 10: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/10.jpg)
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
![Page 11: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/11.jpg)
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
![Page 12: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/12.jpg)
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
![Page 13: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/13.jpg)
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
: int
: int
: String
constructor ( cardNumber)creates card with given position number in ordered pack!
![Page 14: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/14.jpg)
ToDo… Decide on implementation details
For card, cards collection, …
Design other algorithms Shuffle, deal, …
Build & test
Extend Add GUI (Graphical User Interface) Make clever games & playable over Internet!
![Page 15: Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Object-Oriented Design Example - The](https://reader036.vdocuments.net/reader036/viewer/2022062312/5519bf4b5503466f578b4af7/html5/thumbnails/15.jpg)