Bold Aesthetic Creative Games

An Indie Game Studio Developing Bold, Aesthetic & Creative Video Games

C++ Programming Series: Using Conditional Statements (Part 5)

C++ Programming Series: Using Conditional Statements (Part 5)

Argghhh! When will the topic of conditional statement get over? Well, we have to be patient while learning something. I hope that you might have done the home task of the previous post. Just keep trying to code things up and try to implement small ideas with your skill, you will see yourself learning and improving!

Below is some similar-looking code that we discussed in the previous post:

#include <iostream>
using namespace std;

int main()
{
    //Guess the number game!
    while(value != 7)
    {
        cout << "Enter the value: ";

        int value;
        cin >>; value;
    }

    cout << endl << "Horray! You guessed the number!";

    return 0;
}

Yeah! This time we programmed a very simple game! Guess the number! Sounds cool… Right? No! Why not? At least, we can call this our first GAME! At least, we started learning game development!

Try to run that code! Is it running fine? No! What the compiler said? Oh! It looks very hard… Somewhere, in the output log, it might be written that:

'value': undeclared identifier

Now, what it means? it means that the variable, value is not known to the compiler. Just see the code. The condition in the while-statement comes before value being declared. The solution can be this:

#include <iostream>
using namespace std;

int main()
{
    //Guess the number game!
    cout << "Enter the value: ";

    int value;
    cin >> value;

    while(value != 7)
    {
        cout << "Enter the value: ";

        //Say No to: int value; Reason: already declared above.
        cin >> value;
    }

    cout << endl << "Horray! You guessed the number!";
    return 0;
}

Even better and a very neat solution is this:

int main()
{
    //Guess the number game!
    int value;
    do {
        cout << "Enter the value: ";
        cin >> value;
    } while(value != 7); //Semicolon is needed here!

    cout << endl << "Horray! You guessed the number!";

    return 0;
}

The code in the do block is first executed even if the condition in the while is not true. Then, it will only be repeated if the condition remains true. Very simple! Also, in this way, the condition will not get the garbage value of uninitialized but declared value as the do code gives some value to value i.e initializes value.

Looping plays a key role in game development. Such a loop in which program updates logic and screen of a game is called game loop. Game loop is the heart of any game. A game loop may look like:

bool quit = false;
while(!quit) //same as "quit == false"
{
    //> > > Polling Events
    //if(quit event happens)
    //   quit = true;
    //else if(key == arrow x)
    //   move player to direction = x;

    //> > > Updating Game Logic
    //if(enemy hits player)
    //   player hp--;
    //else if(player hits enemy)
    //   enemy hp--;

    //> > > Updating Screen
    //clear screen
    //render enemy
    //render player
    //update screen
}

In future, we will get to this point. But for now, this is a minimal view of a general video game.

Let us talk about another home task. This time it is not a joke! Make a program that takes interview of a user having such questions that have only two answers i.e Yes or No. On the basis of Yes or No of a question, change the route of questioning. In this way, make a hierarchy or tree kind of thing. If possible, comment that code here so, that I can review it! Also, try some experiments with the code in this post and understand it. To learn something like programming, it is necessary to be interested in it…

Leave a Reply

Your email address will not be published. Required fields are marked *