Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
66 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Arrays
 Vectors
 OOP
 Files
 Arrays in Games

 Printable version
 Discuss this article
 in the forums


The Series
 The Basics
 Making a
 Simple Game

 The Power of Arrays

Last time we talked about the steps necessary to create an air hockey game. In order to make this game we needed to know about double-buffering, threads, and how to use the mouse and the keyboard. Originally I was going to talk about AWT and neural networks in this part, but I have decided to push these topics into part iv and talk about arrays instead. You may be thinking that arrays are pretty trivial and have no big impact on making games, but this couldn’t be farther from the truth. In our exploration of arrays we will look at how to create Tetris, and PacMan games. We will also look at some basic file handling concepts as well as some important points about object-oriented programming.

Arrays

If you have programmed in other languages or have worked with Java you have probably seen arrays in action. For those of you who are unfamiliar with arrays and how they work I will briefly outline them here.

When we are programming we use single variables which can be viewed as a lone box in memory. A typical problem that we will come across when programming is the need to store the information for an 8x8 board. By following the single variable method we would be required to declare 64 variables and wouldn’t be allowed to use for loops for our initialization or when we wanted to traverse the whole board. This hardly seems like an efficient way to program and would not only result in large programs, but also in frustrated programmers.

Eg. Single variable method of storing an 8x8 board

int board1;
int board2;
...
int board63;
int board64;

In order to make our lives easier and to make our code at least semi-readable by others we are fortunate that there is a better way. An array is a collection of variables that can be referred to by one name and accessed with an index. For example, if we wanted to access the second element in the second row of the board we would type a line of code that contained board[1][1]. You will notice that the indexes are one less than the number that I was looking for originally. The reason for this is that in an 8x8 array the indexes go from 0-7 for both dimensions. By using arrays we can now use for loops to traverse through the index values of the array and initialize or make changes to all of the data in the array.

Eg. Array method of storing an 8x8 board

int board[][] = new int[8][8];

Now doesn’t that look a lot nicer than the 64 lines that would have been required with the other method? You will notice that in my example above I used a two dimensional array. One, two, three, and maybe even four dimensional arrays are the ones that you will generally see, but for the most part you will only use one and two dimensional arrays.

You will notice that when we create our arrays we must use memory allocation to set up the dimensions that we would like. This means that with the name of the variable we include an empty set of square brackets, [], for every dimension that we want the array to have. We then have the choice of allocating the memory for the array at the time we declare the variable or we can defer this until later on in our program when we are going to use it. Whenever we decide to allocate the memory we set the variable equal to the keyword new, followed by the type of the data which will be found in the array, and then sets of square brackets for every dimension of the array. This time, however, the sets of square brackets instead of being empty will contain the number of "squares" in each dimension.

This may seem a little unfamiliar at first for those of you who have never seen arrays before, but once we start to use arrays in the games later on in this article you will quickly pick up on how they work.





Next : Vectors