Using Arrays in Smart Pascal


We have covered the data types integer, string, real and Boolean. An array is a data structure used to store data items of the same type. In a static array, the array has a fixed length that is included in the declaration. A dynamic array is declared without a length. Its length must be set before use, and the length can be changed to suit changing circumstances. We begin with static arrays, which are easier to use.

Static Arrays

Our first example is an array of string that is used to store names of colours.
  Colours : array [1 .. 6] of string;    

Array declarations must show the identifier (in this case Colours), the bounds (a start value of 1 and an end value of 6) and the type (string).

To identify individual members of the array, supply the index (also known as the subscript) in square brackets. The statement
Colours[2] := 'pink';
uses the index 2 to assign the value 'pink' to the second string in the array. The statement
Canvas.FillStyle := Colours[2];   
would then set the fill colour to pink.
Arrays can be declared and initialised in one statement e.g.
  Colours : array [1 .. 6] of string = ['red', 'pink', 'white', 'black', 'blue', 'green'];

This short demonstration randomly chooses each second a colour for the GameView.

unit Unit1;


  System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application,
  SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics;

  TCanvasProject = class(TW3CustomGameApplication)
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;


  Colours : array [1 .. 6] of string = ['red', 'pink', 'white', 'black', 'blue', 'green'];

procedure TCanvasProject.ApplicationStarting;
  GameView.Delay := 1000;

procedure TCanvasProject.ApplicationClosing;

procedure TCanvasProject.PaintView(Canvas: TW3Canvas);
  // Clear background to one of six colours chosen at random
  var i := RandomInt(6) + 1;
  Canvas.FillStyle := Colours[i];
  Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);

An example of an integer array is
  Alien_X_Coords: array[1 .. 100] of integer;    
You could use this array to store successive x coordinates for an alien to take as it wanders down the screen.

The demonstration on the following page uses the LandHeight array to store a randomly generated terrain to be used in a game. The code is based on Jason McMillen's Artillery.

Arrays are used widely. In Knowledge, for example, Peter Hearnshaw stores street and place names in arrays of string and coordinates of road junctions and place in arrays of integer. He uses parallel arrays; one array stores x coordinates and another stores the corresponding y coordinates. In Driving-3D, Peter uses a single two-dimensional array to store coordinates of points on the track. We use the same array for a Scalextric-like demonstration that you can see by following the second numbered link below. We declare and initialise the array in a single statement. We could instead have declared it with
  TrackCoords: array [1..227, 1..2] of integer;    
The first x and y coordinates would then be assigned with the code:
TrackCoords[1, 1] := 1300;
TrackCoords[1, 2] := 300;
You can see how much more laborious it would be to assign values individually!

Dynamic Arrays

In program Terrain, we declared the LandHeight array with an upper bound of 2000 as a safe limit on the range of indices that we would require. We could have declared the array as a dynamic array with the code:
LandHeight: array of integer;   
We could have set its length with this code in the ApplicationStarting procedure before the instructions to assign the elements of the array:

A dynamic array is zero-based and has useful "pseudo-methods" such as Push (to add an element to the end), Pop (to remove the last element), Clear, Copy, Delete, IndexOf, Swap and Reverse.


Programming - a skill for life!

How to learn the Pascal language the fun way by making games in Smart Mobile Studio