> 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)"> > 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)">
Download presentation
Presentation is loading. Please wait.
Published byRaegan Pellett Modified over 9 years ago
1
Inheritance - Case Study TCP1201: 2013/2014
2
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.
3
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)
4
Case Study – Guessing Game OUTPUT:
5
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:
6
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); }
7
Case Study – Guessing Game TASK 2: Modify the program to allow the players to be addressed by their names.
8
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:
9
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); }
10
Case Study – Guessing Game TASK 3: Modify the program to allow a player to play with the computer.
11
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; } };
12
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; }
13
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); }
14
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!!!
15
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; } };
16
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; }
17
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); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.