1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "humanplayer.h"
#include "computerplayer.h"
#include "player.h"
using namespace std;
bool checkForWin(int guess, int answer, int guessNumber,computerplayer &obj);
void play(Player& player1, Player& player2,computerplayer& obj);
int main()
{
humanplayer one,two; //human player 1, human player 2
computerplayer ein, zwei; //computer player 1, computer player 2
computerplayer obj; //for setting guess range of the computer
int select;
char ans;
cout <<"Welcome to the Guessing Game!\n";
do {
cout <<"*****************************\n";
cout <<"Select the game type (1-3) from the menu, any other number to quit.\n";
cout <<"Human vs. Human\n";
cout <<"Human vs. Computer\n";
cout <<"Computer vs. Computer\n";
cout <<"*****************************\n";
cin >> select;
switch(select)
{
case 1:
//human vs. human
play(one,two, obj);
break;
case 2:
//human vs. computer
play(one,ein,obj);
break;
case 3:
//computer vs. computer
play(ein,zwei,obj);
break;
}
cout <<"do you want to play again? (y/n)" <<endl;
cin >> ans;
}while(ans == 'y' || ans == 'Y');
cout <<endl;
cout <<"*************Results***************\n";
cout <<"Human player 1 won " <<one.getWins() <<" times." <<endl;
cout <<"Human player 2 won " <<two.getWins() <<" times." <<endl;
cout <<"Computer player 1 won " <<ein.getWins() <<" times." <<endl;
cout <<"Computer player 2 won " <<zwei.getWins() <<" times." <<endl;
cout <<"***********************************\n";
return 0;
}
bool checkForWin(int guess, int answer, int guessNumber,computerplayer &obj)
{
cout << "Guess no. " << guessNumber << ", you guessed " << guess << ". ";
if (answer == guess)
{
cout << "You're right! You win!" << endl;
return true;
}
else if (answer < guess)
{
obj.set_high_range(guess);
cout << "Your guess is too high." << endl;
}
else
{
obj.set_low_range(guess);
cout << "Your guess is too low." << endl;
}
return false;
}
void play(Player& player1, Player& player2,computerplayer& obj)
{
int answer=0, guess=0, guessNumber=0;
answer = rand() % 100;
bool win = false;
while (!win)
{
cout << "Player 1's turn to guess." << endl;
guess = player1.getGuess();
guessNumber++;
win = checkForWin(guess, answer, guessNumber,obj);
if (win) {
player1.setWins(player1.getWins()+1);
return;
}
cout << "Player 2's turn to guess." << endl;
guess = player2.getGuess();
guessNumber++;
win = checkForWin(guess, answer, guessNumber,obj);
if (win)
player2.setWins(player2.getWins()+1);
}
}
|