1 / 17

Inheritance - Case Study

Inheritance - Case Study. TCP1201: 2013/2014. Case Study – Guessing Game.

zazu
Télécharger la présentation

Inheritance - Case Study

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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.) TASK 1: Convert this guessing game program to an object-oriented program. boolcheckForWin(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; }

  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 Object-oriented version of Guessing Game: class Player { protected: int guess; public: intgetGuess() { cin >> guess; return guess; } }; boolcheckForWin(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; }

  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 Guessing Game after adding name attribute: class Player { protected: string name; intguess; public: Player(string name) : name(name) {} string getName() { return name; } intgetGuess() { cin >> guess; return guess; } }; boolcheckForWin(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; }

  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 Guessing Game (Human VS Computer): class HumanPlayer : public Player { public: HumanPlayer(string name):Player(name) {} intgetGuess() { cin >> guess; return guess; } }; class ComputerPlayer : public Player { public: ComputerPlayer():Player("Computer") {} intgetGuess() { guess = rand()%100; return guess; } }; class Player { protected: string name; intguess; public: Player(string name) : name(name) {} string getName() { return name; } intgetGuess() { guess= 0; return guess; } };

  12. Case Study – Guessing Game boolcheckForWin(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 Guessing Game (Human VS Computer or Human VS Human): class HumanPlayer : public Player { public: HumanPlayer(string name):Player(name) {} virtual intgetGuess() { cin >> guess; return guess; } }; class ComputerPlayer : public Player { public: ComputerPlayer():Player("Computer") {} virtual intgetGuess() { guess = rand()%100; return guess; } }; class Player { protected: string name; intguess; public: Player(string name) : name(name) {} string getName() { return name; } virtual int getGuess() = 0; };

  16. Case Study – Guessing Game boolcheckForWin(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); HumanPlayerp3("Ali"); HumanPlayerp4("Baba"); play(p3, p4); }

More Related