Back to DFS's Pascal Page


TWO-DIMENSIONAL ARRAYS

Two-dimensional arrays are a little more complicated to use than one-dimensional ones. This page illustrates several concepts.

The purpose of the code presented here is to initialize a two-dimensional array in order to print the starting positions of all of the pieces for a game of chess. A complete sample program is provided below.

The Chessboard as an Array

8bRbNbBbQbKbBbNbR
7bPbPbPbPbPbPbPbP
6        
5        
4        
3        
2wPwPwPwPwPwPwPwP
1wRwNwBwQwKwBwNwR
 abcdefgh

The chessboard is an 8x8 square with squares of alternating colors. The modern way of referring to each individual square is by using a lowercase letter (a to h) followed by a digit (1 to 8). Thus, the square in the lower left corner is referred to as "a1" and its kitty-corner opposite as "h8". The pieces, in descending order of importance, are designated by letter:

KQRBNP
KingQueenRookBishopkNightPawn

Consider the arrays of strings of length 2 as declared below.

type
  Pieces_Type = array ['a'..'h'] of string[2];
  Board_Type =  array [1..8, 'a'..'h'] of string[2];
var
  MenOnBoard : Board_Type;
  WhiteMajorPieces : Pieces_Type;
  BlackMajorPieces : Pieces_Type;

The strings are of length two to allow for 'w' and 'b' to be used to distinguish between the White and Black pieces. With two procedures, it is easy to place all of the pieces on a virtual two-dimensional chessboard. The following procedure initializes one-dimensional arrays with two-character labels for the major White and Black pieces. (Note that the formal parameters are marked as variable so that the global variables, WhiteMajorPieces and BlackMajorPieces, can be changed when passed as the actual parameters from the main line or some other procedure.)

Procedure InitMajorPieces(var White, Black : Pieces_Type);
var
  col : char;
begin
  White['a'] := 'wR';
  White['b'] := 'wN';
  White['c'] := 'wB';
  White['d'] := 'wQ';
  White['e'] := 'wK';
  White['f'] := 'wB';
  White['g'] := 'wN';
  White['h'] := 'wR';
  for col := 'a' to 'h' do
    begin
      Black[col] := White[col];
      Black[col,1] := 'b'
    end
end;

The subscripts for these arrays match those used for each row of the two-dimensional array for the board, 'a' to 'h'. The first eight lines of code initialize the array for the White pieces. Then a loop copies these labels into a parallel array and changes the first character of each label from 'w' to 'b', thus creating all of the labels for the major Black pieces.

Once the arrays for the major pieces have been initialized, we are ready to place all of the pieces on the chessboard represented by MenOnBoard. Since the one-dimensional arrays holding the major pieces will not be changed by this procedure, they are received as value parameters. The array for the board, on the other hand, is to be filled in by this procedure and is therefore received as a variable parameter.

Procedure InitBoard(var Board : Board_Type; White, Black : Pieces_Type);
var
  col : char;
begin
  for col := 'a' to 'h' do
    begin
      Board[1, col] := White[col];
      Board[2, col] := 'wP';
      Board[3, col] := '';
      Board[4, col] := '';
      Board[5, col] := '';
      Board[6, col] := '';
      Board[7, col] := 'bP';
      Board[8, col] := Black[col]
    end
end;

Each iteration of the loop fills in one column (called a file in chess) of the board, starting from the rank nearest the player playing White. The labels for the major pieces are copied from the Pieces_Type arrays, the labels for the pawns are assigned to their proper locations and the remainder of the board is filled in with null strings.

An alternative method of initializing the board would be to clean off the board and then place the pieces in their starting positions. This could be done with the following pair of procedures:

Procedure CleanOffBoard(var Board: Board_Type);
var
  col : char;
  row : integer;
begin
  for col := 'a' to 'h' do
    for row := 1 to 8 do
      Board[row, col] := ''
end;

Procedure InitBoard(var Board : Board_Type; White, Black : Pieces_Type);
var
  col : char;
begin
  CleanOffBoard(Board);
  for col := 'a' to 'h' do
    begin
      Board[1, col] := White[col];
      Board[2, col] := 'wP';
      Board[7, col] := 'bP';
      Board[8, col] := Black[col]
    end
end;

This technique has the advantage that we have now made available a procedure, CleanOffBoard, which could be used in other situations. This procedure also clearly shows how nested loops can be used to access a two-dimensional array.

The procedure below uses nested loops (but less clearly) and can be used to print a representation of the whole chessboard with all of the men in their starting positions. It is strongly recommended that you copy the full program at the end of this file and trace through PrintBoard with watches placed on col, row and i to understand how the nested loops are utilized.

Procedure PrintBoard(Board : Board_Type);
var
  col : char;
  row : integer;
  i : integer;
begin
  write(chr(201));
  for i := 1 to 7 do
    write(chr(205), chr(205), chr(203));
  writeln(chr(205), chr(205), chr(187));
  for row := 8 downto 1 do
    begin
      write(chr(186));
      for col := 'a' to 'h' do
        begin
          if( (row + ord(col)) MOD 2 = 1) then
            textbackground(RED);
          write(Board[row, col]:2);
          textbackground(BLACK);
          write(chr(186))
        end;
      writeln;
      if row > 1 then
        begin
          write(chr(204));
          for i := 1 to 7 do
            write(chr(205), chr(205), chr(206));
          writeln(chr(205), chr(205), chr(185))
        end
    end;
  write(chr(200));
  for i := 1 to 7 do
    write(chr(205), chr(205), chr(202));
  writeln(chr(205), chr(205), chr(188))
end;

Sample Program

This is a program which initializes two one-dimensional arrays, and then uses these arrays to initialize a two-dimensional array. Finally, the PrintBoard procedure is called to display the starting positions of the chessmen.

Program ChessBoardInit (output);
uses crt;
type
  Pieces_Type = array ['a'..'h'] of string[2];
  Board_Type =  array [1..8, 'a'..'h'] of string[2];
var
  MenOnBoard : Board_Type;
  WhiteMajorPieces : Pieces_Type;
  BlackMajorPieces : Pieces_Type;

Procedure InitMajorPieces(var White, Black : Pieces_Type);
var
  col : char;
begin
  White['a'] := 'wR';
  White['b'] := 'wN';
  White['c'] := 'wB';
  White['d'] := 'wQ';
  White['e'] := 'wK';
  White['f'] := 'wB';
  White['g'] := 'wN';
  White['h'] := 'wR';
  for col := 'a' to 'h' do
    begin
      Black[col] := White[col];
      Black[col,1] := 'b'
    end
end;

Procedure InitBoard(var Board : Board_Type; White, Black : Pieces_Type);
var
  col : char;
begin
  for col := 'a' to 'h' do
    begin
      Board[1, col] := White[col];
      Board[2, col] := 'wP';
      Board[3, col] := '';
      Board[4, col] := '';
      Board[5, col] := '';
      Board[6, col] := '';
      Board[7, col] := 'bP';
      Board[8, col] := Black[col]
    end
end;

Procedure PrintBoard(Board : Board_Type);
var
  col : char;
  row : integer;
  i : integer;
begin
  write(chr(201));
  for i := 1 to 7 do
    write(chr(205), chr(205), chr(203));
  writeln(chr(205), chr(205), chr(187));
  for row := 8 downto 1 do
    begin
      write(chr(186));
      for col := 'a' to 'h' do
        begin
          if( (row + ord(col)) MOD 2 = 1) then
            textbackground(RED);
          write(Board[row, col]:2);
          textbackground(BLACK);
          write(chr(186))
        end;
      writeln;
      if row > 1 then
        begin
          write(chr(204));
          for i := 1 to 7 do
            write(chr(205), chr(205), chr(206));
          writeln(chr(205), chr(205), chr(185))
        end
    end;
  write(chr(200));
  for i := 1 to 7 do
    write(chr(205), chr(205), chr(202));
  writeln(chr(205), chr(205), chr(188))
end;


begin {Main Line}
  ClrScr;
  InitMajorPieces(WhiteMajorPieces, BlackMajorPieces);
  InitBoard(MenOnBoard, WhiteMajorPieces, BlackMajorPieces);
  PrintBoard(MenOnBoard)
end.

Back to Top


© DFStermole 2000
Created 16 May 2000
Revised 1 June 2001