Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.

Slides:



Advertisements
Similar presentations
1 CSC 222: Computer Programming II Spring 2004 HW1 review arrays vs. vectors class design object-oriented design data objects + functionality example:
Advertisements

© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
CS12230 Introduction to Programming Lecture 4-x – Consolidation 1.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
It’s All in the Cards Adding and Subtracting Integers
CS18000: Problem Solving and Object-Oriented Programming.
Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Robo David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to the engineering.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
o Simulate a deck of playing cards o Shuffle the deck o Deal (5) cards into the hand o Turn over the first card o The user must guess whether the next.
o Simulate a deck of playing cards o Shuffle the deck o Deal 5 cards into the hand o Turn over the first card o The user must guess whether the next card.
VOCABULARY  Deck or pack  Suit  Hearts  Clubs  Diamonds  Spades  Dealer  Shuffle  Pick up  Rank  Draw  Set  Joker  Jack 
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Putting together a complete system Chapter 10. Overview  Design a modest but complete system  A collection of objects work together to solve a problem.
Sequencing 1 Shuffle the cards and place in a pile with the numbers facing down. Order the cards from 0 to 11. How fast can you do this?
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Agenda Object Oriented Programming Reading: Chapter 14.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Chapter 9 Putting together a complete system. This chapter discusses n Designing a complete system. n Overview of the design and implementation process.
Sequencing 1 Shuffle the cards and place in a pile with the numbers facing down. Order the cards from 0 to 11. How fast can you do this?
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
MA471 Fall 2003 Lecture 2. On With The Games Today we are going to watch each group play a couple of rounds of cards. We will go through the game slowly.
1 CSC 221: Computer Programming I Fall 2006 Lists, data storage & access  ArrayList class  methods: add, get, size, remove, contains, set, indexOf, toString.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
CS12230 Introduction to Programming Extra example– More on responsibilities 1.
© 2004 Pearson Addison-Wesley. All rights reserved6-1 Program Development In your CS102 project, and in many others, the basic activities are:  requirements.
Click to edit Master text styles Stacks Data Structure.
Shlomo Hershkop Basics overview. Shlomo Hershkop Basic Review - Overview Practice coding Practice coding finger guessing game finger guessing.
Section 5.5 Application: The Card Game of War. 5.5 Application: The Card Game of War A deck of cards is shuffled and dealt to two players The players.
The need for Programming Languages
Lecture 5 of Computer Science II
Java Coding – part 2 David Davenport Computer Eng. Dept.,
A game of precision, quick reaction and observation.
Software Development Java Classes and Methods
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept.,
Dot Cards Face-Off Game
Let’s Practice Play 2 Missing Digits !
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Task 2 Implementation help
Java Coding 8 David Davenport Computer Eng. Dept.,
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Stacks.
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The Card Game -

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 saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

Card Games As an example of Object Oriented Design

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.

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.

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 Table player Pack of cards

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

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.

Simple Game class properties Pack of cards 4 Players 4 Scores 4 Piles of cards on table constructor ( 4 players) creates the game with the given players methods playGame() plays a complete game sets scores getScore( player number) returns players score Represents a single card game played by 4 players

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

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

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 playerNos score + getWinners() : ?? + toString() : String Represents a ScoreCard for a card game

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

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 : String constructor ( cardNumber) creates card with given position number in ordered pack!

Playing the Game Algorithm for playGame method Create the pack of cards Shuffle the pack Deal all the pack between the players Create empty piles of cards on table Set all players scores to zero For each round (until players have no cards left) Each player plays card by adding it to their pile on table Find biggest value card on top of piles on table Increment scores of players who played cards with biggest value

Playing the Game public void initGame() // create fullpack of cards & shuffle // deal all cards to players // create score card // create empty set of cards on table for each player // set round no to 0 // set turn of player no to 0

Playing the Game public boolean playTurn( SimplePlayer p, Card c) // if game is over return false // if it's not p's turn return false // p's turn so take p's card & put on table // if not end of round then // move to next players turn else // end of round, update scores & // if not end of game then // move to next round & next player's turn return true private void updateScores() // find max of cards just placed on table // increment scores of player(s) with max card(s)

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!