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

Username:   Password: 
 RegisterRegister   
 Help on how to randomize flashing of objects
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
unicorn-ellie




PostPosted: Sun Dec 29, 2013 9:23 pm   Post subject: Help on how to randomize flashing of objects

Hi, I'm an ICS student and I just have some questions.

Firstly, how would you make a group of objects, such as circles, flash randomly? (Flash, as in appear on the screen, then disappear for a second, then re-appear.)

Also, how do you make the groups of circles have random colours for each, while simultaneously flashing one circle and then the next?

I know how to do the random colours for the circles, but how do you do that while setting the code to make the circles flash randomly?

This is all I got so far:
code:

var colours : array 1 .. 9 of int := init (red, black, blue, yellow, green, magenta, gray, purple, brown)
%you can choose your own colors


for x : 1..2
    drawfilloval (200, 200, 50, 50, 0)
    delay (1000)
    drawfilloval (200, 200, 50, 50, Rand.Int (1, colours (x)))
    delay (1000)

    drawfilloval (450, 200, 50, 50, 0)
    delay (1000)
    drawfilloval (450, 200, 50, 50, Rand.Int (1, colours (x)))
    delay (1000)

    drawfilloval (320, 300, 50, 50, 0)
    delay (1000)
    drawfilloval (320, 300, 50, 50, Rand.Int (1, colours (x)))
    delay (1000)

    drawfilloval (320, 100, 50, 50, 0)
    delay (1000)
    drawfilloval (320, 100, 50, 50, Rand.Int (1, colours (x)))
    delay (1000)
end for


As you can see above, I made the colours for the circles random, but I can't make them flash randomly. (In the code, the flashes are determined by me)

It would be great help if you could tell me how to make the circles flash randomly or provide any input.
Thank you.
Sponsor
Sponsor
Sponsor
sponsor
np_123




PostPosted: Sun Dec 29, 2013 9:48 pm   Post subject: RE:Help on how to randomize flashing of objects

So you want the circles to flash in a random order...

My advice...if you don't know already, learn about procedures. Have four procedures, one to draw each circle
From what I know, I would say an array of procedures would do the trick since you would be able to call the procedures using random integer to select the procedures as an element of the array

Someone else might have a better idea, but procedures will be very useful either way
unicorn-ellie




PostPosted: Sun Dec 29, 2013 10:47 pm   Post subject: Re: Help on how to randomize flashing of objects

Oh, thank you for your help np_123.

I think it's just that I'm not familiar with arrays/not taught them yet.
So, I don't really know how to do an array of procedures.

So far I did this:
code:

var flashes : array 1 .. 4 of int := init (circle1, circle2, circle3, circle4)
var circle1 := flashes (Rand.Int (1, 4))
var circle2 := flashes (Rand.Int (1, 4))
var circle3 := flashes (Rand.Int (1, 4))
var circle4 := flashes (Rand.Int (1, 4))

procedure circle1
    for x : 1 .. 2
        drawfilloval (200, 200, 50, 50, 0)
        delay (1000)
        drawfilloval (200, 200, 50, 50, colour1)
        delay (1000)
    end for
end circle1

procedure circle2
    for x : 1 .. 2
        drawfilloval (450, 200, 50, 50, 0)
        delay (1000)
        drawfilloval (450, 200, 50, 50, colour2)
        delay (1000)
    end for
end circle1

procedure circle3
    for x : 1 .. 2
        drawfilloval (320, 300, 50, 50, 0)
        delay (1000)
        drawfilloval (320, 300, 50, 50, colour3)
        delay (1000)
    end for
end circle3

procedure circle4
    for x : 1 .. 2
        drawfilloval (320, 100, 50, 50, 0)
        delay (1000)
        drawfilloval (320, 100, 50, 50, colour4)
        delay (1000)
    end for
end circle4


But, it won't run because I don't know how to do arrays of procedures.
So far, I'm learning how to do arrays myself and I'm okay with everything else, but if you could just tell me how to do it with procedures, that would be great.
Raknarg




PostPosted: Sun Dec 29, 2013 11:16 pm   Post subject: RE:Help on how to randomize flashing of objects

Procedures are actually data types you can use here. However, one thing I should mention is that Turing does everything in order. If you call circle1 and circle1 does not yet exist, you'll get an errors. So, in Turing, usually all your functions and procedures come first.

Next: This is how you make an array of procedures:
Turing:

procedure hi1
    put "hi1"
end hi1
procedure hi2
    put "hi2"
end hi2
procedure hi3
    put "hi3"
end hi3

var hi : array 1 .. 3 of proc x
hi (1) := hi1
hi (2) := hi2
hi (3) := hi3


Also note: Unless you have a very good good reason for it, your main program should only have 1 delay in it.
np_123




PostPosted: Sun Dec 29, 2013 11:19 pm   Post subject: RE:Help on how to randomize flashing of objects

Okay, to do an array of procedures you would use the following:

Turing:

var circle : array 1 .. 4 of proc x
% x can be anything, it's just an identifier

%Declare procedures here

circle (1) := circle1
circle (2) := circle2
circle (3) := circle3
circle (4) := circle4

/* To call procedures use circle (Rand.Int (1,4)) if you still need the random flashing */



EDIT: Sorry Raknarg didn't see your post as I was typing. Good explanation
unicorn-ellie




PostPosted: Mon Dec 30, 2013 3:15 pm   Post subject: Re: Help on how to randomize flashing of objects

Hi, thank you for both of your helpful posts, Raknarg and np_123.

It has helped me understand arrays of procedures better.
However, is there a way to ensure that all 4 circles flash and they flash only once?

