Computer Science Canada

Help Needed!!!

Author:  Dude_man_47 [ Thu Dec 01, 2005 11:44 pm ]
Post subject:  Help Needed!!!

I am making a program and I want to make boxes turn red when i click on them. I have the code for one box, but i was wondering if there was an easier way then writing 20 if statements. Any help would be greatly apreciated. ^_^

Here is my code for one box.

code:

var x, y, button : int

drawfillbox (0, 0, maxx, maxy, black)
drawfillbox (100, 100, 300, 300, white)

loop
    mousewhere (x, y, button)
    if button not= 0 and x > 100 and x < 300 and y > 100 and y < 300 then
        drawfillbox (100, 100, 300, 300, red)
        exit
    end if
end loop

Author:  Cervantes [ Fri Dec 02, 2005 8:44 am ]
Post subject: 

Welcome to CompSci.ca

Please, use a descriptive title. Link!


Now, as to your question. You'll probably want to look into creating an array of boxes. You would likely also want to create a function to check whether you clicked on a box. For tutorials on these topics, check [Turing Tutorials]. There are also links in the Turing Walkthrough.

Author:  Dude_man_47 [ Fri Dec 02, 2005 9:10 am ]
Post subject: 

Sorry about the title.

I looked into arrays but they dont seem to be working. I tried the tutorial but the code doesn't work. It says that variable has no value.

code:

var grid:array 1..10, 1..5 of int

for row:1..10
     for column:1..5
          put grid(row,column)..
     end for
put ""
end for

Author:  Tony [ Fri Dec 02, 2005 9:47 am ]
Post subject: 

well after initializing
code:

var grid:array 1..10, 1..5 of int

what's the value of grid at (1,1)? or any other location for that matter? Well that hasn't been specified, so of course it's empty. The code is syntatically correct.

Author:  do_pete [ Fri Dec 02, 2005 9:48 am ]
Post subject: 

It says it does't have a value becuase it doesn't have a value; you haven't assigned it anything. Try something like this:
code:
var Buttons : array 1 .. 20, 1 .. 4 of int
Buttons (1, 1) := 20
Buttons (1, 2) := 20
Buttons (1, 3) := 80
Buttons (1, 4) := 40
Buttons (2, 1) := 20
Buttons (2, 2) := 50
Buttons (2, 3) := 80
Buttons (2, 4) := 70
%Assign the rest of the values to the array here

function CheckClick (x1, y1, x2, y2 : int) : boolean
    var MouseX, MouseY, MouseButton : int
    Mouse.Where (MouseX, MouseY, MouseButton)
    if MouseButton not= 0 and MouseX > x1 and MouseX < x2 and MouseY > y1 and MouseY < y2 then
        result true
    else
        result false
    end if
end CheckClick

loop
    for i : 1 .. 2%Change 2 to upper(Buttons) once you've assigned all the values
        if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then
            Draw.Box (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4), brightred)
        else
            Draw.Box (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4), black)
        end if
    end for
end loop

Author:  Cervantes [ Fri Dec 02, 2005 10:42 am ]
Post subject: 

do_pete wrote:

code:
var Buttons : array 1 .. 20, 1 .. 4 of int
Buttons (1, 1) := 20
Buttons (1, 2) := 20
Buttons (1, 3) := 80
Buttons (1, 4) := 40
Buttons (2, 1) := 20
Buttons (2, 2) := 50
Buttons (2, 3) := 80
Buttons (2, 4) := 70
...

That's a very inefficient way to assign values. If there's any pattern (and there usually is), try using for loops:
code:

var Buttons : array 1 .. 20, 1 .. 4 of int
for j : 1 .. upper (Buttons, 1)
    for k : 1 .. upper (Buttons, 2)
        Buttons (j, k) := j * k  % hypothetical pattern
    end for
end for


If there is no pattern, read the values in from a file.

Author:  Dude_man_47 [ Fri Dec 02, 2005 11:08 am ]
Post subject: 

Cool,

I've tryed using a loop in various ways to state Buttons and none seem to work properly.
And if I state them all then its too much code. It starts to look like this:

code:

Buttons (1, 1) := 0
Buttons (1, 2) := 0
Buttons (1, 3) := 50
Buttons (1, 4) := 50
Buttons (2, 1) := 50
Buttons (2, 2) := 0
Buttons (2, 3) := 100
Buttons (2, 4) := 50
Buttons (3, 1) := 100
Buttons (3, 2) := 0
Buttons (3, 3) := 150
Buttons (3, 4) := 50


I want it to be like that but fill the entire screen using a loop. I'll continue to experiment but if anyone sees something that will work i'll be very thankfull.

