#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MAX_VALUE = 1000;
// declare functions
/** \brief the game introduction
* \return true to play or false to quit
*/
bool intro ();
/** \brief get a value between 1 to maximum allowed
* restrict input to allowable values
* \return a valid value
*/
int getNumberInput();
/** \brief get a random value between minVal and maxVal (inclusive)
* \note Not testing if the min is greater than max
* \param minVal lowest value for the random number
* \param maxVal highest value for the random number
* \return the random number
*/
int getRandom( int minVal, int maxVal);
/** \brief can play again if y is initialzed
* \return playA
*
*/
bool playAgain();
/** \brief gives the user a hint to what the number is
* /return 0
*/
int getNumber();
/** \brief initialise any data that does not change during the game
*/
void initGameData();
/** \brief reset any data that does not change during the game
*/
void resetGameData();
//! the number that the player has currently guessed
int numberGuessed;
//! the current random value to guess
int randomNumber;
//! the maximum attempts the player is allowed
int maxAttempts;
//! an attempt counter
int attemptCounter;
//! running the instance
bool running;
int main()
{
srand( time(NULL) );
if (!running)
{
cout << "The time is: " << time(NULL) << endl;
cout << "Max random number is: " << RAND_MAX << " rand " << rand() << endl;
// intro to the game and play or not
running = intro ();
}
// only execute if intro () returns true
// the main game loop
// set the random value, reset the attempt counter
while (running)
{
resetGameData();
do
{
// get the current value to guess from the player
numberGuessed = getNumberInput();
getNumber();
}
while (numberGuessed != randomNumber);
}
cout << "Goodbye and thanks for the fish" << endl;
return 0;
}
int getRandom(int minVal, int maxVal)
{
// get the range of values
int range = maxVal - minVal + 1;
int random = (rand() % range) + minVal;
return random;
}
void initGameData()
{
maxAttempts = 12;
cout << "You have reached the max limit of turns, try again next time!" << endl;
playAgain();
}
void resetGameData()
{
// getting the next random number to guess
randomNumber = getRandom( 1, MAX_VALUE );
// reseting the maxAttempts
maxAttempts = 0;
// set the turn counter to 0
attemptCounter = 0;
cout << "The number is:" << randomNumber << endl;
}
int getNumberInput()
{
int userGuess;
do
{
cout << "Enter a number between 1 and " << MAX_VALUE << " then press Enter ";
cin >> userGuess;
maxAttempts++;
attemptCounter++;
if (maxAttempts == 12 && attemptCounter == 12)
{
initGameData();
}
}
while( userGuess < 1 || userGuess > MAX_VALUE);
cout << "You entered " << userGuess << endl;
return userGuess;
}
bool intro()
{
bool flag = false;
// intro to the game
cout << "A Guess the magic Number Game" << endl;
cout << "Press Y to Continue, any other key to Exit" << endl;
char ch;
cin >> ch;
// convert the char variable to upper case
ch = toupper( ch );
// the test condition
if ( ch == 'Y' )
{
// what to do if the test is true
cout << "Let's play!" << endl;
flag = true;
}
else
{
// what to do if the test is fale
cout << "See you next time!" << endl;
}
return flag;
}
bool playAgain()
{
bool playAgain = false;
char ch;
cout << "Press Y to play again or any other key to exit!" << endl;
cin >> ch;
ch = toupper(ch);
if (ch == 'Y')
{
playAgain = true;
running = true;
main();
}
else
{
playAgain = false;
cout << "See you next time!" << endl;
exit(0);
}
return playAgain;
}
int getNumber()
{
// check if too high/low or correct
if( numberGuessed < randomNumber )
{
cout << numberGuessed << " is too low." << endl;
}
else if(numberGuessed > randomNumber )
{
cout << numberGuessed << " is too high." << endl;
}
else // must be equal
{
cout << "The winner!" << endl;
running = false;
running = playAgain();
}
return 0;
}