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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#include<iostream>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iomanip>
//declare user defined functions
void rules(void);
int minVal (void);
int maxVal (void);
int mysteryNumber (int, int);
bool checkGuess (int guess, int computerChoice);
int gameStats (int gamesPlayed, int numGuess);
using namespace std;
int main ()
{
//declare variables
int guess = 0;
int gamesPlayed = 0;
int numGuess = 0;
int min, max;
int computerChoice = 0;
bool guessCorrect = false;
char Play = ' ';
srand(time(0));
while (Play != 'N')
{
//display the rules
rules ();
//set the mystery number
min = minVal ();
max = maxVal ();
computerChoice = mysteryNumber (min, max);
//player guesses
cout<<"\nWhat is your guess?"<<endl;
cin>>guess;
//check guess
do
{
//check if guess is correct
guessCorrect = checkGuess (guess, computerChoice);
}
while (guessCorrect == false);
{
//ask to play again
cout<<"\nWould you like to play again?";
cout<<"\nY/N"<<endl;
cin>>Play;
toupper(Play);
}
//check for incorrect response
if (Play != 'Y' || Play != 'N')
{
cout<<"Please enter Y/N"<<endl;
}
}
}
//setup user definded functions
int mysteryNumber (int min, int max)
{
//declare variable
int mystNum = 0;
//pick random number
mystNum = rand() %(max - min) + min;
//return [new] mystery number
return mystNum;
}
int minVal (void)
{
//declare variable
int minNum = 0;
//ask if user defined or auto
cout<<"\nIf you would like to define the mystery number range, please enter the lowest number below.";
cout<<"\nIf you leave it blank, it will default to 0"<<endl;
cin>>minNum;
//check to see if left blank
if (minNum = ' ')
{
minNum = 0;
}
//return
return minNum;
}
int maxVal (void)
{
//declare variable
int maxNum = 0;
//ask if user defined or auto
cout<<"\nIf you would like to define the mystery number range, please enter the highest number below.";
cout<<"\nIf you leave it blank, it will default to 100"<<endl;
cin>>maxNum;
//check to see if left blank
if (maxNum = ' ')
{
maxNum = 100;
}
//return
return maxNum;
}
int gameStats (int gamesPlayed, int numGuess)
{
//declare variable
int gameAve = 0;
//update game average value
gameAve = gamesPlayed / numGuess;
//return game average value
return gameAve;
}
void rules (void)
{
cout<<"\t\tWelcome to the Hi / Lo Game";
cout<<"\nHere are the rules of the game: ";
cout<<"\n1)\tYou guess the mystery number";
cout<<"\n2)\tI tell you if your number is too high, too low, or correct";
cout<<"\n3)\tThe default settings for the number range is 0-100. You will have the option to set the range if you want";
cout<<"\n3)\tThe game tracks your stats, once you guess correct you will see how many games played, the number of guesses, and your average";
cout<<"\n\t\tReady to play? Press Enter!";
}
bool guessCheck (int guess, int computerChoice)
{
//set string
if (guess > computerChoice)
{
cout<<"\nYour guess is too high, please try again";
return false;
}
if (guess < computerChoice)
{
cout<<"\nYour guess is too low, please try again";
return false;
}
if (guess = computerChoice)
{
cout<<"\nThat's correct! YAY!";
return true;
}
else
{
cout<<"\nThat is not a valid guess. Please enter your guess in number form.";
return false;
}
}
| |