Author:  Dude_man_47 [ Fri Dec 02, 2005 11:42 am ]
Post subject: 

I found a bit of a pattern but it is still too long and it takes forever for the comp to calculate. And it uses like 20 for loops.

code:

for i : 1 .. 10
    Buttons (i, 2) := 0
    Buttons (i, 4) := Buttons (i, 2) + 50
    for a : 10 .. 20
        Buttons (a, 2) := Buttons (i, 2) + 50
        Buttons (a, 4) := Buttons (a, 2) + 50
        for g : 20 .. 30
            Buttons (g, 2) := Buttons (i, 2) + 50
            Buttons (g, 4) := Buttons (g, 2) + 50
            for h : 30 .. 40
                Buttons (h, 2) := Buttons (g, 2) + 50
                Buttons (h, 4) := Buttons (h, 2) + 50
                for j : 40 .. 50
                    Buttons (j, 2) := Buttons (h, 2) + 50
                    Buttons (j, 4) := Buttons (j, 2) + 50
                    for k : 50 .. 60
                        Buttons (k, 2) := Buttons (j, 2) + 50
                        Buttons (k, 4) := Buttons (k, 2) + 50
                        for l : 60 .. 70
                            Buttons (l, 2) := Buttons (j, 2) + 50
                            Buttons (l, 4) := Buttons (l, 2) + 50
                            for m : 70 .. 80
                                Buttons (m, 2) := Buttons (l, 2) + 50
                                Buttons (m, 4) := Buttons (m, 2) + 50
                                for n : 80 .. 90
                                    Buttons (n, 2) := Buttons (m, 2) + 50
                                    Buttons (n, 4) := Buttons (n, 2) + 50
                                    for o : 90 .. 100
                                        Buttons (o, 2) := Buttons (n, 2) + 50
                                        Buttons (o, 4) := Buttons (o, 2) + 50
                                    end for
                                end for
                            end for
                        end for
                    end for
                end for
            end for
        end for
    end for
end for
for b : 1 .. 10
    Buttons (b, 1) := 0
    Buttons (b, 3) := Buttons (b, 1) + 50
    for c : 10 .. 20
        Buttons (c, 2) := Buttons (b, 2) + 50
        Buttons (c, 4) := Buttons (c, 2) + 50
        for d : 20 .. 30
            Buttons (d, 1) := Buttons (c, 1) + 50
            Buttons (d, 3) := Buttons (d, 1) + 50
            for e : 30 .. 40
                Buttons (e, 1) := Buttons (d, 1) + 50
                Buttons (e, 3) := Buttons (e, 1) + 50
                for f : 40 .. 50
                    Buttons (f, 1) := Buttons (e, 1) + 50
                    Buttons (f, 3) := Buttons (f, 1) + 50
                    for p : 50 .. 60
                        Buttons (p, 1) := Buttons (f, 1) + 50
                        Buttons (p, 3) := Buttons (p, 1) + 50
                        for q : 60 .. 70
                            Buttons (q, 1) := Buttons (p, 1) + 50
                            Buttons (q, 3) := Buttons (q, 1) + 50
                            for r : 70 .. 80
                                Buttons (r, 1) := Buttons (q, 1) + 50
                                Buttons (r, 3) := Buttons (r, 1) + 50
                                for s : 80 .. 90
                                    Buttons (s, 1) := Buttons (r, 1) + 50
                                    Buttons (s, 3) := Buttons (s, 1) + 50
                                    for t : 90 .. 100
                                        Buttons (t, 1) := Buttons (s, 1) + 50
                                        Buttons (t, 3) := Buttons (t, 1) + 50
                                    end for
                                end for
                            end for
                        end for
                    end for
                end for
            end for
        end for
    end for
end for




There has to be an easier/shorte way!!!

Author:  Tony [ Fri Dec 02, 2005 11:48 am ]
Post subject: 

Dude_man_47 wrote:
I found a bit of a pattern but it is still too long and it takes forever for the comp to calculate. And it uses like 20 for loops.

That's because you're redeclearing
code:

Buttons (o, 2) := Buttons (n, 2) + 50
Buttons (o, 4) := Buttons (o, 2) + 50

10^10 times. That's a BIG number of times.
You've got to end your forloops.. really

Author:  do_pete [ Fri Dec 02, 2005 11:52 am ]
Post subject: 

Youve got more than 80 lines there, wouldn't it be easier to declare the individual coordinates? Anyway i found a solution:
code:
for i : 1 .. upper (Buttons, 1)
    Buttons (i, 1) := maxx div 2 - 50
    Buttons (i, 2) := i * 20
    Buttons (i, 3) := maxx div 2 + 50
    Buttons (i, 4) := i * 20 + 15
end for

Author:  Dude_man_47 [ Fri Dec 02, 2005 1:08 pm ]
Post subject: 

Tanks, i did some fine tuning to it and found a pattern. I ran into a problem though. Buttons is an int. my code goes

code:

    if i mod 10 = 0 then
    Buttons (i, 3) := (i/10 + 1)*50


There's a for loop called i and if i mod 10 is 10 then i divid it by ten, say i is 20, 20/10 = 2. 2+ 1 = 3. 3 x 50 = 150. It says it is of the wrong type even though it doesnt get a decimal. the eror is at the *50. it says wrong type. What can i do to fix this?

Author:  do_pete [ Fri Dec 02, 2005 1:15 pm ]
Post subject: 

you can't use decimals when assigning a value to an int unless you use round() and you have to replace "/" with div

Author:  Tony [ Fri Dec 02, 2005 1:26 pm ]
Post subject: 

that's right. For example

4/2 returns 2.0

that is of the type real, not an int you're looking for. div drops that .0, so 5 div 2 will return 2 of type int

Author:  Dude_man_47 [ Fri Dec 02, 2005 1:33 pm ]
Post subject: 

Thanks, how do you round to 10. like if you have 19, round to 20, or 14 round to 10?

Author:  do_pete [ Fri Dec 02, 2005 1:45 pm ]
Post subject: 

try this:
code:
value := round (value / 10) * 10

Author:  Dude_man_47 [ Fri Dec 02, 2005 3:28 pm ]
Post subject: 

Thanks for the help now it works!!!!
Check it out.

code:

setscreen ("graphics:450;450,nobuttonbar")
var Buttons : array 1 .. 100, 1 .. 4 of int
for i : 1 .. 100
    if i mod 10 = 0 then
        Buttons (i, 1) := 0
        Buttons (i, 3) := 50
        Buttons (i, 2) := (i div 10) * 50
        Buttons (i, 4) := (i div 10 + 1) * 50
    elsif i = 1 then
        Buttons (i, 1) := 0
        Buttons (i, 2) := 0
        Buttons (i, 3) := 50
        Buttons (i, 4) := 50
    elsif i < 10 and i not= 1 then
        Buttons (i, 1) := (i-1) * 50
        Buttons (i, 2) := 0
        Buttons (i, 3) := i * 50
        Buttons (i, 4) := 50
    elsif i > 10 and i mod 10 not= 0 then
        Buttons (i, 1) := i mod 10 * 50
        Buttons (i, 2) := i div 10 * 50
        Buttons (i, 3) := i mod 10 * 50 + 50
        Buttons (i, 4) := i div 10 * 50 + 50
    end if
end for

function CheckClick (x1, y1, x2, y2 : int) : boolean
    var MouseX, MouseY, MouseButton : int
    Mouse.Where (MouseX, MouseY, MouseButton)
    if MouseButton not= 0 and MouseX > x1 and MouseX < x2 and MouseY > y1 and MouseY < y2 then
        result true
    else
        result false
    end if
end CheckClick

loop
    for i : 1 .. 100     %Change 2 to upper(Buttons) once you've assigned all the values
        if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then
            drawfillbox (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4), brightred)
        else
            Draw.Box (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4), black)
        end if
    end for
end loop

Author:  Mr. T [ Fri Dec 02, 2005 8:51 pm ]
Post subject:  Alex's Opinion

Dude_man_47 wrote:
Thanks, how do you round to 10. like if you have 19, round to 20, or 14 round to 10?

Look up ceil and floor.

Author:  Cervantes [ Fri Dec 02, 2005 9:48 pm ]
Post subject:  Re: Alex's Opinion

Pwned wrote:
Dude_man_47 wrote:
Thanks, how do you round to 10. like if you have 19, round to 20, or 14 round to 10?

Look up ceil and floor.

Using ceil() and floor() to round will not work that way. Judging by that quote, round(), and only round(), will suit his needs. floor() would change 19 to 10, not 20. (This is all assuming some division and then later multiplication occured.)

Author:  Mr. T [ Sat Dec 03, 2005 12:02 am ]
Post subject:  Re: Alex's Opinion

Cervantes wrote:
Pwned wrote:
Dude_man_47 wrote:
Thanks, how do you round to 10. like if you have 19, round to 20, or 14 round to 10?

Look up ceil and floor.

Using ceil() and floor() to round will not work that way. Judging by that quote, round(), and only round(), will suit his needs. floor() would change 19 to 10, not 20. (This is all assuming some division and then later multiplication occured.)

