Arrays

An array is a data structure that is similar to a variable and has a number of elements, or "slots". While a variable is like a box that can hold one value, an array is like having multiple boxes to hold a piece of information in each slot. As a rule of thumb, an array must be of the same data type. You can not combine e.g. integers and strings in a single array.

Array Index

Main article : Array Index

Arrays use an array index to allow you to select which slot to read or write data from or to. An array may have more than one array index (see below.) In BASIC dialects, including PlayBasic, the array index is almost always contained in parenthesis ( ( ) ). In game programming languages that use a form C-Syntax, the array index is contained in brackets ( [ ] ).

; BASIC Style
MyArray(5)

// C-Syntax Style
MyArray[5];

Array Dimension

Main article : Array Dimension

The dimension of an array is dependent on the number of indexes it has. An array can have 1, 2, or 3 indexes. These are 1D, 2D, and 3D arrays respectively.

Data Type Sign

Arrays that contain strings or real (or floating point) numbers are required to have a data type sign as a part of their name. For strings, it is a dollar sign ( $ ). For floating point numbers, it is a ( # ) sign. integer arrays require no data type sign.

StringArray$(5)
RealNumberArray#(5)
IntegerArray(5)

(See : data type)

Using Arrays

Uses for Arrays

Arrays can be used to hold anything that requires a group or set of data.

Good Uses for Arrays

  • An array of types can be used to contain all the game objects in your game, or they can be contained in several arrays with one array per game object type.
  • A 2D array can hold a table of information
    • A tile map used to hold information about the layout of a game level. A 3D array can hold information about the layout of multiple levels.
    • A highscore table.
  • An array can hold data from which you want to select randomly.

Bad Uses for Arrays

  • The different stats of a single game object. You are better off using types.

Creating an Array

In BASIC, you can create an array just like you create a variable, except that you must declare how many slots you need. The number is known as the array index. Also the array index must be enclosed in parentheses.

DIM someArray(5)

Depending on the programming language you are using, this will create an array with either 5 or 6 slots. The difference is that some languages begin arrays at slot 1 and others at slot 0.

After you have created the array, you can treat each slot like it was a different variable.

someArray(1) = 10
someArray(2) = 15
someArray(3) = 20
someArray(4) = 25
someArray(5) = 30

Accessing The Array

After assigning a value to each element or "slot" in the array, you can access the array the same way you access a variable.

Array Output

One way of accessing the array is, to write the value of a specific element in the array to the screen using the Print function.

Print someArray(3)

This will print the value of array index 3 (assuming its been assigned).

Array Input

You can also have the user assign the value of the array index.

Dim as Integer i 

Input "How many players "; i

Dim as String player(i)

This will let the user decide how many elements the array will contain.

Now to have the user assign a value to each element in the array :

Dim as Integer i

Input "How many players "; i

Dim as String player(i), names(i)

For v = 1 to i
    Print "Player "; v;
    Input "enter your name"; names(v)
        i = v
        player(i) = names(i)
Next v

For a = 1 to i
      Print player(a)
Next a
Sleep

This will let the user assign all values of the arrays except the array's name.

Iterating Arrays

To iterate means to go through a group of items one by one. Because the array indexes are numeric, you can use a FOR loop to quickly go through elements (slots) in the array. You can print each one like this:

someArray(1) = 1
someArray(2) = 2
someArray(3) = 3
someArray(4) = 4
someArray(5) = 5

FOR i = 1 TO 5
    print someArray(i)
NEXT i

Another example of iterate would be to use a FOR loop to give each array element a random value. This would be good for a dice game where the player rolls multiple dice (such as Yahtzee).

FOR i = 1 TO 5
    someArray(i) = INT(RND(1)*6)+1
NEXT i

Randomizing Arrays

To randomize an array, would be to have the array's set values appear in a random order. This is useful if you wanted the program to make a decision for you. An example would be a simple coin toss:

Randomize Timer
Dim As String coin(2)

coin(1) = "Heads"
coin(2) = "Tails" 

Print coin(Int(Rnd(1)*2)+1)
Sleep

The result would be just like flipping a coin!

Another possibility would be to randomize combinations of arrays :

Randomize Timer
Dim as String day(7), quality(2)

day(1) = "Monday will be "
day(2) = "Tuesday will be "
day(3) = "Wednesday will be "
day(4) = "Thursday will be "
day(5) = "Friday will be "
day(6) = "Saturday will be "
day(7) = "Sunday will be "

quality(1) = "Sunny"
quality(2) = "Rainny"

Print day(Int(Rnd(1)*7)+1); quality(Int(Rnd(1)*2)+1)
sleep

Filing Arrays with READ and DATA

  • write me

Related Pages

Links

Game Design Network Pages

To Do

  • Choosing a random item from an array.
  • Global arrays
  • Shuffling the contents of an array.
  • Sorting an array using bubble sort and other sorting algorithms.
Categories: Arrays : Data : Data Structures : Programming
page_revision: 23, last_edited: 1244744299|%e %b %Y, %H:%M %Z (%O ago)