Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Guess my number game with random numbers - feedback pls

Here are some projects of actual programs you can practice making.

Guess my number game with random numbers - feedback pls

Postby michaelhussar on Thu Oct 08, 2009 12:01 am

I set it up to have user selected ranges for random numbers
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;
}
michaelhussar
 
Posts: 3
Joined: Mon Oct 05, 2009 12:01 am

Re: Guess my number game with random numbers - feedback pls

Postby noobgrammer on Thu Oct 08, 2009 10:14 am

Alright I can tell that this was a just write some code and hope for the best
where all probably guilty of that in the beginning

So first I will say slap yourself for using a goto that is a programmer sin. Those are only used when poor design is done with too much to lose at starting over. I'm just teasing you :D but they are bad to use.
Ok now that we got that over with all I will say is start over with a plan. I will point out some of the problems to help you get a better design going I'm not sure if you understand the else's that well in the first part of the code


Code: Select all
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;
        }


that else is pointless. It doesn't do anything accept reinitialize it to false.

Your rand for one I do not know how it gives me a different number each time since you are not seeding it. It does however give me the same numbers each time I start the program. also you put redundant numbers inside the rand that contradict them self's so in the end nothing happens. Leave out those numbers.


Code: Select all
return rand() % (HiRange - 1 + 1) + 1;

is the same as

return rand() % (HiRange) + 1;


To seed your random put this at the top of main it only has to be done once

Code: Select all
//include this header necessary for the time
#include <ctime>

srand((unsigned)time(0));//top of main


also you have most of your code inside a else statement which technically is not wrong it just not a standard way of programming. there are ways around it.

you also have a mystery number :?: in here and a bool check I'm not sure what they do. You are also not resetting some of your bools so once their changed they stay that way through the games life.


Code: Select all
if ((bolLastOver100 == true) && (intPlayerNumber > 6) )// mystery number 6, bolLastOver100 never gets reset


You have your code checking every single time through for play again if

Code: Select all
if ((strPlayAgain != "y") && (strPlayAgain != "Y"))


to fix this and few other problems . Inside your win if put another if that checks if play again is true if true reinitialize everything inside of that you probably could then just stick a continue in there. that will take you strait back to the top of the loop. and if your play again check fails you put a break in your else. That way you're not getting another random number if you didn't want to play again.

Code: Select all
if (intPlayerNumber == intTheNumber)
            {
                cout <<"YOU WON!!!\n";
               
                cout <<"Play Again? (enter 'y' to continue) \n";
                cin>> strPlayAgain;
               
                system("cls");
            if (strPlayAgain == 'Y' || strPlayAgain == 'y')
            {
               // reset everything
               continue;
            }
            else
               break;
            }


now that way you will go back to the top of your loop with bolFirstTime set back to true and can choose a new range if necessary

Probably more than you expected as a reply
Let me know if any of that doesn't make sence
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Guess my number game with random numbers - feedback pls

Postby michaelhussar on Sat Oct 24, 2009 2:46 pm

Hey noobgrammer,

Actually, I really appreciated the explicit feed back. It a breath of fresh air to have some really provide a full explanation.
I will have to go through these comment slowly and digest all the information to learn from what you suggested. You helped me with some of the difficulties I had during by google search looking for answers. Especially the random number question. I knew that seeding was needed but I could find the proper way of doing it.

Again, thank you very much.
michaelhussar
 
Posts: 3
Joined: Mon Oct 05, 2009 12:01 am


Return to Projects

Who is online

Users browsing this forum: No registered users and 0 guests