Sign in to follow this  
xtremeprog05

My first game (Guess my Number)

Recommended Posts

xtremeprog05    154
I just wanted to post my first game here to see what everyone thinks. So far I havent really found any bugs (except that if you type a character instead of a number the loop goes on forever saying: "Guess a number: Too high!". I used dev-cpp to make it. If anybody has any ideas for the game I'll take any advice. In future versions i'm going to give the user lives. Well heres the code, if you find anything wrong just let me know thanks
// NumbersXL v1.0
#include <iostream>
#include <ctime>

using namespace std;

void playAgain();

int main()
{
    srand(time(0));
    
    int theNumber = rand() % 100 + 1;
    int guess;
    
    cout << "\tWelcome to NumbersXL!\n";
    
    do{
         cout << "Guess a number: ";
         cin >> guess;
         cin.ignore();
         
         if ( guess < theNumber )
         {
              cout << "Too low!\n";
         }
         if ( guess > theNumber )
         {
              cout << "Too high!\n";
         }
         if ( guess == theNumber )
         {
              cout << "Correct! The number was "<< theNumber <<".\n";
              playAgain();
         }
    }while ( guess != theNumber );
    
    
    return 0;
}

void playAgain()
{
     char yesOrNo;
     
     cout << "\n\nWould you like to play again? (y,n)\n";
     cin >> yesOrNo;
     cin.ignore();
     
     if ( yesOrNo == 'y' )
     {
          cout << "\n";
          main();
     }
     else if ( yesOrNo == 'n' )
     {
          cout << "Thank You for playing!\n\n";
          
          cout << "/////////////////////////////////////////\n";
          cout << "// ---------Made by CE Medias--------- //\n";
          cout << "/////////////////////////////////////////\n";
          
          cout << "\nGo to www.cemedias.tk for more\n";
          cout << "information on games and updates.\n\n";
          
          system("pause");
          
          exit( EXIT_SUCCESS );
     }
     else
         cout << "ERROR: Invalid input!\nGame will now close.";
}

My Site

Share this post


Link to post
Share on other sites
Ra    1062
You should edit your post and put the code in [source][/source] tags to preserve formatting.

Share this post


Link to post
Share on other sites
Ra    1062
The only real problem I see with it is this:

playAgain() is called when you have finished a game, which calls main() if you want to play again, which calls playAgain() ... ad infinitum. This could potentially end up overflowing the stack because the return address for each function is being pushed on the stack when you call. It'd be better if you had playAgain() return a bool, true or false, to indicate whether the user wants to play again or not. This could then be hooked up to a loop which would run again if the user wanted to play again.

Example:
int main()
{
do
{
...
}
while (playAgain());

/* display quit message */
...
}

bool playAgain()
{
...

if (yesorno == 'y')
{
return true;
}
else
{
return false;
}
}

Share this post


Link to post
Share on other sites
ApochPiQ    23065
Looks like a great start! The code structure is very clean and consistent.

There's a couple of little things that could be improved on. In the loop in main(), you could change the three if's to a set of if's and else's. This won't really change the behavior of the game at all, but it shows to the reader of your code that the three cases are all mutually exclusive, and it is a good habit to get into:

if ( guess < theNumber )
{
cout << "Too low!\n";
}
else if ( guess > theNumber )
{
cout << "Too high!\n";
}
else if ( guess == theNumber )
{
cout << "Correct! The number was " << theNumber << ".\n";
playAgain();
}



The other thing is that the way your loop is structured is a bit dangerous - it's something called unbounded recursion. By simply calling main() from playAgain(), you never actually exit from main() or playAgain() as long as the player keeps going. In a severe case this could lead to a crash when the program runs out of memory. Unbounded recursion can happen in many places, so it's a good skill to be able to recognize it and avoid it even in simple code like this game.

I would suggest a small tweak to the loop. First, have playAgain() be a bool function. If the player types a y, the function returns true. If the player types an n, the function returns false. You can then set up playAgain() to loop until either a y or n is typed.

Once this is done, change the loop in main() from while(guess != theNumber) to while(!playAgain()). This way, when n is typed in playAgain, the game will exit from main(). This also makes sure that playAgain() will completely exit and not chew up memory. Basically it fixes the unbounded recursion problem.


Anyways, you're off to a good start it looks like... maybe a good thing to try would be adding a feature that limits the number of guesses a player is allowed to make (i.e. make it so I can lose). Keep it up!


[edit] I type too slow - Ra beat me to all the good stuff.

Share this post


Link to post
Share on other sites
xtremeprog05    154
I changed the program like you said, but when I run it: if i get too high or too low it automatically sends me to playAgain(). Why? I'm sure it is something simple that I just overlooked.

here is my code:





// NumbersXL v1.0
#include <iostream>
#include <ctime>

using namespace std;

bool playAgain();

int main()
{
srand(time(0));

int theNumber = rand() % 100 + 1;
int guess;

cout << "\tWelcome to NumbersXL!\n";

do{
cout << "Guess a number: ";
cin >> guess;
cin.ignore();

if ( guess < theNumber )
{
cout << "Too low!\n";
}
else if ( guess > theNumber )
{
cout << "Too high!\n";
}
else if( guess == theNumber )
{
cout << "Correct! The number was "<< theNumber <<".\n";
playAgain();
}
}while ( !playAgain() );


return 0;
}

bool playAgain()
{
char yesOrNo;

cout << "\n\nWould you like to play again? (y,n)\n";
cin >> yesOrNo;
cin.ignore();

if ( yesOrNo == 'y' )
{
cout << "\n";
return true;
}
else if ( yesOrNo == 'n' )
{
cout << "Thank You for playing!\n\n";

cout << "/////////////////////////////////////////\n";
cout << "// ---------Made by CE Medias--------- //\n";
cout << "/////////////////////////////////////////\n";

cout << "\nGo to www.cemedias.tk for more\n";
cout << "information on games and updates.\n\n";

system("pause");

return false;
}
else
cout << "ERROR: Invalid input!\nGame will now close.";
return false;
}






My site

Share this post


Link to post
Share on other sites
ApochPiQ    23065
Oops, I left out a step, sorry.

You actually need two loops: one that runs until the game is finished, and one that runs until playAgain() returns false. I would suggest splitting the game loop into a separate function, like so:

int main()
{
do
{
playGame();
} while(playAgain());

return 0;
}

void playGame()
{
// Choose a number here

do
{
// Get guess and check it
} while(guess != answer);
}

bool playAgain()
{
// Keep as-is
}


That should work a little better [wink]

Share this post


Link to post
Share on other sites
xtremeprog05    154
Thanks! Now I have another problem though lol. The program runs smoothly until it asks me to play again. If I choose yes it just asks me again. If I say no it quits like it should.

Here is my code:




// NumbersXL v1.0
#include <iostream>
#include <ctime>

using namespace std;

bool playAgain();
void playGame();

int main()
{
do
{
playGame();
} while ( playAgain() );

return 0;
}


void playGame()
{
srand(time(0));

int theNumber = rand() % 100 + 1;
int guess;

cout << "\tWelcome to NumbersXL!\n";

do{
cout << "Guess a number: ";
cin >> guess;

if ( guess < theNumber )
{
cout << "Too low!\n";
}
else if ( guess > theNumber)
{
cout << "Too high!\n";
}
else if ( guess == theNumber)
{
cout << "Correct! The number was "<< theNumber <<"\n";
playAgain();
}

}while ( guess != theNumber );


}

bool playAgain()
{
char yesOrNo;

cout << "\n\nWould you like to play again? (y,n)\n";
cin >> yesOrNo;
cin.ignore();

if ( yesOrNo == 'y' )
{
cout << "\n";
return true;
}
else if ( yesOrNo == 'n' )
{
cout << "Thank You for playing!\n\n";

cout << "/////////////////////////////////////////\n";
cout << "// ---------Made by CE Medias--------- //\n";
cout << "/////////////////////////////////////////\n";

cout << "\nGo to www.cemedias.tk for more\n";
cout << "information on games and updates.\n\n";

system("pause");

return false;
}
else
cout << "ERROR: Invalid input!\nGame will now close.";
return false;
}





EDIT: Sorry, a couple of minutes ago I gave the wrong source. Now its right though. im still having the same problem...
All help will be appreciated!!!


My site

[Edited by - xtremeprog05 on June 28, 2005 5:55:55 PM]

Share this post


Link to post
Share on other sites
ApochPiQ    23065
You're still calling playAgain() in playGame() when the right number is guessed - taking that out should fix it.

Another thing to watch out for is this:


}
else
cout << "ERROR: Invalid input!\nGame will now close.";
return false;
}


