C++Game DevelopmentIntroduction to C++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 https://www.embarcadero.com/free-tools/dev-cpp/free-download.

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,

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,

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.

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,

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.

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

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

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

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.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
Artificial Intelligence TechC++C++11C++14C++17Learn C++

Why You Should Know ELU Artificial Neural Net Functions

Artificial Intelligence TechC++C++11C++14C++17Learn C++

What You Need To Know About C++ Gaussian Error Linear Units

C++C++11C++14C++17Learn C++

What Is The SoftPlus Activation Function in C++ Neural Nets?

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Make AI Binary/Heaviside Step Functions In C++

en_USEnglish