Ok, I must have misread what he was trying to do.

Author:  Dude_man_47 [ Fri Dec 09, 2005 12:20 pm ]
Post subject: 

setscreen ("graphics:max;max,nobuttonbar,offscreenonly")
var mx, my, mb : int
var mx1, my1 : int := 0
var com : string := "a"
var yes, yes1 : boolean := false
Draw.FillBox (0, 0, maxx, maxy, white)
drawline (0, 50, maxx, 50, black)
setscreen ("graphics:450;450,nobuttonbar")
var Buttons : array 1 .. 10, 1 .. 4 of int
for i : 1 .. 10
if i = 1 then
Buttons (i, 1) := 0
Buttons (i, 2) := 0
Buttons (i, 3) := 50
Buttons (i, 4) := 50

elsif i < 11 and i not= 1 then
Buttons (i, 1) := (i - 1) * 50
Buttons (i, 2) := 0
Buttons (i, 3) := i * 50
Buttons (i, 4) := 50
end if
end for
function CheckClick (x1, y1, x2, y2 : int) : boolean
var MouseX, MouseY, MouseButton : int
Mouse.Where (MouseX, MouseY, MouseButton)
if MouseButton not= 0 and MouseX > x1 and MouseX < x2 and MouseY > y1 and MouseY < y2 then
result true
else
result false
end if
end CheckClick
proc bucket
loop
mousewhere (mx1, my1, mb)
if mb = 1 then
var back := Pic.New (0, 0, maxx, maxy)
loop
mousewhere (mx, my, mb)
for i : 1 .. 5
if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then
if i = 1 then
com := "line"
elsif i = 2 then
com := "circle"
elsif i = 3 then
com := "box"
elsif i = 4 then
com := "exit"
elsif i = 5 then
com := "eraser"
elsif i := 6 then
com := "bucket"
end if
yes := true
else
yes := false
end if
end for
if mb not= 0 and my < 52 then
yes := true
end if
exit when yes = true
exit when mb = 0
Draw.Fill (mx, my, red, not= red)
View.Update
end loop
Pic.Free (back)
end if
exit when com not= "bucket"
%-------------------------------------------------------
proc eraser
loop
mousewhere (mx1, my1, mb)
if mb = 1 then
var back := Pic.New (0, 0, maxx, maxy)
loop
mousewhere (mx, my, mb)
for i : 1 .. 5
if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then
if i = 1 then
com := "line"
elsif i = 2 then
com := "circle"
elsif i = 3 then
com := "box"
elsif i = 4 then
com := "exit"
elsif i = 5 then
com := "eraser"
end if
yes := true
else
yes := false
end if
end for
if mb not= 0 and my < 52 then
yes := true
end if
exit when yes = true
exit when mb = 0
Draw.FillBox (mx + 20, my + 20, mx, my, white)
View.Update
end loop
Pic.Free (back)
end if
exit when com not= "eraser"
end loop
end eraser

%-------------------------------------------------------------------------------------

proc line
loop
mousewhere (mx1, my1, mb)
if mb = 1 then
var back := Pic.New (0, 0, maxx, maxy)
loop
mousewhere (mx, my, mb)
for i : 1 .. 5
if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then
if i = 1 then
com := "line"
elsif i = 2 then
com := "circle"
elsif i = 3 then
com := "box"
elsif i = 4 then
com := "exit"
elsif i = 5 then
com := "eraser"
end if
yes := true
else
yes := false
end if
end for
if mb not= 0 and my < 52 then
yes := true
end if
exit when yes = true
exit when mb = 0
Pic.Draw (back, 0, 0, picCopy)
Draw.Line (mx1, my1, mx, my, black)
View.Update
end loop
Pic.Free (back)
end if
exit when com not= "line"
end loop
end line
%-----------------------------------------------------------
proc circle
loop
mousewhere (mx1, my1, mb)

