1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

cmd based guessing game (C++)

Discussion in 'Game Programming' started by Systemerror, Dec 29, 2008.

  1. Systemerror

    Systemerror New Member

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    void Start ();
    void GetResults ();
    
    int  input, correctAnswer, lifesLeft, maxGuesses;
    char caseTest;
    
    
    void
    Start ()
    {
         input = 0;
         correctAnswer = 0;
         lifesLeft = 0;
         maxGuesses = 6;
    
         cout << "\n------------------------------------------------------------------------------\n";
         cout << "Select difficulty mode, You are restricted to five\nlifes on any difficulty.\n"; // the user has to select a difficutly level
         cout << "1 = 10 numbers to choose from  (easy) \n";
         cout << "2 = 15 numbers to choose from  (intermediate) \n";
         cout << "3 = 25 numbers to choose from  (master) \n";
         cout << "Anything other than 1, 2 or three will end the program!\n";
         cout << "\n------------------------------------------------------------------------------\n";
    
         caseTest = 15;
    
         cin >> caseTest;                   // read the user's choice
         cout << "\n";
    
         switch (caseTest)
         {
            case '1' : maxGuesses = 10;  // the random number will be between 0 and max
             cout << "Easy?, you're soft!  ):\n\n";
            break;
            case '2' : maxGuesses = 15;
            cout << "Ohh... starting to get brave are we?\n\n";
            break;
            case '3' : maxGuesses = 25;
            cout << "So you have a master brain huh?, we'll see...\n\n";
            break;
            default : exit(0);
            break;
         }
    
         lifesLeft = 5;         // number of lifes
         srand( (unsigned)time( NULL ) ); // init Rand() function
         correctAnswer = rand() % maxGuesses;  //  random value between 0 and maxguesses
    
         GetResults();
    
    }
    
    
    void
    GetResults ()
    {
         if (lifesLeft <= 0)
            // if player has no more life then he lose ZOMG^^
         {
            cout << "You lose punk!, muhahahahaha.\n\n";
            Start();
         }
    
         cout << "Type a number to see if you're correct: \n";
         cin >> input;          // read user's number
    
         if ((input>maxGuesses) || (input<0)) // if the user number isn't correct, restart
         {
            cout << "\nError>> Enter a number that's\nwithin the specified range!\n";
            GetResults();
         }
    
         if (input == correctAnswer)
         {
            cout << "You've Done it! congrats.\n\n"; // the user found the secret number
            Start();
         }
    
         else if (input > correctAnswer)
         {
            cout << "Your answer was higher than the correct one\n";
            lifesLeft = lifesLeft - 1;    // -1 to the user's life
            cout << "Number of remaining life(s) are:\n" << lifesLeft << "\n\n";
            GetResults();
         }
    
         else if (input < correctAnswer)
         {
            cout << "Your guess was smaller than the correct one\n";
            lifesLeft = lifesLeft - 1;
            cout << "Number of remaining life(s) are:\n" << lifesLeft << "\n\n";
            GetResults();
         }
    }
    
    
    int
    main ()
        {
         cout << "                                                                         ";
         cout << "                                                                         ";
         cout << "\n###########################   GuessWhat?    #########################\n";
         cout << "\nWelcome to this lame guessing gamer\n";
         cout << "You have to guess the number i'm thinking of (would you believe)\n";
         cout << "You have a maximum of 5 guesses, so choose carefully!\n";
         cout << "######################################################################\n";
         Start();
         return 0;
    }
    
    
     
  2. Xintruder

    Xintruder New Member

    Hey,
    Thanks for the code. If I compile this program under linux would it work?
    Also, can you do us the pleasure and explain the code?
    Starting with

    That would be very useful :)
     
  3. Systemerror

    Systemerror New Member

    Sure, but before I continue I would like to let you know that I have some good 3D games I am working on, with good graphics, physics and AI to which I'll post here soon.

    Ok for your first question, yes it will compile and run under linux as the code doesn't use any platform depedant libs etc, thus making if platform independant - all the includes etc are part of the standard C++ language and independant to the OS it's self :) I love portable code.

    Secondly, I will try to briefly explain the code to you but I am usually not good at explaining things but doing them; ahh well i'll give it a try (but I hope you know a little c++)...,

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    Here, all we are doing is including the relevant resources into the source to allow that specific functionality within it, in this case we're including the iostream, that is the Input/Output stream header to deal with console based applications to read and write data,
    the standard C++ lib, and the time header to give us time functionality - the reason for this is because the srand(); function isn't actually truly random, however when mixing it with time functionalty it is.

    Code:
    using namespace std;
    We declare we are using the namespace std prefix so you dont have to type std:://call every time.

    Code:
    void Start ();
    void GetResults ();
    
    Creating an instance if these two functions.

    Code:
    int  input, correctAnswer, lifesLeft, maxGuesses;
    char caseTest;
    
    Global variables which we will be using.

    Code:
    void
    Start ()
    {
         input = 0;
         correctAnswer = 0;
         lifesLeft = 0;
         maxGuesses = 6;
    
         cout << "\n------------------------------------------------------------------------------\n";
         cout << "Select difficulty mode, You are restricted to five\nlifes on any difficulty.\n"; // the user has to select a difficutly level
         cout << "1 = 10 numbers to choose from  (easy) \n";
         cout << "2 = 15 numbers to choose from  (intermediate) \n";
         cout << "3 = 25 numbers to choose from  (master) \n";
         cout << "Anything other than 1, 2 or three will end the program!\n";
         cout << "\n------------------------------------------------------------------------------\n";
    
         caseTest = 15;
    
         cin >> caseTest;                   // read the user's choice
         cout << "\n";
    
         switch (caseTest)
         {
            case '1' : maxGuesses = 10;  // the random number will be between 0 and max
             cout << "Easy?, you're soft!  ):\n\n";
            break;
            case '2' : maxGuesses = 15;
            cout << "Ohh... starting to get brave are we?\n\n";
            break;
            case '3' : maxGuesses = 25;
            cout << "So you have a master brain huh?, we'll see...\n\n";
            break;
            default : exit(0);
            break;
         }
    
         lifesLeft = 5;         // number of lifes
         srand( (unsigned)time( NULL ) ); // init Rand() function
         correctAnswer = rand() % maxGuesses;  //  random value between 0 and maxguesses
    
         GetResults();
    
    }
    

    This is defining the start() function, that is giving the function instructions on what is should do when it's called..., I can't be bothered to explain every detail of the function - but with a little C++ knowledge you'll understand it.

    Code:
    void
    GetResults ()
    {
         if (lifesLeft <= 0)
            // if player has no more life then he lose ZOMG^^
         {
            cout << "You lose punk!, muhahahahaha.\n\n";
            Start();
         }
    
         cout << "Type a number to see if you're correct: \n";
         cin >> input;          // read user's number
    
         if ((input>maxGuesses) || (input<0)) // if the user number isn't correct, restart
         {
            cout << "\nError>> Enter a number that's\nwithin the specified range!\n";
            GetResults();
         }
    
         if (input == correctAnswer)
         {
            cout << "You've Done it! congrats.\n\n"; // the user found the secret number
            Start();
         }
    
         else if (input > correctAnswer)
         {
            cout << "Your answer was higher than the correct one\n";
            lifesLeft = lifesLeft - 1;    // -1 to the user's life
            cout << "Number of remaining life(s) are:\n" << lifesLeft << "\n\n";
            GetResults();
         }
    
         else if (input < correctAnswer)
         {
            cout << "Your guess was smaller than the correct one\n";
            lifesLeft = lifesLeft - 1;
            cout << "Number of remaining life(s) are:\n" << lifesLeft << "\n\n";
            GetResults();
         }
    }
    
    
    
    Again defining another function, which will later be called.

    Code:
    int
    main ()
        {
         cout << "                                                                         ";
         cout << "                                                                         ";
         cout << "\n###########################   GuessWhat?    #########################\n";
         cout << "\nWelcome to this lame guessing gamer\n";
         cout << "You have to guess the number i'm thinking of (would you believe)\n";
         cout << "You have a maximum of 5 guesses, so choose carefully!\n";
         cout << "######################################################################\n";
         Start();
         return 0;
    }
    
    
    Here is the main function, that is, the entry point of the program where other functions are called.

    I hope I explained this ok, and hope you appreciate it :p
     
  4. hanleyhansen

    hanleyhansen New Member

    Great post!! Looking forward to your 3D games!!
     
  5. skp819

    skp819 New Member

    That is interesting. Thanks for sharing it.
     
  6. shabbir

    shabbir Administrator Staff Member

  7. phone00x

    phone00x New Member

    hello friend's
    i'am newbie.....
    thank's for you code. It's nice
     
  8. TriG0rZ

    TriG0rZ New Member

    this is awesome man thanks for this
     
  9. LenoxFinlay

    LenoxFinlay Banned

    How do i write a multiplayer number guessing game in C language?
     
  10. shipra123

    shipra123 New Member

    Hi,
    This appears to be very useful. Thanks.
     
  11. stiff

    stiff New Member

    hi
    this is nice man and also wonderful
     

Share This Page