OS X games from the ground up: more fun with variables and arrays

You can see an index of all the posts in this series: go to index.

If you are starting from this point, or need a fresh set of files, here are the starter files from the end of the previous post: variables

In the previous post in this series, you saw how to use variables to store information in your programme. You also saw how to use arrays to store a collection of variables. At the end of that post I left you with a challenge. The first part of challenge was to display the phrase “10 green bottles hanging on the wall”, but to store the number 10 and the word ‘green’ in variables. If you managed to complete the exercise successfully, you would have ended up with something like this (of course your variables may have different names to mine):

When you build and run this, you should see the game instructions you added in an earlier post, followed by this:

green bottles exercise 1

The second part of the exercise was to change the phrase to “9 blue bottles hanging on the wall” by changing the values you use to initialise the variables. This will have made your code look something like this:

Which will display:

green bottles exercise 2

Now that we have a reasonable command of variables and arrays, let’s think how these might be useful to us for storing the list of words.

If we refer back to the original BASIC programme, we see code that looks like this:

What’s happening here? Line 90 is actually doing something you’ve already seen in C. It is creating an array with 40 elements. In BASIC, you dimension an array when you create it, so DIM is short for dimension.

A$ is the name of the array. String variables in BASIC always end with $. An important thing to note here is that the name of the array in the original BASIC programme is just a single letter. This was a common practice at the time this programme was written because some BASIC dialects only supported single letter variable names. C suffers no such restriction, so you should always make your variable names as descriptive as possible, while also keeping them succinct. For example, calling a variable s doesn’t really tell you what the variable is for, whereas calling a variable score gives you a good idea what it does.

Lines 200 to 290 contain all the words for the programme. At present these are stored as string literals, but the programme needs to put them into the array for them to be useful. In BASIC, information can be embedded in the programme using the command DATA. When the programme needs to make use of this data it can read it into a variable using the command READ. The first READ command will read the first piece of data stored, the second will read the second and so on.

Line 100 is an example of a loop. This repeats this line of code 39 times, and for each time it repeats, it reads another word into an element of the A$ array. When this loop finishes, all the words embedded in the DATA lines will have been stored in the array A$. You’ll learn more about loops in a future post.

So in C we’ve seen how to create a single string. For example, we could create a string with the first word, “ability”, with the following line of code:

But how do we create an array of strings? We could declare an array to hold four words like this:

You’ll notice this looks very similar to the way you declared a character array. The difference is that there is now an asterisk between the data type and the name of the array.  We could now initialise the elements of the array like this:

Here’s a challenge for you. With our previous array examples we’ve been able to declare and initialise the array at the same time. This is also possible with string arrays. Have a go at doing this before you read on, then click below to see the solution.

So what does the asterisk mean? We’ve already established that in c when you declare and initialise a simple variable, some memory is set aside to hold the value. When we want to access that memory we use the name of the variable to reference it. Sometimes though, it’s helpful to be able to reference the memory location (or address) of some data directly. We do this using something called a pointer. A pointer is simply a type of variable that holds the address of some data. We use the asterisk to say that we want a pointer rather than a simple variable. So when our line of code above is executed, what we get is something like this:

string array storage

Each of our strings is stored in memory, one after the other. You’ll recall that each string is terminated with a null character (represented by the escape sequence \0). Our array then consists of a four pointers, and each pointer contains the address in memory of the first character of the associated string. You could say that each element of the array points to its string in memory.

We can use pointers in the same way we used simple variables. So if we wanted to display the second word we could do this with:

OK, let’s set up our word lists. Although the original BASIC programme placed all the words into a single array, we will place the words into three arrays, for the first adjective words, second adjective words, and noun words respectively. Modify your code so it looks like this:

You’ll notice I’ve added a new keyword, const, to the start of the declaration. This is short for constant, and it tells C that these arrays will not change once they’ve been initialised. When you have  variables or arrays that should remain constant, it’s always a good idea to use the const keyword, because the compiler will show an error message if you try to make changes to those values. This will help you keep errors out of your programmes.

In the next part of this series you will see how to pick words at random from each list and display them. Until then, here’s a challenge. Display the phrase “flexible cognitive algorithm” by displaying the correct element from each array. Put a new line at the start and end of the phrase and separate each word with a space.

The solution will be shown in the next post.

This entry was posted in Games, Programming Tutorials and tagged , , , , , . Bookmark the permalink.

Leave a reply