if mb = 1 then
var back := Pic.New (0, 0, maxx, maxy)
loop
mousewhere (mx, my, mb)
for i : 1 .. 5
if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then
if i = 1 then
com := "line"
elsif i = 2 then
com := "circle"
elsif i = 3 then
com := "box"
elsif i = 4 then
com := "exit"
elsif i = 5 then
com := "eraser"
end if
yes := true
else
yes := false
end if
end for
if mb not= 0 and my < 52 then
yes := true
end if
if mb not= 0 and my1 < 52 then
yes := true
end if
exit when yes = true
exit when mb = 0
Pic.Draw (back, 0, 0, picCopy)
Draw.Oval ((mx - mx1) div 2 + mx1, (my1 - my) div 2 + my, (mx - mx1) div 2, (my1 - my) div 2, black)
View.Update
end loop
Pic.Free (back)
end if
exit when com not= "circle"
end loop
end circle
%--------------------------------------------------------------------------
proc box
loop
mousewhere (mx1, my1, mb)
if mb = 1 then
var back := Pic.New (0, 0, maxx, maxy)
loop
mousewhere (mx, my, mb)
for i : 1 .. 5
if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then
if i = 1 then
com := "line"
elsif i = 2 then
com := "circle"
elsif i = 3 then
com := "box"
elsif i = 4 then
com := "exit"
elsif i = 5 then
com := "eraser"
end if
yes := true
else
yes := false
end if
end for
if mb not= 0 and my < 52 then
yes := true
end if
exit when yes = true
exit when mb = 0
Pic.Draw (back, 0, 0, picCopy)
Draw.Box (mx1, my1, mx, my, black)
View.Update
end loop
Pic.Free (back)
end if
exit when com not= "box"
end loop
end box
%----------------------------------------------------------------------------

/*
loop
loop
if CheckClick (Buttons (1, 1), Buttons (1, 2), Buttons (1, 3), Buttons (1, 4)) then
com := "line"
else
Draw.FillBox (Buttons (1, 1), Buttons (1, 2), Buttons (1, 3), Buttons (1, 4), red)
drawline (0, 50, maxx, 50, black)
end if
if CheckClick (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4)) then
com := "circle"
else
Draw.FillBox (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4), green)
end if
if CheckClick (Buttons (3, 1), Buttons (3, 2), Buttons (3, 3), Buttons (3, 4)) then
com := "box"
else
Draw.FillBox (Buttons (3, 1), Buttons (3, 2), Buttons (3, 3), Buttons (3, 4), blue)
end if
if CheckClick (Buttons (5, 1), Buttons (5, 2), Buttons (5, 3), Buttons (5, 4)) then
com := "eraser"
else
Draw.FillBox (Buttons (5, 1), Buttons (5, 2), Buttons (5, 3), Buttons (5, 4), purple)
end if
if CheckClick (Buttons (4, 1), Buttons (4, 2), Buttons (4, 3), Buttons (4, 4)) then
com := "exit"
else
Draw.FillBox (Buttons (4, 1), Buttons (4, 2), Buttons (4, 3), Buttons (4, 4), black)
end if
exit when com not= "a"
end loop
loop
if com = "line" then
line
elsif com = "circle" then
circle
elsif com = "box" then
box
exit when com not= "box"
elsif com = "eraser" then
eraser
exit when com not= "eraser"
elsif com = "exit" then
yes1 := true
end if
exit when yes1 = true

end loop
exit when yes1 = true
end loop*/
loop
if CheckClick (Buttons (1, 1), Buttons (1, 2), Buttons (1, 3), Buttons (1, 4)) then
com := "line"
else
Draw.FillBox (Buttons (1, 1), Buttons (1, 2), Buttons (1, 3), Buttons (1, 4), red)
drawline (0, 50, maxx, 50, black)
end if
if CheckClick (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4)) then
com := "circle"
else
Draw.FillBox (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4), green)
end if
if CheckClick (Buttons (6, 1), Buttons (6, 2), Buttons (6, 3), Buttons (6, 4)) then
com := "bucket"
else
Draw.FillBox (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4), yellow)
end if
if CheckClick (Buttons (3, 1), Buttons (3, 2), Buttons (3, 3), Buttons (3, 4)) then
com := "box"
else
Draw.FillBox (Buttons (3, 1), Buttons (3, 2), Buttons (3, 3), Buttons (3, 4), blue)
end if
if CheckClick (Buttons (5, 1), Buttons (5, 2), Buttons (5, 3), Buttons (5, 4)) then
com := "eraser"
else
Draw.FillBox (Buttons (5, 1), Buttons (5, 2), Buttons (5, 3), Buttons (5, 4), purple)
end if
if CheckClick (Buttons (4, 1), Buttons (4, 2), Buttons (4, 3), Buttons (4, 4)) then
com := "exit"
else
Draw.FillBox (Buttons (4, 1), Buttons (4, 2), Buttons (4, 3), Buttons (4, 4), black)
end if
%if click on colours then
%drawColour := whatdotcolour(mx,my)
mousewhere (mx, my, mb)
View.ClipSet (0, 50, maxx, maxy)
if my > 51 then
if com = "line" then
line
elsif com = "circle" then
circle
elsif com = "box" then
box
elsif com = "eraser" then
eraser
elsif com = "exit" then
yes1 := true
elsif com = "bucket"
bucket
end if
end if
exit when yes1 = true
end loop
end loop


: