Case Study 2

advertisement
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.
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;
}
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:
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;
}
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;
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;
}
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 Player {
protected:
string name;
int guess;
public:
Player(string name)
: name(name) {}
string getName()
{
return name;
}
int getGuess() {
guess= 0;
return guess;
}
};
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“;
}
else if (answer < guess)
cout << "Your guess is too high!\n“;
else
cout << "Your guess is too lo!w\n”;
return false;
}
return true;
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 Player {
protected:
string name;
int guess;
public:
Player(string name)
: name(name) {}
string getName()
{
return name;
}
virtual int
getGuess() = 0;
};
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“;
}
else if (answer < guess)
cout << "Your guess is too high!\n“;
else
cout << "Your guess is too lo!w\n”;
return false;
}
return true;
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);
}
Download