Page 1 of 212> Show 40 post(s) from this thread on one page

Go4Expert (http://www.go4expert.com/)
-   Game Programming (http://www.go4expert.com/articles/game-programming/)
-   -   cmd based guessing game (C++) (http://www.go4expert.com/articles/cmd-based-guessing-game-cpp-t15668/)

 Systemerror 30Dec2008 03:24

cmd based guessing game (C++)

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; }```

 Xintruder 30Dec2008 06:09

Re: cmd based guessing game (C++)

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

Quote:
 #include #include #include
That would be very useful :)

 Systemerror 30Dec2008 06:49

Re: cmd based guessing game (C++)

Quote:
 Originally Posted by Xintruder (Post 40814) 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 :)
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

 hanleyhansen 31Dec2008 00:33

Re: cmd based guessing game (C++)

Great post!! Looking forward to your 3D games!!

 skp819 6Jan2009 10:58

Re: cmd based guessing game (C++)

That is interesting. Thanks for sharing it.

 shabbir 6Jan2009 12:24

Re: cmd based guessing game (C++)

 phone00x 12Jan2009 11:39

Re: cmd based guessing game (C++)

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

 TriG0rZ 12Jan2009 19:16

Re: cmd based guessing game (C++)

this is awesome man thanks for this

 LenoxFinlay 27Jun2009 12:53

Re: cmd based guessing game (C++)

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

 shipra123 26Oct2009 18:41

Re: cmd based guessing game (C++)

Hi,
This appears to be very useful. Thanks.

 All times are GMT +5.5. The time now is 08:41. Page 1 of 212> Show 40 post(s) from this thread on one page