;

PDA

Click to See Complete Forum and Search --> : Array game


marinebro0306
09-09-2005, 10:29 PM
I am trying to make a computer version of CatchPhrase, which is also like Password. When I press a button, I want a random word from an array to display in a dynamic text box. It's probably very simple, but I simply can't find the explanation for this.

I know how to make an array, but how do I make it so a random word is shown at the press of the button?
This could also be comparible to an 8-ball game, where when you press, a random word/phrase pops up.

blanius
09-10-2005, 12:55 AM
in first frame

myWords=new Array("one","two","three")
totalWords=myWords.length


function randRange(min,max) {
randomNum= Math.floor(Math.random()*(max-min))+min;
return randomNum
}


then in button or other event put

on (release){
currentWord=myWords[randRange(0,totalWords)];
}

RobVos
09-10-2005, 04:51 AM
Hai there,

maybe i think to simple but what about generating an number with the
nr=(Math.random() * 100); function. This wil give you a number between 0 and 100. then you can call the array with i thought Array[nr] and put it in a variable with vartxt1=Array[nr]

is that possible?

greetings Rob

blanius
09-10-2005, 09:37 AM
isn't that basically what I said?

using things like totalWords keeps you from having to rework code when the length of the array changes.

The randRange function I just find usefull in general that way if I want to get a number between say 10-100 I can.

Now if you want each element only used once, like when dealing cards from an array of 52 cards what I'd do is to make a second array and using the random number to fill it with random elements from the first array always checking that it hasn't been used before then you can just pull from the array from the top and remove the array element just like dealing a shuffled deck of cards.

marinebro0306
09-10-2005, 12:30 PM
Now if you want each element only used once, like when dealing cards from an array of 52 cards what I'd do is to make a second array and using the random number to fill it with random elements from the first array always checking that it hasn't been used before then you can just pull from the array from the top and remove the array element just like dealing a shuffled deck of cards.

So...how do I do that?

blanius
09-10-2005, 04:46 PM
Ok took a bit of work to get it going.

try this
http://bretlanius.com/flash/shuffle.html

and source is
http://bretlanius.com/flash/shuffle.fun

you've have to actually shuffle a copy of the original array to be very usefull but this should get you going.

The meat of this is the following prototype function. By using a this method all arrays in your movie can easily be "shuffled"

Array.prototype.shuffle = function() {
len = this.length;
for (var i=0; i < len; i++) {
rand = Math.floor(Math.random()*len);
//swap current index with a random one
temp = this[i];
this[i] = this[rand];
this[rand] = temp;
}
}

marinebro0306
09-13-2005, 11:10 PM
Thanks!