Computer Science Canada

Help Needed Related to Randomizing Pictures

Author:  glitchy_gal12 [ Sun May 28, 2006 3:07 pm ]
Post subject:  Help Needed Related to Randomizing Pictures

Ok, I have to create this slot machine program, from which I have to randomize 4 different shapes into 3 seperate square boxes on the same screen, and that the program's purpose is for the user to keep pushing a button until all three slot boxes have 3 identical shapes. The shapes that I am using are a star, an oval, a square, and a maple leaf. The program I'm using is Turing. I'm kind of struggling with this randomizing stuff. If you can, please help me with producing the code Sad

Author:  Cervantes [ Sun May 28, 2006 3:28 pm ]
Post subject: 

You've got 4 possible shapes to produce on screen one. You can pick a random number between 1 and 4 using
code:
Rand.Int (1, 4)

Author:  glitchy_gal12 [ Sun May 28, 2006 3:37 pm ]
Post subject: 

Cervantes wrote:
You've got 4 possible shapes to produce on screen one. You can pick a random number between 1 and 4 using
code:
Rand.Int (1, 4)


Thanks for replying Smile Thanks for the tip, but out of curiousity, is the answer to my problem within to the following thread? http://www.compsci.ca/v2/viewtopic.php?t=12451&highlight=randomize

Author:  Cervantes [ Sun May 28, 2006 3:58 pm ]
Post subject: 

Maybe.

As for drawing your actual picture, the easiest thing to do would be to create an array of procedures.

code:

var outcome : array 1 .. 4 of procedure p (x1, y1, x2, y2 : int)

procedure box (x1, y1, x2, y2 : int)
    Draw.FillBox (x1, y1, x2, y2, black)
end box
% Do the same for the other shapes

outcome (1) := box
% Do the same for the other shapes

outcome (Rand.Int (1, 4)) (100, 100, 200, 200)


Not sure I've got the syntax right, here. Can't test it at the moment. Try playing around with it if it's not right, then post back here with the syntax error.

Author:  HellblazerX [ Sun May 28, 2006 4:09 pm ]
Post subject: 

Cervantes wrote:
an array of procedures.

What?!!!! Shocked Wow, never knew that. Does this only apply to Turing, or does this ability extend to other languages?

Author:  Cervantes [ Sun May 28, 2006 4:17 pm ]
Post subject: 

Yes, it extends to other languages. It extends especially well to functional languages (a procedure is just a function to a language that doesn't have a defined unit (nil) value, to paraphrase wtd).

For an in-depth explanation of this phenomenon, see Turing as a Functional Programming Lanugage.

Author:  glitchy_gal12 [ Sun May 28, 2006 4:29 pm ]
Post subject: 

Cervantes wrote:
Maybe.

As for drawing your actual picture, the easiest thing to do would be to create an array of procedures.

code:

var outcome : array 1 .. 4 of procedure p (x1, y1, x2, y2 : int)

procedure box (x1, y1, x2, y2 : int)
    Draw.FillBox (x1, y1, x2, y2, black)
end box
% Do the same for the other shapes

outcome (1) := box
% Do the same for the other shapes

outcome (Rand.Int (1, 4)) (100, 100, 200, 200)


Not sure I've got the syntax right, here. Can't test it at the moment. Try playing around with it if it's not right, then post back here with the syntax error.


Ok, I found no syntax errors (Horray!). Very Happy However, now that I've figured out how to randomize the outcome once, how would I display 2 more seperate random outcomes on the same screen, in addition to my first outcome display? Confused Here's the code I have so far:

code:
var outcome : array 1 .. 4 of procedure p (x1, y1, x2, y2 : int)

var outcome2 : array 1..4 of procedure p (x1,y1,x2,y2:int)


procedure box (x1, y1, x2, y2 : int)
    Draw.FillBox (120, 280, 180, 340, black)
end box

procedure star (x1,y1,x2,y2:int)
    Draw.FillStar (120,280,180,340,black)
end star

procedure oval (x1,y1,x2,y2:int)
    Draw.FillOval (150,310,30,30,black)
end oval

procedure mapleleaf (x1,y1,x2,y2:int)
    Draw.FillMapleLeaf (120,280,180,340,black)
end mapleleaf




outcome (1) := box
outcome (2) := star
outcome (3) := oval
outcome (4) := mapleleaf


outcome (Rand.Int (1, 4)) (100, 100, 200, 200)
[/code]

Author:  Cervantes [ Sun May 28, 2006 4:33 pm ]
Post subject: 

code:

for i : 1 .. 3
    outcome (Rand.Int (1, 4)) (100 * i, 100, 100 * i + 75, 175)
end for

Author:  glitchy_gal12 [ Sun May 28, 2006 4:42 pm ]
Post subject: 

Cervantes wrote:
code:

for i : 1 .. 3
    outcome (Rand.Int (1, 4)) (100 * i, 100, 100 * i + 75, 175)
end for


Would that code cause the shapes to overlap each other? I tried testing it onto Turing, and the shapes ended up overlapping. Is it possible for the three seperate outcomes to be in spaced out positions so that the different outcomes can be viewed more easily? By the way, thanks a million for helping me. Smile

Author:  Cervantes [ Sun May 28, 2006 5:02 pm ]
Post subject: 

They're overlapping because you hardcoded the values into the box, star, oval, and mapleleaf procedures. Use the parameters.

Cervantes wrote:

code:
procedure box (x1, y1, x2, y2 : int)
    Draw.FillBox (x1, y1, x2, y2, black)
end box


Author:  glitchy_gal12 [ Sun May 28, 2006 5:12 pm ]
Post subject: 

Cervantes wrote:
They're overlapping because you hardcoded the values into the box, star, oval, and mapleleaf procedures. Use the parameters.

Cervantes wrote:

code:
procedure box (x1, y1, x2, y2 : int)
    Draw.FillBox (x1, y1, x2, y2, black)
end box



Oh ok. I get it now. Once again. thanks so much for your time! Very Happy Really appreciate it.

Author:  Cervantes [ Sun May 28, 2006 6:16 pm ]
Post subject: 

glitchy_gal12 wrote:
Once again. thanks so much for your time! Very Happy Really appreciate it.


It was my pleasure. Smile


: