I'm trying to represent a two-dimensional coordinate grid with a two-dimensional array. Problem is, declaring the array flips the X and Y coordinates because of the way Delphi allocates the array. This makes it difficult to read elements of the array. For example, the following program gives a range check error while trying to print:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
{$R+}
procedure play;
var
grid: array of array of boolean;
x, y: integer;
begin
try
setLength(grid, 3, 8);
grid[1, 5] := true;
for y := low(grid) to high(grid) do
begin
for x := low(grid[y]) to high(grid[y]) do
begin
if grid[x, y] then
write('X')
else write('.');
end;
writeln;
end;
readln;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end;
begin
play;
end.
I have to write the index backwards (if grid[y, x] then) to keep that from happening, but then the grid prints out sideways, with the X shown at (5, 1) instead of at (1, 5). If I try to change the shape of the grid by saying setLength(grid, 3, 8); then the assignment on the next line gives a range check error. I end up having to write all of my coordinates backwards, and any time I forget they're backwards, bad things end up happening in the program.
Does anyone know any tricks to make the coordinate order work intuitively?