Please understand my request before commenting, I'm looking for positive critiques of my work. Anything I've done which could be done better while using the same, or similar, restrictions on data types and #include files that I've used. To put it another way, if you see anything that could be done more efficiently, using the same restrictions, please advise.
I know there's more elegant ways to do this. I'm just trying to push my understanding of the basic C++ constructs, before getting into more advanced C++ implementations.
code:
//02_ex_03_guess_my_number_aplha
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
bool exitGame = false;
bool playAgain = true;
bool roundOver = false;
char playAgainYes = 'y';
int playModeSelect = 0;
int computersNumber = 0;
int tooLow = 0;
int tooHigh = 101;
int playersNumber = 0;
int tries = 0;
enum playMode { playerVersusCpu = 1, cpuVersusPlayer, exitMenu};
srand(static_cast<unsigned int>(time(0)));
cout << "\tGuess My Number alpha\n\n\n";
do
{
cout << "1 - Player Versus Computer\n";
cout << "2 - Computer Versus Player\n";
cout << "3 - Exit\n";
cout << "Select a game mode: ";
cin >> playModeSelect;
switch (playModeSelect)
{
case playerVersusCpu:
cout << "You have selected Player VS Computer\n";
do
{
tries = 0;
playAgainYes = 'n';
cout << "Welcome. Try to guess my number: ";
computersNumber = (rand()) % 100 + 1;
do
{
cout << "DEBUG " << computersNumber;
cin >> playersNumber;
if (playersNumber > computersNumber)
{
cout << "Too bad. Your guess was too high.\n";
cout << "Try again: ";
++tries;
}
else if (playersNumber < computersNumber)
{
cout << "Too bad. Your guess was too low.\n";
cout << "Try again: ";
++tries;
}
else if (playersNumber = computersNumber)
{
cout << "You did it. You guessed my number.\n";
++tries;
cout << "It only took you " << tries << " tries.\n";
do
{
cout << "Would you like to play again? ('y'/'n'): ";
cin >> playAgainYes;
if (playAgainYes == 'y')
{
playAgain = true;
cout << playAgainYes << "DEBUG\n";
}
else if (playAgainYes == 'n')
{
playAgain = false;
cout << playAgainYes << "DEBUG\n";
}
else
{
cout << "Inncorrect input\n";
cout << "Select 'y' or 'n': ";
}
} while ((playAgainYes != 'y') && (playAgainYes != 'n' ));
}
} while (playersNumber != computersNumber);
} while (playAgainYes == 'y');//
break;
case cpuVersusPlayer:
cout << "\n\nYou have selected Computer VS Player\n";
do
{
cout << "Pick a number: ";
cin >> playersNumber;
tries = 0;
tooLow = 0;
tooHigh = 101;
do
{
computersNumber = rand() % 100 + 1;
while ((computersNumber != playersNumber) && (computersNumber > tooLow) && (computersNumber < tooHigh))
{
++tries;
cout << "Is it, " << computersNumber << "?\n";
cout << "This was my " << tries << " try.\n";
if ((computersNumber > tooLow) && (computersNumber < playersNumber))
{
tooLow = computersNumber;
}
else if ((computersNumber < tooHigh) && (computersNumber > playersNumber))
{
tooHigh = computersNumber;
}
}
} while (computersNumber != playersNumber);
cout << "Well then it has to be " << computersNumber << "\n";
cout << "Only took me " << ++tries << " goes!\n\n";
do
{
cout << "Would you like to play again? ('y'/'n'): ";
cin >> playAgainYes;
if (playAgainYes == 'y')
{
playAgain = true;
cout << playAgainYes << "DEBUG\n";
}
else if (playAgainYes == 'n')
{
playAgain = false;
cout << playAgainYes << "DEBUG\n";
}
else
{
cout << "Inncorrect input\n";
cout << "Select 'y' or 'n': ";
}
} while ((playAgainYes != 'y') && (playAgainYes != 'n'));
} while (playAgainYes == 'y');
break;
case exitMenu:
exitGame = true;
break;
default:
cout << "Invalid selection. Please try again.\n";
cout << "1 - Player Versus Computer\n";
cout << "2 - Computer Versus Player\n";
cout << "3 - Exit\n";
cout << "Select a game mode: ";
break;
}
} while (exitGame != true); //plan on implementing an escape feature to the program
system("pause");
return 0;
}