Inheritance - Case Study TCP1201: 2013/2014. Case Study – Guessing Game Listed below is the code to play a guessing game using procedural programming.

Slides:



Advertisements
Similar presentations
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
Advertisements

For(int i = 1; i
Slides, Flips, and Turns CLICK HERE TO BEGIN THE GAME!!
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Vectors, lists and queues
Inheritance (2).
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Building Java Programs
MALT©2006 Projector game M512 Time to Times Learn your tables by playing this game. Again and again.
MALT©2006 Projector game M512 Time to Times Learn your tables by playing this game. Again and again.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
For the ultimate in entertainment, play with 2 or more people, individually or as a team Players alternate turns Each player picks an “answer” and must.
1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game.
© 2007 Ray S. Babcock Tracks Game is played on a (nxn) set of squares. There are three possible moves (labeled A,B,C). Players alternate making a move.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Full abstraction * abstract-machine Compositional * black-box Game semantics.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Jeopardy Start Click the Start button to go to the Category Board and begin the game. On the Category Board, click the button of your choice to select.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Insert Category 1 $100 $200 $300 $400 $500 $ 500$500 Insert Category 2 Insert Category 3 Insert Category 4 Insert Category 5.
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
Find the area of the shaded region below. What fraction of the entire figure does the shaded portion make up? 9 cm Compute the following expressions with.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
Game Board Title Your name. Game Board Title Topic 1 – 100 points Enter your answer.
Category 1 Category 5 Category 4 Category 3 Category
RULES OF THE GAME There are 2 rounds. Round 1 - One person from each team comes to the front of the room. Team 1 will give a one-word clue to their “guesser”
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
EXAMPLE 3 Solve an inequality with a variable on one side Fair You have $50 to spend at a county fair. You spend $20 for admission. You want to play a.
1. 2 Review How many bits are in a byte? a) 4 b) 8 c) 16 d) 256.
TODAY WE ARE GOING TO PLAY….. “NAME THAT PROPERTY!”
Bearings Bingo. Pick 9 of the numbers below 110 o 60 o 130 o 208 o 230 o 210 o 330 o 100 o 235 o 170 o 225 o 350 o 240 o 200 o 215 o 334 o.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
T HURSDAY, M ARCH 15, 2012 Practical Money Skills – How Much do you know???? Go to
Jane wins $21 if a die roll shows a six, and she loses $2 otherwise
SATMathVideos.Net A basketball player scored 27 points in a game, which was composed of 1 point baskets (free throws), 2 point baskets and 3 point baskets.
A Group Communication Game Year 10 Psychology: Human Relationships.
Adding Sounds Games Programming in Scratch. Games Programming in Scratch L6 Adding Sounds Learning Objectives Learn how to add sound to a Scratch game.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Topic 4: Looping Statements
Chapter 5: Control Structures II (Repetition)
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
CMPT 201 Functions.
Lab Session-9 CSIT-121 Spring 2005
CLICK HERE TO BEGIN THE GAME!!
Random Number Generation
CS 2308 Exam I Review.
Introduction to Programming
סדר דין פלילי – חקיקה ומהות ההליך הפלילי
Multi-Step Equation Game! Play!.

Guess the letter!.
CS150 Introduction to Computer Science 1
Chapter 5: Control Structures II (Repetition)
PICK 6 Game Board
You must show all steps of your working out.
Question 1.
T. Trimpe Science Scramble Science Experiments T. Trimpe
Facial Hair.
Iteration – While Loops
Presentation transcript:

Inheritance - Case Study TCP1201: 2013/2014

Case Study – Guessing Game Listed below is the code to play a guessing game using procedural programming. In the game, two players attempt to guess a number. Y (Note: your program should include the cstdlib library for the rand() function.) bool checkForWin(int guess, int answer) { cout << "You guessed " << guess << "."; if (answer == guess) { cout << "You're right! You win!" << endl; return true; } else if (answer < guess) cout << "Your guess is too high!" << endl; else cout << "Your guess is too low!" << endl; return false; } TASK 1: Convert this guessing game program to an object-oriented program.

