Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int genRandMax(int nMax)
- // Generates an integer between 0 and nMax;
- {
- return rand() % nMax;
- }
- int main()
- {
- srand(time(0));
- bool bRunning = true;
- // Difficulty
- const int nEasyMax = 100;
- const int nMedMax = 500;
- const int nHardMax = 1000;
- while (bRunning)
- {
- int nRandNumber;
- int nPlayerDifficultyChoice;
- const int *nCurrentDifficulty;
- cout << "Please choose your difficulty \n1) Easy (0 - " << nEasyMax << ") \n2) Medium (0 - " << nMedMax << ") \n3) Hard (0 - " << nHardMax << ")" << endl;
- cin >> nPlayerDifficultyChoice;
- cout << endl;
- switch (nPlayerDifficultyChoice) {
- case(1): {
- nCurrentDifficulty = &nEasyMax;
- break;
- }
- case(2): {
- nCurrentDifficulty = &nMedMax;
- break;
- }
- case(3): {
- nCurrentDifficulty = &nHardMax;
- break;
- }
- default: {
- nCurrentDifficulty = &nEasyMax;
- break;
- }
- }
- nRandNumber = genRandMax(*nCurrentDifficulty);
- bool bWon = false;
- int nPlayerGuess = 0;
- while (!bWon)
- {
- //cout << "The number is " << nRandNumber << endl;
- cout << "Guess an integer between 0 and " << *nCurrentDifficulty << endl;
- cin >> nPlayerGuess;
- cout << endl;
- if (nPlayerGuess > nRandNumber) {
- cout << "Nope, too high" << endl;
- } else if (nPlayerGuess < nRandNumber) {
- cout << "Nope, too low" << endl;
- } else if (nPlayerGuess == nRandNumber) {
- cout << "You got it!" << endl;
- bWon = true;
- } else {
- cout << "Something went wrong :(" << endl;
- exit(-1);
- }
- }
- char sContinueResponse = 'Y';
- cout << "Would you like to play again? (Y/N)" << endl;
- cin >> sContinueResponse;
- cout << endl;
- if (toupper(sContinueResponse) == 'Y') {
- bRunning = true;
- } else if (toupper(sContinueResponse) == 'N') {
- bRunning = false;
- } else {
- exit(-2);
- }
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement