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

Username:   Password: 
 RegisterRegister   
 Help Needed!!!
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dude_man_47




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




PostPosted: Fri Dec 02, 2005 8:44 am   Post subject: (No 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.
Dude_man_47




PostPosted: Fri Dec 02, 2005 9:10 am   Post subject: (No 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
Tony




PostPosted: Fri Dec 02, 2005 9:47 am   Post subject: (No 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
do_pete




PostPosted: Fri Dec 02, 2005 9:48 am   Post subject: (No 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
Cervantes




PostPosted: Fri Dec 02, 2005 10:42 am   Post subject: (No 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.
Dude_man_47




PostPosted: Fri Dec 02, 2005 11:08 am   Post subject: (No 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.
Dude_man_47




PostPosted: Fri Dec 02, 2005 11:42 am   Post subject: (No 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!!!
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Dec 02, 2005 11:48 am   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
do_pete




PostPosted: Fri Dec 02, 2005 11:52 am   Post subject: (No 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
Dude_man_47




PostPosted: Fri Dec 02, 2005 1:08 pm   Post subject: (No 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?
do_pete




PostPosted: Fri Dec 02, 2005 1:15 pm   Post subject: (No subject)

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




PostPosted: Fri Dec 02, 2005 1:26 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dude_man_47




PostPosted: Fri Dec 02, 2005 1:33 pm   Post subject: (No subject)

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




PostPosted: Fri Dec 02, 2005 1:45 pm   Post subject: (No subject)

try this:
code:
value := round (value / 10) * 10
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 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: