Site icon Learn C++

Dev-C++ Tutorial: Learn To Develop Very Simple Guessing Game

Embarcadero Dev-C++ is free, and is a fast, portable and simple C/C++ IDE for Windows. The free version is great for beginners. If you want to develop professionally it is highly recommended you start with C++ Builder CE version. Dev-C++ can be downloaded from Embarcadero’s site, Sourceforge, or Github. The original developer is Bloodshed Software.

You can also create some small games in Dev-C++, you may need C++ Builder, VSCode, VS C++ to develop much more professional games. Every beginner should learn to code Guessing Game, which is mostly classic in most of courses. This is very good example to show how user and computer acts as in inputs and outputs with the logic in code lines. If you are new to C++, you can easily start learning C++ with this game.

First let’s describe our game,

Guessing Game: Our game is about to guessing numbers between 1 and 100. In this game the computer ( actually this is your application that you will develop) will choose a random number between 1 and 100, and the player will try to find this number in 10 steps. User should try to do as few attempts as possible. At the beginning our application will hold a random number, and it will ask for it. Each time the player enters a guess, the computer tells user whether the guess is too high, too low, or right, game will over. If user can’t find it in all steps, the game will be over and computer will print the random number that hold.

Let’s start to code,

  1. Create a new Project in Dev-C++
  2. To have a random number we will use rand() function which generates a number between 0 and RAND_MAX. RAND_MAX is maximum number that is a constant defined in <cstdlib>. We can not change constant numbers or we must modify which is not recommended.
    Mathematically, in C or C++ we limit this by using modulo operator with %, for example rand()%100 means random number between 0-99. If we want to hold a number between 1-100 we must add 1 to this, so our random number will be like this,
[crayon-6622b633cd0e9183426193/]

Random numbers are not really numbers, it depend on timer, so each time you run it will hold same number. In some games this is advantage to generate random maps, roads in a same random series, so you can make different levels but each level will have same shaped maps or roads etc.. To initialize random seed we must use this with srand() function as given below,

[crayon-6622b633cd0f0428175739/]

So now we have a number hold.

3. Let’s keep coding with these information. We need these libraries below listed with #include to use our functions.

[crayon-6622b633cd0f2700479250/]

4. Now we can focus on our main program, we will code inside the main() procedure. Let’s add some variables for counting steps and random input, we can add generating a random number hold it as an integer randomnumber,

[crayon-6622b633cd0f3716601828/]

5. We can print out some information texts wit cout command and let’s create our empty loop counting 10 times with { } brackets , finally let it write randomnumber at the end.

[crayon-6622b633cd0f5198007199/]

At this step, you can compile by pressing F9 run your code by pressing F10, or you can use Compile and Run buttons in Dev-C++ editor. It should hold a random number, display information and then it will show the number that it hold.

6. Now we will continue to write codes inside our for() loop, go to //guessing line, delete it and add these to get input with cin command from user

[crayon-6622b633cd0f8355882479/]

7. Now we have a user input, we need a logic to decide if it is higher or lower or equal to random number. To do this we should use if () … else if() … else() serie as below

[crayon-6622b633cd0fa598108293/]

8. Finally, let’s modify this logic and whole code should be as below,

[crayon-6622b633cd0fc144565588/]

Note that last else has two lines and it exits from the program, so we put these lines in { …. } brackets to define lines of else group.

9. You can compile by pressing F9 run your code by pressing F10 or F11, or you can use Compile and Run buttons in Dev-C++ editor. It should hold a random number, display information and then it will show the number that it hold. Try to guess as few steps as possible.

In this example, there are other ways to do same game with do… while() loop or with while() loop alone. We hope this example helps you to understand some basics and logics of C++ programming. These codes are friendly with some other compilers like GNU C/C++, VS Code, Bcc or CLANG Compiler for the C++ Builder Console (VCL) application.

Exit mobile version