and also a limit of three tries before game is over.
As I added in features like allowing the user to choose the range for
the random number and the 3 tries, the conditionals seemed to
get a bit tangled.
While it runs fine, I'm not sure if I handled it the best possible way. I would love some
suggestions. Any design theory about setting up conditionals would be
great.
Thanks
Michael
- Code: Select all
#include <iostream>
#include <string>
using namespace std;
string strTriesLeft (int intNumTriesPassed)
{
if ((intNumTriesPassed == 2) || (intNumTriesPassed == 0))
{
return " tries left! ";
}
else
{
return " try left! ";
}
}
int intGenRand (int HiRange)
{
return rand() % (HiRange - 1 + 1) + 1;
}
int main()
{
//Variable Declarations
int intTheNumber = 0;
int intPlayerNumber = 0;
int intNumTries = 3;
int intHiRandRange = 0;
bool bolLastOver100 = false;
bool bolFirstTime = true;
string strPlayAgain = "y";
//Main Game Loop
for (;;)
{
if (bolFirstTime)
{
cout <<"You'll be guessing a number from 1 to something. \n";
cout <<"What number should \'something\' be? ";
cin >> intHiRandRange;
intTheNumber = intGenRand(intHiRandRange);
bolFirstTime = false;
}
else
{
bolFirstTime = false;
}
//cout << intTheNumber << endl;
cout << "I'm thinking of a number between 1 and " << intHiRandRange << ". Your guess? \n" << endl;
cin >> intPlayerNumber;
if (intPlayerNumber == intTheNumber)
{
cout <<"YOU WON!!! \n";
playtest:
cout <<"Play Again? (enter 'y' to continue) \n";
cin>> strPlayAgain;
intTheNumber = intGenRand(intHiRandRange);
system("cls");
}
else
{
if (intPlayerNumber > intTheNumber)
{
if ((bolLastOver100 == true) && (intPlayerNumber > 6) )
{
system("cls");
--intNumTries;
if (intNumTries > 0)
cout << "Remember, it's a number between 1 and " << intHiRandRange << ". \n";
cout << "You have " << intNumTries << strTriesLeft(intNumTries) << endl;
}
else if (intPlayerNumber > intHiRandRange)
{
bolLastOver100 = true;
system("cls");
cout << "That was too high. ";
--intNumTries;
cout << "You have " << intNumTries << strTriesLeft(intNumTries) << endl;
}
else
{
system("cls");
cout << "That was too high ";
--intNumTries;
cout << "You have " << intNumTries << strTriesLeft(intNumTries) << endl;
}
}
else
{
if (intPlayerNumber < intTheNumber)
system("cls");
cout << "That was too low. ";
--intNumTries;
cout << "You have " << intNumTries << strTriesLeft(intNumTries) << endl;
}
}
if (intNumTries == 0)
{
intNumTries = 3;
goto playtest;
}
if ((strPlayAgain != "y") && (strPlayAgain != "Y"))
{
break;
}
}
return 0;
}
