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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include <iostream>
using namespace std;
//Declare constants for range of values, # of guesses
const int LowBound = 1;
const int HighBound = 100;
const int GuessMax = 6;
int main()
{
//Tell player about the game
cout << "Welcome to the Guessing Game!" << endl;
cout << "The point of the game is for me to guess" << endl;
cout << "the number that you're thinking of." << endl;
cout << "I have six guesses or else you win!" << endl;
//Declare variables for scoring
int CompWins = 0;
int UserWins = 0;
//See if the player want to play/continue playing
char PlayAns;
cout << "would you like to play?" << endl;
cout << "Enter 'Y' for yes, 'N' for no" << endl;
cin >> PlayAns;
For (PlayAns == 'Y'){
// Set up variables to store the known range of the valid integer values.
int Lower = LowBound;
int Higher = HighBound;
// Use a variable to hold the current guess.
int Guess;
/*Use a variable to store the users responses. ( L = Too low.*/
/* H = Too high. Anything else = correct.)*/
char = Feedback;
// Make the first guess.
Guess = (Lower + Higher) / 2;
cout << "I guess" << Guess << endl;
cout << "Please enter H if I guessed too high, L for too low, " << endl;
cout << "and anything else if I am correct." << endl;
cin >> Feedback;
//See if we are right, or keep guessing.
If (Feedback = L) {
//The guess is too low so the last guess becomes the new Higher
Higher = Guess;
} Else If (Feedback = H) {
// The guess is too high so the last guess becomes the new Lower
Lower = Guess;
} Else {
// I got it right.
cout << "I won in one guess." << endl;
cout << "Computer's Score: " << NewScoreC = CompWins + 1 << endl;
cout << "User's Score: " << NewScoreU = UserWins << endl;
return 0;
}
// Calculate the second guess.
Guess = (Lower + Higher) / 2;
cout << "I guess" << "Guess" << "Please respond H, L, or R." << endl;
cin >> Feedback;
If (Feedback = L) {
Higher = Guess;
} Else If (Feedback = H) {
Lower = Guess;
} Else {
cout << "I won in two guesses." << endl;
cout << "Computer's Score: " << NewScoreC = CompWins + 1 << endl;
cout << "User's Score: " << NewScoreU = UserWins << endl;
return 0;
}
// Calculate the third guess.
Guess = (Lower + Higher) / 2;
cout << "I guess" << "Guess" << "Please respond H, L, or R." << endl;
cin >> Feedback;
If (Feedback = L) {
Higher = Guess;
} Else If (Feedback = H) {
Lower = Guess;
} Else {
cout << "I won in three guesses." << endl;
cout << "Computer's Score: " << NewScoreC = CompWins + 1 << endl;
cout << "User's Score: " << NewScoreU = UserWins << endl;
return 0;
}
// Calculate the fourth guess.
Guess = (Lower + Higher) / 2;
cout << "I guess" << "Guess" << "Please respond H, L, or R." << endl;
cin >> Feedback;
If (Feedback = L) {
Higher = Guess;
} Else If (Feedback = H) {
Lower = Guess;
} Else {
cout << "I won in four guesses." << endl;
cout << "Computer's Score: " << NewScoreC = CompWins + 1 << endl;
cout << "User's Score: " << NewScoreU = UserWins << endl;
return 0;
}
// Calculate the fifth guess.
Guess = (Lower + Higher) / 2;
cout << "I guess" << "Guess" << "Please respond H, L, or R." << endl;
cin >> Feedback;
If (Feedback = L) {
Higher = Guess;
} Else If (Feedback = H) {
Lower = Guess;
} Else {
cout << "I won in five guesses." << endl;
cout << "Computer's Score: " << NewScoreC = CompWins + 1 << endl;
cout << "User's Score: " << NewScoreU = UserWins << endl;
return 0;
}
// Calculate the FINAL GUESS.
cout << "You picked" << "Guess" << endl;
cout << "Right? (Y or N)" << endl;
cin >> Feedback;
If (Feedback != Y) {
cout << "You beat me!" << endl;
} Else If (Feedback == Y) {
cout << "I won!" << endl;
cout << "Computer's Score: " << NewScoreC = CompWins << endl;
cout << "User's Score: " << NewScoreU = UserWins + 1 << endl;
return 0;
}
} While (PlayAns = 'N'){
cout << "Thanks for Playing!" << endl;
return 0;
}
}
|