In the code, if you run it, sometimes, it will flash all 4 once, but other times only 2 or 3 circles will appear, but flash the number of times it would take 4 circles to flash once.

Is there a way to fix this?

This is how I coded it:
code:

var colours : array 1 .. 9 of int := init (red, black, blue, yellow, green, magenta, gray, purple, brown)
var colour1 := colours (Rand.Int (1, 9))
var colour2 := colours (Rand.Int (1, 9))
var colour3 := colours (Rand.Int (1, 9))
var colour4 := colours (Rand.Int (1, 9))

procedure circle1
    for x : 1 .. 2
        drawfilloval (200, 200, 50, 50, 0)
        delay (1000)
        drawfilloval (200, 200, 50, 50, colour1)
        delay (1000)
    end for
end circle1

procedure circle2
    for x : 1 .. 2
        drawfilloval (450, 200, 50, 50, 0)
        delay (1000)
        drawfilloval (450, 200, 50, 50, colour2)
        delay (1000)
    end for
end circle2

procedure circle3
    for x : 1 .. 2
        drawfilloval (320, 300, 50, 50, 0)
        delay (1000)
        drawfilloval (320, 300, 50, 50, colour3)
        delay (1000)
    end for
end circle3

procedure circle4
    for x : 1 .. 2
        drawfilloval (320, 100, 50, 50, 0)
        delay (1000)
        drawfilloval (320, 100, 50, 50, colour4)
        delay (1000)
    end for
end circle4

var circle : array 1 .. 4 of proc x
circle (1) := circle1
circle (2) := circle2
circle (3) := circle3
circle (4) := circle4

circle (Rand.Int (1, 4))
circle (Rand.Int (1, 4))
circle (Rand.Int (1, 4))
circle (Rand.Int (1, 4))


If you could tell me a way to ensure that all 4 circles flash once, that would be great.
Sorry for all this inconvenience, I know I'm not the best at operating Turing.
np_123




PostPosted: Mon Dec 30, 2013 3:41 pm   Post subject: RE:Help on how to randomize flashing of objects

The way I would do it...

Turing:


var check : array 1 .. 4 of boolean := init (false, false, false, false)
var number : int


loop
    var count : int := 0

    number := Rand.Int (1, 4)

    if check (number) = false then
        circle (number)
        check (number) := true
    end if

    for x : 1 .. 4
        if check (x) = true then
            count += 1
        end if
    end for

    exit when count = 4

end loop




The general idea is to create Boolean variables to check whether or not the circle has flashed or not

EDIT: Using Rand.Int may cause the loop to run several times without doing anything, so it could be improved by having it check that if there is only one circle that hasn't flashed, it will make that one flash - using if statements
There wouldn't really be any visible difference, unless you're really unlucky with Rand.Int

EDIT: I thought that it auto-indented when you use code/syntax tags? I had to indent in Turing and then paste the indented version...
Raknarg




PostPosted: Mon Dec 30, 2013 5:12 pm   Post subject: RE:Help on how to randomize flashing of objects

That's fine if you only have a few items, it's not so great if you have a huge list of items. However, that will do for now
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Mon Dec 30, 2013 6:38 pm   Post subject: Re: Help on how to randomize flashing of objects

I feel that an array of procedures is overkill (by a lot).

Since you want to flash every circle once (in a random order) you might want to consider an array that contains the locations and colours of the circles (using records) and then either shuffle the array.

Then you just go through your shuffled array and have every circle flash once. This way you only need one procedure that takes the position and colour of the circle as arguments and flashes the circle on screen.
Raknarg




PostPosted: Mon Dec 30, 2013 10:45 pm   Post subject: RE:Help on how to randomize flashing of objects

Great point. You should consider what you actually need to shuffle. All you need are positions and colours. Do they really need separate procedures when they all basically do the same thing?
np_123




PostPosted: Mon Dec 30, 2013 11:07 pm   Post subject: RE:Help on how to randomize flashing of objects

In order to use one procedure, you could use three arrays - one each for x-coordinate, y-coordinate, and finally one for the colours.
Each of these could be passes as parameters/arguments to the procedure.

The arrays would be used to select the 3 traits using only one number each time to reference them

I use arrays all the time for stuff like this, but if there's a better way, I'd definitely like to hear it too, as it would also help improve my understanding.
Raknarg




PostPosted: Mon Dec 30, 2013 11:39 pm   Post subject: RE:Help on how to randomize flashing of objects

There's a better way. use one array and look into records as dreadnought suggested. He linked it for you.
np_123




PostPosted: Tue Dec 31, 2013 12:14 am   Post subject: RE:Help on how to randomize flashing of objects

Okay, I'll look into that. Thanks
np_123




PostPosted: Tue Dec 31, 2013 1:42 am   Post subject: RE:Help on how to randomize flashing of objects

So basically, with a record you:
-create your record type
-create fields within the record
-create a variable of your defined record type
-store values within the record using the general notation 'variable.field'

(Feel free to correct me if I said something incorrect)

It seems to be making sense now why a record would be the better way to go instead of the arrays.

So, to apply it to the situation in question, would it then be something like:
creating the record type 'circle';
fields x,y, and colour;
Then creating an array of circle to store the location and colours??
Raknarg




PostPosted: Tue Dec 31, 2013 11:28 am   Post subject: RE:Help on how to randomize flashing of objects

Sure, you could do that. This way all your info is organized in a way that makes sense. This is also the first step into understanding objects, which are as useful as arrays in programming
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  [ 15 Posts ]
Jump to:   


Style:  
Search: