Computer Science Canada

Why am I getting an array error here?

Author:  GlobeTrotter [ Tue Jun 22, 2004 9:09 pm ]
Post subject:  Why am I getting an array error here?

code:

setscreen ("graphics:600,300;offscreenonly")
var xsize, ysize : int
xsize := 30
ysize := 30
var tile : array 0 .. maxx div xsize, 0 .. maxy div ysize of int
var px, py, pdir : int
var chars : array char of boolean
var holding := false
px := 1
py := 1
pdir := 1
for x : 0 .. maxx div xsize
    for y : 0 .. maxy div ysize
        if y = white or x = white or x = maxx div xsize then
            tile (x, y) := darkgrey
        else
            tile (x, y) := white
        end if
        tile (px, py) := red
        tile (5, 1) := grey
        tile (6, 1) := darkgrey
    end for
end for
loop
    cls
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        loop
            Input.KeyDown (chars)
            exit when ~chars (KEY_RIGHT_ARROW)
        end loop
        if tile (px + 1, py) = white and ~holding then
            tile (px + 1, py) := red
            tile (px, py) := white
            px += 1
        elsif tile (px + 1, py) = white and holding and tile (px + 1, py + 1) = white then
            tile (px + 1, py) := red
            tile (px, py) := white
            tile (px + 1, py + 1) := grey
            tile (px, py + 1) := white
            px += 1
        end if
        pdir := 1
    end if
    if chars (KEY_LEFT_ARROW) then
        loop
            Input.KeyDown (chars)
            exit when ~chars (KEY_LEFT_ARROW)
        end loop
        if tile (px - 1, py) = white and ~holding then
            tile (px - 1, py) := red
            tile (px, py) := white
            px -= 1
        elsif tile (px - 1, py) = white and holding and tile (px - 1, py + 1) = white then
            tile (px - 1, py) := red
            tile (px, py) := white
            tile (px - 1, py + 1) := grey
            tile (px, py + 1) := white
            px -= 1
        end if
        pdir := -1
    end if
    if chars (KEY_UP_ARROW) then
        loop
            Input.KeyDown (chars)
            exit when ~chars (KEY_UP_ARROW)
        end loop
        if tile (px + pdir, py) ~= white and tile (px + pdir, py + 1) = white and ~holding then
            tile (px + pdir, py + 1) := red
            tile (px, py) := white
            px += pdir
            py += 1
        elsif tile (px + pdir, py) ~= white and tile (px + pdir, py + 1) = white and holding then
            tile (px + pdir, py + 1) := red
            tile (px, py) := white
            tile (px + pdir, py + 2) := grey
            tile (px, py + 1) := white
            px += pdir
            py += 1
        end if
    end if
    if chars (KEY_DOWN_ARROW) then
        loop
            Input.KeyDown (chars)
            exit when ~chars (KEY_DOWN_ARROW)
        end loop
        if tile (px + pdir, py) = grey and ~holding and tile (px + pdir, py + 1) = white then
            holding := true
            tile (px + pdir, py) := white
            tile (px, py + 1) := grey
        elsif holding and tile (px + pdir, py + 1) = white then
            holding := false
            tile (px, py + 1) := white
            tile (px + pdir, py + 1) := grey
        end if
    end if
    if tile (px, py - 1) = white and ~holding then
        tile (px, py - 1) := red
        tile (px, py) := white
        py -= 1
    elsif tile (px, py - 1) = white and holding then
        tile (px, py - 1) := red
        tile (px, py) := white
        tile (px, py) := grey
        tile (px, py + 1) := white
        py -= 1
    end if
    for x : 0 .. maxx by xsize
        for y : 0 .. maxy by ysize
            drawfillbox (x, y, x + xsize, y + ysize, tile (x div xsize, y div ysize))
            drawbox (x, y, x + xsize, y + ysize, 7)
            if y ~= 0 and tile (x, y) ~= white and tile (x, y - 1) = white then
                tile (x, y - 1) := tile (x, y)
                tile (x, y) := white
            end if
        end for
    end for
    drawfilloval (px * xsize + (xsize div 2) + pdir * (xsize div 3), py * xsize + (ysize div 2), 5, 5, 7)
    View.Update
end loop


Shouldn't the y ~= 0 part get rid of the error? It doesn't.

Author:  Cervantes [ Wed Jun 23, 2004 7:48 am ]
Post subject: 

the for x : 0 .. maxx by xsize and same with the for y : 0 .. maxy by ysize is going from 0 to maxx and maxy, respecitvely. maxx and maxy are much larger than your tile array.
it needs to look like this:
code:

    for x : 0 .. maxx div xsize
        for y : 0 .. maxy div ysize
            drawfillbox (x * xsize, y * ysize, x * xsize + xsize, y * ysize + ysize, tile (x, y))
            drawbox (x * xsize, y * ysize, x * xsize + xsize, y * ysize + ysize, 7)
            if y ~= 0 and y ~= 1 then
                if tile (x, y) ~= white and tile (x, y - 1) = white then
                    tile (x, y - 1) := tile (x, y)
                    tile (x, y) := white
                end if
            end if
        end for
    end for

Author:  GlobeTrotter [ Wed Jun 23, 2004 12:05 pm ]
Post subject: 

Thanks. I didn't realize I had the by ysize instead of div ysize.


: