Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Why am I getting an array error here?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GlobeTrotter




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Wed Jun 23, 2004 7:48 am   Post subject: (No 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
GlobeTrotter




PostPosted: Wed Jun 23, 2004 12:05 pm   Post subject: (No subject)

Thanks. I didn't realize I had the by ysize instead of div ysize.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: