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

Username:   Password: 
 RegisterRegister   
 Da Snake.. problem with the flexible array....
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
santabruzer




PostPosted: Tue Feb 10, 2004 11:04 pm   Post subject: Da Snake.. problem with the flexible array....

here is the most ineffiecient program i have written in my programming career.. and i mean it.. ..... fix if you can Razz... well it works for the first ten "food" items....
code:
setscreen ("graphics:400;400,nocursor,offscreenonly")
var chars : array char of boolean
var cellsize := 16
var lsnake := 3
var count := 1
var limit := 400 div cellsize
var delaytime := 90
var points := 0
var ate := false
var colorfood := yellow
var colorsnake := 7
var temprow, tempcol := 0
var leftright, updown := 0
var trigger := false
var trig3 := false
type coor :
    record
        row, col : int
    end record
var snake : flexible array 1 .. lsnake of coor

proc drawboard
    drawbox (0, 0, maxx, maxy, 7)
    for i : 1 .. limit
        drawline (i * cellsize, 0, i * cellsize, maxy, 7)
        drawline (0, i * cellsize, maxx, i * cellsize, 7)
    end for
end drawboard

proc drawcell (col, row, clr : int)
    var temprow1 := row - 1
    var tempcol1 := col - 1
    drawfillbox (temprow1 * cellsize, tempcol1 * cellsize, row * cellsize, col * cellsize, clr)
end drawcell

proc clear
    updown := 0
    leftright := 0
end clear

proc checkfood (rownum, colnum : int)
    var center := cellsize div 2
    if whatdotcolor (colnum * cellsize - center, rownum * cellsize - center) = colorfood then
        points += 1
        ate := false
        trig3 := true
    elsif whatdotcolor (colnum * cellsize - center, rownum * cellsize - center) = colorsnake then
        trigger := true
    end if

end checkfood

proc movement
    if updown = 1 then
        checkfood (snake (lsnake).row + 1, snake (lsnake).col)
        snake (lsnake).row += 1
    elsif updown = 2 then
        checkfood (snake (lsnake).row - 1, snake (lsnake).col)
        snake (lsnake).row -= 1
    elsif leftright = 1 then
        checkfood (snake (lsnake).row, snake (lsnake).col - 1)
        snake (lsnake).col -= 1
    elsif leftright = 2 then
        checkfood (snake (lsnake).row, snake (lsnake).col + 1)
        snake (lsnake).col += 1
    end if
end movement

proc food
    var count := 0
    if ate = false then
        loop
            loop
                tempcol := Rand.Int (1, limit)
                for i : 1 .. lsnake
                    if tempcol not= snake (i).col then
                        count += 1
                    end if
                end for
                exit when count = lsnake
                count := 0
            end loop
            temprow := Rand.Int (1, limit)
            count := 0
            for i : 1 .. lsnake
                if temprow not= snake (i).row then
                    count += 1
                end if
            end for
            exit when count = lsnake
        end loop
        ate := true
    end if
end food

var tlen, trow, tcol := 0

for i : 1 .. lsnake
    snake (i).row := limit div 2
    snake (i).col := limit div 2
end for

var trig2 := true

loop
    tlen := lsnake
    trow := snake (lsnake).row
    tcol := snake (lsnake).col

    if points mod 5 = 0 and points not= 0 then
        if trig2 = false then
            lsnake += 1
            new snake, lsnake
            trig2 := true
        elsif trig3 = true then
            trig2 := false
            trig3 := false
        end if
    end if

   
   
    snake (lsnake).row := trow
    snake (lsnake).col := tcol

    Input.Flush
    Input.KeyDown (chars)
    for i : 1 .. lsnake - 1
        snake (i).row := snake (i + 1).row
        snake (i).col := snake (i + 1).col
    end for

    if snake (lsnake).row >= 0 and snake (lsnake).row <= limit and
            snake (lsnake).col >= 0 and snake (lsnake).col <= limit then
        if chars (KEY_UP_ARROW) then
            clear
            updown := 1
        elsif chars (KEY_DOWN_ARROW) then
            clear
            updown := 2
        elsif chars (KEY_LEFT_ARROW) then
            clear
            leftright := 1
        elsif chars (KEY_RIGHT_ARROW) then
            clear
            leftright := 2
        elsif chars (chr (ORD_SPACE)) then
            clear
        end if
    else

        exit
    end if

    exit when trigger = true


    for i : 1 .. lsnake
        drawcell (snake (i).row, snake (i).col, colorsnake)
    end for

    drawcell (temprow, tempcol, colorfood)
    movement
    drawcell (snake (lsnake).row, snake (lsnake).col, colorsnake)

    food

    drawboard
    delay (delaytime)

    View.Update
    cls
    if count = lsnake then
        count := 1
    end if
end loop
put points
Sponsor
Sponsor
Sponsor
sponsor
Paul




PostPosted: Tue Feb 10, 2004 11:08 pm   Post subject: (No subject)

oooOOOoo I love it, I'll save this for further consultation. Though when you press a key in the opposite direction your going, you loose. And by how much does it grow ?
santabruzer




PostPosted: Tue Feb 10, 2004 11:09 pm   Post subject: (No subject)

it grows by one.. but ofcourse in the 20 min i had when i wrote this prog.. i stuck to the basics.. mod it.. Razz
DanShadow




PostPosted: Wed Feb 11, 2004 4:04 pm   Post subject: (No subject)

hmm...why dont you just save the (x,y) co-ordinates in a record array or something. I just found out about records a month ago, and now use them in pretty much every program I do. Very Happy
I did that program awhile ago, just by recording the (x,y) co-ordinates in a normal array, and redrew them. But if the (x or y) co-ordinate of the head of your snake is equal to the (x or y) co-ordinate of any part of the tail of your snake...you lose.[/code]
santabruzer




PostPosted: Wed Feb 11, 2004 4:22 pm   Post subject: (No subject)

firstly.. i did use a record array.. secondly that would be acceptable if the first part of the snake is with contact with the last part of the sanke.. ... iono... a array matrix is so much easier.. .. and have it like an X, Y co-ordinate.. if that is what you mean.. but what you suggested.. i know.. i just wanted to build something with "whatdotcolor"... any ideas to the flexible array on the sanke.. it basically stops working after 10 of those things are eaten.. saying that a varaible is not passed.. but yet.. i do pass it.. so i have no idea..
McKenzie




PostPosted: Wed Feb 11, 2004 11:06 pm   Post subject: (No subject)

All too easy.

Simple mistake Santa:
Quote:
lsnake := lsnake + points div 5

should be:
code:
lsnake := lsnake + 1


I think you see why Smile
santabruzer




PostPosted: Wed Feb 11, 2004 11:16 pm   Post subject: (No subject)

well.. excellent.. apparently.. i wanted it to happen.. and i forgot after i put the trigger in that i can just remove the points div 5.. wow.. i'm really amazed someone actually read that long program.. in that sloppy format.. with three trigger Razz
McKenzie




PostPosted: Thu Feb 12, 2004 8:38 am   Post subject: (No subject)

You'd be surprized just how many snake programs I've read over the years Smile
Sponsor
Sponsor
Sponsor
sponsor
the_short1




PostPosted: Thu Apr 08, 2004 10:58 pm   Post subject: (No subject)

OMG!!! i love it.... very nicde,... smooth.... i will make a .,exe for my sis... she LOVEs this game..... only thing i would change is get rid of the hidous buttonbar..... good game... i dont care if its uneficeint
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  [ 9 Posts ]
Jump to:   


Style:  
Search: