37
Posted February 5, 2011 by Spyros in C/C++ Programming
 
 

How to Write a Simple C++ Game in 15 Minutes

adder
adder

One of the things that have always fascinated me in programming is coding a game. I believe that many programmers take up C++ in order to create their first game. Although there are lots of different things that you need to be knowledgeable of to create a 2d or 3d game, you could actually be writing a small but entertaining c++ game in just a matter of minutes or hours.

In this tutorial, i will be showing you how to create a small console based game. It is a mastermind variant where you are given a number and you are then asked to guess whether the next number is going to be higher or lower. I actually created this game in about 10-15 minutes, so it is actually feasible to write such a small game in a really short period of time. And it is also quite entertaining i must say :)

#include <iostream>
#include <time.h>

using namespace std;

int main()
{
    cout << "This is a very simple number guessing game. Each time you will be given a number of the range 0-10." << endl;
    cout << "The objective of the game is to guess whether the next number is going to be higher or not. As simple as that." << endl;
    cout << "You have the ability to make no more than 3 mistakes before you lose, so guess wisely." << endl << endl;
    cout << "You are starting with number 5. Is the next number higher(write H) or lower(write L) ?" << endl;

    int mistakes = 0;
    int correctGuesses;
    int prevNum = 5, nextNum;
    char choice;

    do
    {
        srand ( time(NULL) );
        do
            nextNum = rand() % 11;
        while (nextNum == prevNum);

        cin >> choice;

        if (choice == 'H')
        {
            if (prevNum < nextNum)
            {
                cout << "Correct ! The new number is " << nextNum << endl;
                correctGuesses++;
            }
            else if (prevNum > nextNum)
            {
                cout << "Wrong, you made a mistake ! The new number is " << nextNum << endl;
                mistakes++;
            }
        }

        if (choice == 'L')
        {
            if (prevNum > nextNum)
            {
                cout << "Correct ! The new number is " << nextNum << endl;
                correctGuesses++;
            }
            else if (prevNum < nextNum)
            {
                cout << "Wrong, you made a mistake ! The new number is " << nextNum << endl;
                mistakes++;
            }
        }

        prevNum = nextNum;
    }
    while(mistakes < 3);

    cout << "You've made 3 mistakes ! Game is now over !" << endl;
    cout << "You had " << correctGuesses << " correct guesses before the game was over" << endl;

    return 0;
}

Let’s first execute this and play the game :

This is a very simple number guessing game. Each time you will be given a number of the range 0-10.
The objective of the game is to guess whether the next number is going to be higher or not. As simple as that :)
You have the ability to make no more than 3 mistakes before you lose, so guess wisely.

You are starting with number 5. Is the next number higher(write H) or lower(write L) ?
H
Correct ! The new number is 8
L
Correct ! The new number is 7
L
Correct ! The new number is 6
L
Correct ! The new number is 4
H
Wrong, you made a mistake ! The new number is 3
L
Correct ! The new number is 1
H
Correct ! The new number is 10
L
Correct ! The new number is 7
L
Correct ! The new number is 4
H
Wrong, you made a mistake ! The new number is 2
H
Wrong, you made a mistake ! The new number is 0
You’ve made 3 mistakes ! Game is now over !
You had 8 correct guesses before the game was over

This game is pretty easy to create as you can see. First of all, we declare the important variables that help us preserve the state of the game. This way, we know how many mistakes the user made and how many correct guesses they had. We utilize a do – while loop to start asking the user for H or L. Then, there is also another do – while that is used to pick a random number ranged from 0 to 10. We use this loop so that we are sure that the next number which is randomly selected is not the same as the previous one. Notice that “rand() % 11″ picks our number. % is the mod calculation, which provides us with the remainder of the equation “randomValue / 11″, which is of course a value ranging from 0 to 10.

Then, after we pick the next number, we start checking against this new value and the previous one, based on what the user provided us with as input ( H for higher, L for lower). After the checking is over, we increment the correct or wrong counter and make the current nextNum the new prevNum that is used for the next iteration.

The do – while loop keeps going on till 3 mistakes are made. Of course, you can change that, or better yet declare it to be a constant value. I kept it simple in this example. In the end, we get a report of our ingame progress.

Wasn’t this game development interesting and easy ? Of course, you could take it even further by designing a statistics system that will trace and store your answers, keep highscores and more. I would actually like to get a copy of the program where highscores are included, so if anybody has the will to practice on that and develops it, please send it to me. I will definately publish it here.


Spyros