The indentation makes it look like the return false is supposed to be part of the else block, but without braces {} around the two lines, only the cout << line will be called as part of the else block. Again, in this case, it won't cause you any problems - but it's something to be aware of when programming in C/C++ style languages.

Share this post


Link to post
Share on other sites
xtremeprog05    154
Thanks, it all works fine. I figured it would be something simple.

Here is the final code:




// NumbersXL v1.0
#include <iostream>
#include <ctime>

using namespace std;

bool playAgain();
void playGame();

int main()
{
do
{
playGame();
} while ( playAgain() );

return 0;
}


void playGame()
{
srand(time(0));

int theNumber = rand() % 100 + 1;
int guess;

cout << "\tWelcome to NumbersXL!\n";

do{
cout << "Guess a number: ";
cin >> guess;

if ( guess < theNumber )
{
cout << "Too low!\n";
}
else if ( guess > theNumber)
{
cout << "Too high!\n";
}
else if ( guess == theNumber)
{
cout << "Correct! The number was "<< theNumber <<"\n";
}

}while ( guess != theNumber );


}

bool playAgain()
{
char yesOrNo;

cout << "\n\nWould you like to play again? (y,n)\n";
cin >> yesOrNo;
cin.ignore();

if ( yesOrNo == 'y' )
{
cout << "\n";
return true;
}
else if ( yesOrNo == 'n' )
{
cout << "Thank You for playing!\n\n";

cout << "/////////////////////////////////////////\n";
cout << "// ---------Made by CE Medias--------- //\n";
cout << "/////////////////////////////////////////\n";

cout << "\nGo to www.cemedias.tk for more\n";
cout << "information on games and updates.\n\n";

system("pause");

return false;
}
else
{
cout << "ERROR: Invalid input!\nGame will now close.";
return false;
}
}






Thanks for everyones help. maybe in a couple days i'll have a new version of this game posted on the forum but with lives and other things added.


thanks again




My site

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this