Earlier, we set up a separate “else” statement in case of user error. That way the program wouldn’t crash if they accidently typed something other than “R”, “r”, “L” or “l”. The problem with what we did is, while the program wouldn’t hang, it would just end. That seems a little severe for a typo.
It would be better to give the user a second chance, maybe even a third and fourth chance. Actually it’d be nice if the program just looped, constantly prompting the user, until the user figures out exactly what answer to give. And we can do this!
There are three main ways of doing a loop, or an iterative structure. Two of them are based around the while statement, the other is a for statement. The for statement is more of a counter; the while statement will loop as long as a control expression, the part in parenthesis, is true. That is, while this condition is true, do the action in the curly braces. The do while statement does the same thing, except the code in curly braces is run before the condition is checked. That is exactly what we want. We want to prompt the user for the direction, get the variable, then check if it’s right. If it is, we jump out of the loop. If it isn’t, we repeat the loop.
First we need to figure out under what condition(s) do we run the loop? We, if we don’t get an “R”, “r”, “L” or “l” we need to repeat it. A quick truth table will help us figure out the conditions we want. Well, with four possible outcomes it won’t be “quick” exactly…
|
d == R
|
d == r
|
d == L
|
d == l
|
REPEAT
|
d != R
|
d != r
|
d != L
|
d != l
|
REPEAT
|
|
F
|
F
|
F
|
F
|
T
|
T
|
T
|
T
|
T
|
T
|
|
F
|
F
|
F
|
T
|
F
|
T
|
T
|
T
|
F
|
F
|
|
F
|
F
|
T
|
F
|
F
|
T
|
T
|
F
|
T
|
F
|
|
F
|
F
|
T
|
T
|
F
|
T
|
T
|
F
|
F
|
F
|
|
F
|
T
|
F
|
F
|
F
|
T
|
F
|
T
|
T
|
F
|
|
F
|
T
|
F
|
T
|
F
|
T
|
F
|
T
|
F
|
F
|
|
F
|
T
|
T
|
F
|
F
|
T
|
F
|
F
|
T
|
F
|
|
F
|
T
|
T
|
T
|
F
|
T
|
F
|
F
|
F
|
F
|
|
T
|
F
|
F
|
F
|
F
|
F
|
T
|
T
|
T
|
F
|
|
T
|
F
|
F
|
T
|
F
|
F
|
T
|
T
|
F
|
F
|
|
T
|
F
|
T
|
F
|
F
|
F
|
T
|
F
|
T
|
F
|
|
T
|
F
|
T
|
T
|
F
|
F
|
T
|
F
|
F
|
F
|
|
T
|
T
|
F
|
F
|
F
|
F
|
F
|
T
|
T
|
F
|
|
T
|
T
|
F
|
T
|
F
|
F
|
F
|
T
|
F
|
F
|
|
T
|
T
|
T
|
F
|
F
|
F
|
F
|
F
|
T
|
F
|
|
T
|
T
|
T
|
T
|
F
|
F
|
F
|
F
|
F
|
F
|
Looking at this, we can see there are 15 possibilities in which we want to skip out of the loop, but only one time in which we want the loop to be repeated. We can apply a few of our logical conditionals to see which one we need to make this loop work.
|
d == R
|
d == r
|
d == L
|
d == l
|
REPEAT
|
AND
|
!AND
|
AND !=
|
OR
|
!OR
|
|
F
|
F
|
F
|
F
|
T
|
F
|
T
|
T
|
F
|
T
|
|
F
|
F
|
F
|
T
|
F
|
F
|
T
|
F
|
T
|
F
|
|
F
|
F
|
T
|
F
|
F
|
F
|
T
|
F
|
T
|
F
|
|
F
|
F
|
T
|
T
|
F
|
F
|
T
|
F
|
T
|
F
|
|
F
|
T
|
F
|
F
|
F
|
F
|
T
|
F
|
T
|
F
|
|
F
|
T
|
F
|
T
|
F
|
F
|
T
|
F
|
T
|
F
|
|
F
|
T
|
T
|
F
|
F
|
F
|
T
|
F
|
T
|
F
|
|
F
|
T
|
T
|
T
|
F
|
F
|
T
|
F
|
T
|
F
|
|
T
|
F
|
F
|
F
|
F
|
F
|
T
|
F
|
T
|
F
|
|
T
|
F
|
F
|
T
|
F
|
F
|
T
|
F
|
T
|
F
|
|
T
|
F
|
T
|
F
|
F
|
F
|
T
|
F
|
T
|
F
|
|
T
|
F
|
T
|
T
|
F
|
F
|
T
|
F
|
T
|
F
|
|
T
|
T
|
F
|
F
|
F
|
F
|
T
|
F
|
T
|
F
|
|
T
|
T
|
F
|
T
|
F
|
F
|
T
|
F
|
T
|
F
|
|
T
|
T
|
T
|
F
|
F
|
F
|
T
|
F
|
T
|
F
|
|
T
|
T
|
T
|
T
|
F
|
T
|
F
|
F
|
T
|
F
|
Truth tables can be an excellent way of checking logic. They can also be an excellent way of finding quicker ways of doing the same thing. I’m not going to get into optimization now, just keep in mind that working up a quick truth table can be one way to minimize operations.
We can now see there are two ways of doing this properly. The first is ORing each of the four possibilities together and then seeing if looping if the result is false. We can also check to see if each of the possibilities are false, then AND the results. I’m going to use that method because it’s what we’re really doing. If “d” does not equal “R” and “d” does not equal “r” and “d” does not equal “L” and “d” does not equal “l” then we need to prompt for a new answer. That’s probably not ideal, since we’re both negating the answer and checking for OR, but I’ll take the slight performance hit on this. There’s a lot more leeway with these things on a tiny text game than there would be on a huge, complex 3D game, or on airplane controls or medical equipment.
void main()
{
char d;
cout << endl;
cout << endl << "You are in a room.";
cout << endl << "There are two doors, one on the right,";
cout << endl << "one on the left.";
do
{
cout << endl << "Which do you want to do? (R/L) : ";
cin >> d;
} while ( d != 'R' && d != 'r' && d != 'L' && d != 'l' );
if (d == 'R' || d == 'r')
{
cout << endl << "You went through the door on the right";
}
else if (d == 'L' || d == 'l')
{
cout << endl << "You went through the door on the left";
}
cout << endl;
}
So the part in curly braces is run. The user is asked what to do, then is prompted to enter a response. The user does, and we check it. The user has to give a correct answer before the program will continue.
While operates in the same manner, only the condition is checked before the code is run. That won’t work for this example, since our prompt is inside the code. Unless we prompt twice, once outside the code to get the initial response, then in the loop.
a = 0;
while ( a < 100)
{
cout << endl << "a is " << a;
a++;
}
This is probably a bad example of while. First, we declare “int a;” which we set to zero. We check to see if “a” is less than 100. It is, so we print it.
The next part may look a little different. We want to increment a by 1. We could do this by saying “a = a + 1;” but C++ uses a shortcut to this. In fact, notice that “a++” and “C++” are a lot alike. C++ is mostly just the C language, with a few extensions. Therefore we say C++ is one increment past C. Just like here, a++ is a plus 1, or one increment past a.
As a sidebar, C is a descendent of a language called B. There was never an A language, so far as I know. The Java and C# languages borrow heavily from C++, so you could say they’re descendants of C++.
We can also use a decrement, or a–, if we want to subtract one from a.
There are two variations of the increment and decrement: pre- and post-increment. Pre-increment (++a) means the variable is incremented before being used, while post-increment means it is incremented after use.
Anyway, going back to our while loop that counts to 100. The loop is correct, but there’s actually another loop that’s built specifically for counting: the for iterative structure.
for (a=0; a<100; a++)
{
cout << endl << "a is " << a;
}
The for control expression is broken up into three parts. The first is the loop index, or control variable. It sets the variable. The second is the condition. The third is the increment or decrement value.
The increment value doesn’t have to be a “plus 1″ value. You can mix it up however you like. Just mind the conditional. If you run something like “a=0; a==2; a=a+3″ you’ll get an infinite loop. However, the following is perfectly valid.
for (a=0; a<99; a=a+3)
{
cout << endl << "a is " << a;
}
In this case, the statement will start at zero. It will print “a is 0″. Then it will increment by 3 and print that result. It will keep doing that as long as a is less than 99. Note that condition is less than 99; it does not include 99.
So that is how we approach programs logically. We declare variables so the program can manipulate data. We use conditionals and logic operators to give the program choices. We use loops to cycle through data and perform actions, or to stick the user in a sort of limbo until we get the data we need.
We still have some ground to cover before we really dive into building a game. But we could build a game right now. We could set up a while loop (while the user hasn’t “quit”) containing a bunch of if statements navigating through a maze. If you’re into homework, I’d recommend you make a small game using these techniques. Nothing fancy, just two or three rooms.
The rest of this section will cover functions, a way to break the program into small, re-usable parts. After that, we’ll start building a map.
The TBAG entries are where I blog building a Text Based Adventure Game (rather unfortunate acronym that). The project teaches the building blocks of computer science using C++ to build a text game. To view the entire series, click here.