170 likes | 291 Views
Inheritance - Case Study. TCP1201: 2013/2014. Case Study – Guessing Game.
E N D
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.) 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; }
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 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; }
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 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; }
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 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; } };
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; }
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 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; };
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; }
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); }