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

Username:   Password: 
 RegisterRegister   
 Loop / For Loop Not Exiting
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Sean




PostPosted: Mon Jun 09, 2008 6:41 pm   Post subject: Loop / For Loop Not Exiting

Alright, going back to Turing now and trying to creat a game. I have the basics down of Mouse.Where, Drawing Random Boxes using arrays, and that, however when my condition is met, it doesn't exit properly. I tried adding in a boolean to set it to true when the condition is met and that it would trigger an outside if statement from the for loop to stop the entire game loop.

I'm confused as to what I am doing wrong.

Turing:

View.Set ("offscreenonly")

var x, y, b : int
var box_x, box_y : array 0 .. 10 of int
var checker : boolean := false

loop
    Mouse.Where (x, y, b)
    View.Update
    cls
    for x_position : lower (box_x) .. upper (box_x)
        randint (box_x (x_position), 0, maxx)
    end for

    for y_position : lower (box_y) .. upper (box_y)
        randint (box_y (y_position), 0, maxy)
    end for
    delay (1000)
    for counter : 0 .. 10
        Draw.FillBox (box_x (counter), box_y (counter), box_x (counter) + 30, box_y (counter) + 30, black)
        if x >= box_x (counter) and x <= box_x (counter) + 30 and x >= box_y (counter) and x <= box_y (counter) + 30 then
            checker := true
        end if
    end for
    if checker = true then
        exit
    end if
end loop


I've tried tracing it, looking up int he index and looking through the forums, however to my prevail nothing worked. I don't want the solution in code, but in an explation so I can learn from my mistake without taking a complete code from someone else.
Sponsor
Sponsor
Sponsor
sponsor
Gryphon




PostPosted: Mon Jun 09, 2008 6:54 pm   Post subject: Re: Loop / For Loop Not Exiting

You have the right idea in trying to exit the loop but the wrong syntax, I dont recall ever seeing a loop exited the way you did, although it would make sense if you could do this. What you want to try is the exit when statement which is normally used to exit loops. The syntax is this:
code:

exit when variable = something
richcash




PostPosted: Mon Jun 09, 2008 8:17 pm   Post subject: Re: Loop / For Loop Not Exiting

First of all, you have a typo in your if statement. You should be checking the y coordinate of the mouse against the y coordinates of the box(es).

After fixing that typo, your code does work, but probably not the way you want. Try leaving the mouse in one spot and waiting until a box randomly gets positioned on it (you may want to increase the number of boxes, as it may take a while).

Each box is drawn and then less than milliseconds later the mouse is checked only once to be on that box. So you can not possibly move the mouse over quick enough to one of the boxes and get it registered by that if statement. You will almost certainly be moving the mouse around during the delay(1000).

I suggest you use Time.Elapsed instead. Check if the mouse is touching any of the boxes each iteration of the loop, but only re-randomize and draw the boxes every so often (approximately 1 second, for example).

I'll leave a possible way of handling the time.
code:
View.Set ("offscreenonly")
var t := 0
var b_x, b_y := 0
loop
    if Time.Elapsed > t * 1000 then
        b_x := Rand.Int (100, maxx - 100)
        b_y := Rand.Int (100, maxy - 100)
        t += 1
        cls
        Draw.FillBox (b_x, b_y, b_x + 25, b_y + 25, black)
    end if
    var x, y, b : int
    Mouse.Where (x, y, b)
    exit when x > b_x and x < b_x + 25 and y > b_y and y < b_y + 25
    View.Update
end loop
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: