actionscript 3 - AS3 get random unique Characters from Alphabet array -
feel free copy , paste code fla. should work trace vars.
i trying create kids matching game. selects 1 letter alphabet , ask them find letter 3 choices. going randomize 3 letters pick not yet in code.
my issue of time removing array var using "pop" , duplicates , comes out null. doing wrong here?
import flash.events.mouseevent; import flash.display.*; /// array of alphabet var alphabet:array = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]; // arry hold 3 unique letters var randarray:array = new array(); function getrandomelementof(array:array):object { var idx:int=math.floor(math.random() * array.length); // supposed remove letter can't chosen again array.pop() // adds 1 of 3 letters new array randarray.push(array[idx]); return array[idx]; } function testarray(evt:mouseevent){ var 1 = getrandomelementof(alphabet); trace(one); var 2 = getrandomelementof(alphabet); trace(two); var 3 = getrandomelementof(alphabet); trace(three); trace("can find letter " + 1 + "? " + randarray); // resets random array randarray = new array(); // resets letters forto chosen again. alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]; } /// button click stage test vars stage.addeventlistener(mouseevent.click, testarray);
an example shuffler, splicing letters alphabet collection:
var alphabet:vector.<string> = new <string>[ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ]; while (alphabet.length > 0) { var letter:string = alphabet.splice(int(math.random() * alphabet.length), 1)[0]; trace(letter); }
example output:
v, m, f, e, d, u, s, l, x, k, q, h, a, i, w, n, p, y, j, c, t, o, r, g, b, z
applied example, here's reset function reset alphabet collection original state, random letter function remove single letter alphabet collection, , shuffle function randomize alphabet collection:
/** alphabet collection */ var alphabet:vector.<string>; /** reset alphabet */ function reset():void { alphabet = new <string>[ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ]; } /** random letter alphabet */ function getrandomletter():string { return (alphabet.splice(int(math.random() * alphabet.length), 1)[0]); } /** shuffle alphabet collection */ function shufflealphabet():vector.<string> { var alphabetshuffled:vector.<string> = new vector.<string>(); while (alphabet.length > 0) { alphabetshuffled.push(getrandomletter()); } return alphabetshuffled; }
the following pulls random letter alphabet found , display entire alphabet shuffled:
// random letter: reset(); var randomletter:string = getrandomletter(); trace("can find letter: " + randomletter + "?"); // display entire alpha shuffled: reset(); trace(shufflealphabet());
example game output:
can find letter: q?
r,i,u,j,y,d,k,w,t,f,n,g,a,p,x,h,q,l,s,o,c,v,m,z,e,b
can find letter: p?
i,f,c,s,j,p,q,m,d,t,h,x,o,v,w,g,k,a,n,y,l,u,z,r,b,e
can find letter: s?
b,u,o,s,c,n,i,e,w,l,p,q,z,r,a,g,j,k,y,m,t,v,x,d,h,f
Comments
Post a Comment