Thread: Game: Computer Guess My Number

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    61

    Game: Computer Guess My Number

    Greetings EeveryOne

    You all know "Guess My Number" game when the computer chose a secret number and the player tries to guess it. This is my attempt to do the reverse as the player chose a secret number and the computer will try to guess it.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    
    #include <limits>
    
    #define FlushInput \
    cin.clear();\
    std::cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
    
    using namespace std;
    
    string MyToLower(string str);
    
    int main()
    {
        int SecretNumb = 0;
        int Tries      = 0;
        int Guess      = 0;
        int MinRange   = 0;
        int MaxRange   = 100;
        string Input   = "";
        string Hint    = "";
    
        bool WrongInput = true;
    
        // Seed random number generator;
        srand(static_cast<unsigned int>(time(0)));
    
        cout << "\t\tComputer Guess My Number\n\n"
             << "Chose a number and the computer will try to guess it.\n"
             << "After each guess type either:\n\n"
             << "*too high: if the number guessed is higher than your secret number.\n\n"
             << "*too low : if the number guessed is lower  than your secret number.\n\n"
             << "*right   : if the number guessed is the same as your secret number.\n\n"
             << "\t------------------------------------------\n"
             << endl;
    
        do
        {
            cout << "Please Enter A Secret Number "
                 << "between 1 and 100: ";
            cin  >> Input;
            FlushInput;
    
            SecretNumb = atoi(Input.c_str());
    
            if(isalpha(Input[0])        ||
               SecretNumb < MinRange    ||
               SecretNumb > MaxRange      )
            {
                cout << "Wrong Input.\n";
            }
        }while(isalpha(Input[0])        ||
               SecretNumb < MinRange    ||
               SecretNumb > MaxRange      );
    
        do
        {
            //random between Max & Min inclusive:
            //(ran()%(Max-Min+1))+Mix
            Guess = (rand()%(MaxRange-MinRange+1))+
                    (MinRange);
    
            cout << "My Guess Is: " << Guess
                 << endl;
    
            WrongInput = true;
            while(WrongInput)
            {
                WrongInput = false;
    
                cout << "Human's Hint: ";
                getline(cin, Hint);
                cout << endl;
    
                if(MyToLower(Hint) == "too high")
                {
                    if((Guess==SecretNumb))
                        cout << "Did You Forgot Your Secret Number,\nThe PC Already Guessed It!"
                             << endl;
                    else MaxRange = Guess-1;
                }
                else
                if(MyToLower(Hint) == "too low")
                {
                    if((Guess==SecretNumb))
                        cout << "Did You Forgot Your Secret Number,\nThe PC Already Guessed It!"
                             << endl;
                    else MinRange = Guess+1;
                }
                else
                if(MyToLower(Hint) == "right")
                {
                    Guess = SecretNumb;
                }
                else
                {
                    cout << "Wrong Input." << endl;
                    WrongInput = true;
                }
            }
    
            ++Tries;
    
            cout << "-----------------\n";
            cout << "MaxRange = " << MaxRange << "\n";
            cout << "MinRange = " << MinRange << "\n";
            cout << "-----------------\n";
            cout << endl;
    
        }while(Guess!=SecretNumb);
    
        cout << "Solved In " << Tries << " Tries."
             << endl;
    
        return 0;
    }
    
    string MyToLower(string str)
    {
        int leng = str.length();
    
        for(int i = 0; i<=leng; i++)
        {
            str[i] = tolower(str[i]);
        }
    
        return str;
    }
    I would like to hear your feedback and comments if any to improve my programming skills
    Last edited by Laythe; 03-31-2012 at 05:08 PM.
    Knowledge Is Power!

    Don't give me a fish but teach me how to catch one

    I am not asking you to code it for me, what i need is guidance so i can learn

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > #define FlushInput \
    Don't write code using macros in C++, just make it a function.
    If you really care about performance (and anything to do with I/O doesn't count), then you can make it an inline function.

    Your main() is over 100 lines long, and is desperately in need of splitting up.
    Something along the lines of
    Code:
    int main ( ) {
      showHelp();
      secret = getUserSecret();
      computerGuesses(secret);
    }
    > if(isalpha(Input[0])
    You should probably do this before trying to do atoi() on the string.
    Also, there are better C++ ways of converting a string to an integer (look at >> from a stringstream for example)

    But definitely +1 for your indentation skills!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guess number game (simple)
    By Fotis in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2011, 05:00 PM
  2. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  3. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  4. Guess my Number game! check it out
    By Yosif in forum Game Programming
    Replies: 1
    Last Post: 06-09-2007, 09:43 PM
  5. Guess the number game
    By Ninestar in forum C Programming
    Replies: 12
    Last Post: 12-08-2005, 11:30 AM