Hey, welcome!

O.K. lets start. Yes, lets start MS VC++ 5or 6!
Now you should see the folowing on your screen:

Thats the "developing-enviroment". Cool, huh?

Now I will explain you how to work with this IDE with an example.
We will code the higher-lower-game, you know it: the computer guesses a number and you have to think of that number and the computer only says: "your guessed number was higher/lower"....blablabla...

Now, you will see 4 tabs on the left hand side: "ClassView", "ResourceView", "FileView" and "Info-(guess what)View". We will work only with "ResourceView", so select it.

>Open the "dialog" - Folder, you will see a node with the name: "IDD_GUESS_DIALOG".
This is the dialog created for you, yes only for you (you actually have paid enough for this proggie, so it can do a few things for you ;-)!
>Double click this node and you will see the dialog which will be the main dialog, the only, holy one dialog the dialog of freedom and peace and love all over the world! No! this was only a joke ;-).
>Play a little bit with this dialog, stretch em and move the buttons around, but DONT DELETE them! If you think, your dialog should be tested, click on the little gray trigger on the left-hand-side below the tabs and you will see, how your dialog looks like, if it is shown by your proggie.
>Now, right click the dialog and select the
"properties"-option. Play a little bit with this options (and the options of the buttons too) to see, how the layout changes, but DONT CHANGE their ID!

Now it is time to take a look in the project-dir (this directory in which the source files are). You will see, that there are ALOT of files, they are too only created for you and your program, so DONT CHANGE them manually! This files are all needed for your programm!

Before, I said that you must not change the ID of the dialog. I will explain a little bit about ID's. Each dialog and dialog-object and each other resource (things, which are added, but not the source-code. The icon is a resource too!) has a unique number which is represented by the ID. Look into the "Resource.h" file and you will see the following code:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by GUESS.RC
//
#define IDR_MAINFRAME		128
#define IDD_GUESS_DIALOG	102
Look at the line with "#define IDD_GUESS_DIALOG 102". What you can see here is the unique number of the dialog.
ID's are used to identify the objects of a dialog.
 

This is the best time to start with the program! Design a dialog, that looks like this (delete the OK-Button and the static-caption with the "todo..."-text):

A short description to each number:

  1. This is a new button with the ID: "IDC_NEW_BUTTON" (you have to name it!). The Caption is "New game"
  2. This was the Cancel-Button. Leave the ID, change only the caption to "Give up"
  3. A static caption. Static means, that this caption never will be changed, so change only the caption to "Guess a number between 0 and 100". The ID is "IDC_STATIC", dont change it!
  4. This is a new button with the caption "Guess". ID: "IDC_GUESS_BUTTON"
  5. A new edit-field with the ID: "IDC_NUMBER_EDIT". Change to the "Styles" tab and select the option "Number" (so you can enter only numbers into the edit-field.
  6. Caption: "Wrong tries:". ID: "IDC_WRONG_CAPTION"

Now I will explain what we need for the programm:

  1. If the user clicks on "New game", a new game will start and the computer has to guess a number between 0 and 100.
  2. If the user clicks "Give up" the programm will exit and we wont have to do any extra-coding ("IDCANCEL" says the dialog to end).
  3. Will stay as it is!
  4. If the user clicks "Guess" the computer compares the right number with our entered number (in 5) and increments the counter for the wrong tries if the number wasn't the guessed number. Furthermore we will say with a Messagebox if the number was higher or lower. If the number is right, we will display a congratulations-box and reinit the game.

What we will do first:
>Init all the procedures and variables. We have to make "member variables". These variables are the data-store for our edit-field and the "Wrong tries:"-field. In the meber variable of the data-field, the guessed number will be stored.
You have to link a editable control with a variable. This variable is called "member variable".

Select the "IDC_WRONG_CAPTION" and link it with the variable "m_wrong_caption". This time you have to change the "Variable type" to "CString".

You linked one control with a "CString" variable. Click on "help->Search..." and enter "CString" to learn more about these variables. DO IT NOW!!!

Finished reading something about CString variables? They're cool, huh?

At this point I can say, that it is almost done, we have to say what routines should be called if the user presses a button and code the game!

Lets specify the "new game"- routine:

You now should see this:

The red arrow points to our function.
Before we can add some code we have to specify global variables.
>scroll to the top of the file and add the following code(after the last "#endif"):

int guessed_computer; //this is the number "guessed" by the computer
int wrong_tries; //number of wrong tries

Scroll back to the "OnNewGame()"-function.
>delete the comment.
>Write a routine that genrrates a random number and stores it in
"computer_guessed". Here is my sample routine (had to include "stdlib.h" and "time.h"):
 
//initialisation routine
srand( (unsigned)time( NULL ) );
guessed_computer = rand( ) % 100;

>Now we have to add a routine which sets the wrong guesses to zero and displays this.

//set the wrong guesses to zero

wrong_tries = 0; //sets the variable to zero

UpdateData( TRUE ); //specifies that all member-variables
//have to take their value from their
//controls

m_wrong_caption = "Wrong tries: 0";

UpdateData( FALSE );//specifies that all controls have to
//take their value from their member-
//variables.

 

This routine is ready!

Dont forget to save often!

Now we have to make the guess-routine!
Lets go on...

Now, add a "member function" called "OnGuess" to the button "Guess" (this button with the ID "IDC_GUESS_BUTTON").
Here is the solution, if cant do it yourself:

Yeah! we have to write the last two routines:
*Check if the right number.
*if not, increment counter and display message.
*if yes, show a "congratulation - message-box".

Here the simple check:


UpdateData( TRUE );
if ( m_guessed_user == guessed_computer )
{
}
else
{
}
UpdateData( FALSE );

And here the routine if the two numbers are equal:

MessageBox("Yeah! You guessed right!", "WINNER!", MB_OK);

CGuessDlg::OnNewGame( ); //restart the game and do,
//as if the user had clicked
//on the "new gane"-button

If you aren't understanding some functions, look at their definition (you can select any expression and press F1 to get help!)

Here is the routine for the false guess:

char *buffer;
CString dummy = (m_guessed_user < guessed_computer) ? "lower" : "higher";
MessageBox("Your number is " +dummy+ " than that one I guessed!", "NONO!", MB_OK);
_itoa( ++wrong_tries, buffer, 10 ); //some "int to string"-conversion
dummy = buffer; //can't add a char* to a string, so we have to save the string in a CString
m_wrong_caption = "Wrong tries: "+dummy;
 
Your game is ready(only some finetuning is needed)!
Click on "Layout->TabOrder...". Now select the controls in the order I numbered them before.
>If you want to execute your game, click on "Execute guess.exe", you have to answer the following question with yes and you can test your game!
>If you want to distribute your exe, click on "Build->Set Active Configuration" double click "release". Push F7 and the exe is built in the "release"-dir.
I have included the exe-file, so you can see, how the program looks like, if it works...

Here is an example, how correct code has to look like ;-)

This was my little tutorial, hope you enjoyed it!
Write me if you have any suggestions or questions.
Donnations please to Bill Gates (he will need it, if the lawyers are finished with him hehehe ;-).

GoFnoR@gmx.at