Case Study – Guessing Game void play() { int answer = 0, guess = 0; answer = rand()%100; bool win = false; while (!win) { cout << "\nPlayer 1's turn to guess." << endl; cin >> guess; win = checkForWin(guess, answer); if (win) return; cout << "\nPlayer 2's turn to guess." << endl; cin >> guess; win = checkForWin(guess, answer); } int main() { play(); } HINT The function header of the play function: void play(Player &p1, Player & p2)

Case Study – Guessing Game OUTPUT:

Case Study – Guessing Game class Player { protected: int guess; public: int getGuess() { cin >> guess; return guess; } }; bool checkForWin(int guess, int answer) { cout << "You guessed " << guess << "."; if (answer == guess) { cout << "You're right! You win!\n“; return true; } else if (answer < guess) cout << "Your guess is too high!\n“; else cout << "Your guess is too lo!w\n”; return false; } Object-oriented version of Guessing Game:

Case Study – Guessing Game void play(Player &player1, Player &player2) { int answer = 0, guess = 0; answer = rand()%100; bool win = false; while (!win) { cout << "\nPlayer 1's turn to guess." << endl; guess = player1.getGuess(); win = checkForWin(guess, answer); if (win) return; cout << "\nPlayer 2's turn to guess." << endl; guess = player2.getGuess(); win = checkForWin(guess, answer); } int main() { Player p1, p2; play(p1, p2); }

Case Study – Guessing Game TASK 2: Modify the program to allow the players to be addressed by their names.

Case Study – Guessing Game class Player { protected: string name; int guess; public: Player(string name) : name(name) {} string getName() { return name; } int getGuess() { cin >> guess; return guess; } }; bool checkForWin(int guess, int answer) { cout << "You guessed " << guess << "."; if (answer == guess) { cout << "You're right! You win!\n“; return true; } else if (answer < guess) cout << "Your guess is too high!\n“; else cout << "Your guess is too lo!w\n”; return false; } Guessing Game after adding name attribute:

Case Study – Guessing Game void play(Player &player1, Player &player2) { int answer = 0, guess = 0; answer = rand()%100; bool win = false; while (!win) { cout << player1.getName() << "'s turn to guess.\n“; guess = player1.getGuess(); win = checkForWin(guess, answer); if (win) return; cout << player2.getName() << "'s turn to guess.\n" guess = player2.getGuess(); win = checkForWin(guess, answer); } int main() { Player p1("Ali"); Player p2(“Baba"); play(p1, p2); }

Case Study – Guessing Game TASK 3: Modify the program to allow a player to play with the computer.

Case Study – Guessing Game class Player { protected: string name; int guess; public: Player(string name) : name(name) {} string getName() { return name; } int getGuess() { guess= 0; return guess; } }; Guessing Game (Human VS Computer): class HumanPlayer : public Player { public: HumanPlayer(string name):Player(name) {} int getGuess() { cin >> guess; return guess; } }; class ComputerPlayer : public Player { public: ComputerPlayer():Player("Computer") {} int getGuess() { guess = rand()%100; return guess; } };

Case Study – Guessing Game bool checkForWin(int guess, int answer) { cout << "You guessed " << guess << "."; if (answer == guess) { cout << "You're right! You win!\n“; return true; } else if (answer < guess) cout << "Your guess is too high!\n“; else cout << "Your guess is too lo!w\n”; return false; }

Case Study – Guessing Game void play(HumanPlayer &player1, ComputerPlayer &player2) { int answer = 0, guess = 0; answer = rand()%100; bool win = false; while (!win) { cout << player1.getName() << "'s turn to guess.\n“; guess = player1.getGuess(); win = checkForWin(guess, answer); if (win) return; cout << player2.getName() << "'s turn to guess.\n" guess = player2.getGuess(); win = checkForWin(guess, answer); } int main() { HumanPlayer p1("Ali"); ComputerPlayer p2; play(p1, p2); }

Case Study – Guessing Game TASK 4: Modify the program such that the play method can accept two Human players or one Human player and one Computer player. The function header for play should be: void play(Player &player1, Player &player2) HINT: POLYMORPHISM!!!

Case Study – Guessing Game class Player { protected: string name; int guess; public: Player(string name) : name(name) {} string getName() { return name; } virtual int getGuess() = 0; }; Guessing Game (Human VS Computer or Human VS Human): class HumanPlayer : public Player { public: HumanPlayer(string name):Player(name) {} virtual int getGuess() { cin >> guess; return guess; } }; class ComputerPlayer : public Player { public: ComputerPlayer():Player("Computer") {} virtual int getGuess() { guess = rand()%100; return guess; } };

Case Study – Guessing Game bool checkForWin(int guess, int answer) { cout << "You guessed " << guess << "."; if (answer == guess) { cout << "You're right! You win!\n“; return true; } else if (answer < guess) cout << "Your guess is too high!\n“; else cout << "Your guess is too lo!w\n”; return false; }

Case Study – Guessing Game void play(Player &player1, Player &player2) { int answer = 0, guess = 0; answer = rand()%100; bool win = false; while (!win) { cout << player1.getName() << "'s turn to guess.\n“; guess = player1.getGuess(); win = checkForWin(guess, answer); if (win) return; cout << player2.getName() << "'s turn to guess.\n" guess = player2.getGuess(); win = checkForWin(guess, answer); } int main() { HumanPlayer p1(“Catherine"); ComputerPlayer p2; play(p1, p2); HumanPlayer p3("Ali"); HumanPlayer p4("Baba"); play(p3, p4); }