Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-20-2010, 02:33 PM   #1
bafmagnet
Registered User
 
Join Date: Feb 2010
Location: Johannesburg, South Africa
Posts: 13
Default Compare random numbers

hi. I am making a "scratch and win" game that generates 9 icons and the player gets a prize if he gets 3 identical ones; there are 10 prizes. so i have made 9 instances of a movie clip with 10 frames, a different icon on each frame, each clip is sent to a frame with a random number . how do i test if there are 3 matching icons? i can't figure out the maths involved. this is what i have so far:

stop();
var rannum:Number = Math.round(Math.random()*10);
var rannum2:Number = Math.round(Math.random()*10);
var rannum3:Number = Math.round(Math.random()*10);
var rannum4:Number = Math.round(Math.random()*10);
var rannum5:Number = Math.round(Math.random()*10);
var rannum6:Number = Math.round(Math.random()*10);
var rannum7:Number = Math.round(Math.random()*10);
var rannum8:Number = Math.round(Math.random()*10);
var rannum9:Number = Math.round(Math.random()*10);
//
var mouseclick:Number = 0;
var mask_mc:Sprite = new Sprite();
icon_string.mask = mask_mc;
addChild(mask_mc);
//
stage.addEventListener(Event.ENTER_FRAME, start1);
function start1(e:Event):void {
icon_string.icon_mc_1.gotoAndStop(rannum);
icon_string.icon_mc_2.gotoAndStop(rannum2);
icon_string.icon_mc_3.gotoAndStop(rannum3);
icon_string.icon_mc_4.gotoAndStop(rannum4);
icon_string.icon_mc_5.gotoAndStop(rannum5);
icon_string.icon_mc_6.gotoAndStop(rannum6);
icon_string.icon_mc_7.gotoAndStop(rannum7);
icon_string.icon_mc_8.gotoAndStop(rannum8);
icon_string.icon_mc_9.gotoAndStop(rannum9);
stage.removeEventListener(Event.ENTER_FRAME, start1);
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseD);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseM);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseU);
function mouseD(event:MouseEvent):void {
mouseclick = 1;
}
function mouseM(event:MouseEvent):void {
if (mouseclick == 1) {
mask_mc.graphics.beginFill(0x000000);
mask_mc.graphics.drawEllipse(mouseX, mouseY, 40, 40);
mask_mc.graphics.endFill();
}
}
function mouseU(event:MouseEvent):void {
mouseclick = 0;
}
claimbtn.buttonMode = true;
claimbtn.addEventListener(MouseEvent.MOUSE_UP, checkticket);
function checkticket(event:MouseEvent):void {
// this is the part i need
}
bafmagnet is offline   Reply With Quote
Old 02-20-2010, 05:53 PM   #2
madgett
is my last name...
 
Join Date: May 2004
Posts: 1,051
Default

If you are just checking to see if 3 random numbers showed up as equal you can put them all in an array, sort the array numerically and find the consecutive matches.

Add this code:
ActionScript Code:
var rannumArr:Array = new Array(rannum, rannum2, rannum3, rannum4, rannum5, rannum6, rannum7, rannum8, rannum9); rannumArr.sort(Array.NUMERIC); var prevnum:int = -1; var match:uint = 0; for each(var n:Number in rannumArr) {     if (n != prevnum) {         match = 1;         prevnum = n;     }     else if (n == prevnum) match++; } if (match >= 3) trace("There are more than 3 matching icons!");
__________________
Anything is possible with Flash, it's just a matter of inventing the possibilities
Certified Swfwizard
madgett is offline   Reply With Quote
Old 02-20-2010, 06:00 PM   #3
svenjoypro
DontJustEnjoyIt,SvEnjoyIt
 
svenjoypro's Avatar
 
Join Date: Sep 2005
Location: California
Posts: 1,389
Default

I would do something like this:

ActionScript Code:
stop(); var randomArray:Array = new Array(); for (var i=0; i<10; i++) {      randomArray.push(Math.round(Math.random()*10)); } /* var rannum:Number = Math.round(Math.random()*10); var rannum2:Number = Math.round(Math.random()*10); var rannum3:Number = Math.round(Math.random()*10); var rannum4:Number = Math.round(Math.random()*10); var rannum5:Number = Math.round(Math.random()*10); var rannum6:Number = Math.round(Math.random()*10); var rannum7:Number = Math.round(Math.random()*10); var rannum8:Number = Math.round(Math.random()*10); var rannum9:Number = Math.round(Math.random()*10); */ // var mouseclick:Number = 0; var mask_mc:Sprite = new Sprite(); icon_string.mask = mask_mc; addChild(mask_mc); // stage.addEventListener(Event.ENTER_FRAME, start1); function start1(e:Event):void { for (var i=0; i<randomArray.length; i++) { icon_string["icon_mc_"+i].gotoAndStop(randomArray[i]); } /* icon_string.icon_mc_1.gotoAndStop(rannum); icon_string.icon_mc_2.gotoAndStop(rannum2); icon_string.icon_mc_3.gotoAndStop(rannum3); icon_string.icon_mc_4.gotoAndStop(rannum4); icon_string.icon_mc_5.gotoAndStop(rannum5); icon_string.icon_mc_6.gotoAndStop(rannum6); icon_string.icon_mc_7.gotoAndStop(rannum7); icon_string.icon_mc_8.gotoAndStop(rannum8); icon_string.icon_mc_9.gotoAndStop(rannum9); */ stage.removeEventListener(Event.ENTER_FRAME, start1); } stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseD); stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseM); stage.addEventListener(MouseEvent.MOUSE_UP, mouseU); function mouseD(event:MouseEvent):void { mouseclick = 1; } function mouseM(event:MouseEvent):void { if (mouseclick == 1) { mask_mc.graphics.beginFill(0x000000); mask_mc.graphics.drawEllipse(mouseX, mouseY, 40, 40); mask_mc.graphics.endFill(); } } function mouseU(event:MouseEvent):void { mouseclick = 0; } claimbtn.buttonMode = true; claimbtn.addEventListener(MouseEvent.MOUSE_UP, checkticket); function checkticket(event:MouseEvent):void {     randomArray.sort(Array.NUMERIC);     for (var i=0; i<randomArray.length; i++) {         if (randomArray[i] == randomArray[i+1]) {               if (randomArray[i] == randomArray[i+2]) {                    trace("WIN");               }               else {                     i++;               }         }     } }
First the randomArray is sorted, then we loop through the values to check if there are atleast three of the same symbols.
Here is the logic of my for loop:
check if the first and second entries are correct, if so then check if the first and third are correct, if so then there are atleast three of the same. If the first two are the same, but the third is different, then increase i by one (and the for loop increases i by one) to skip the second entry, because the second is equal to the first and there aren't three of those so we don't care.
I didn't actually try this, so there may be errors, I'm not sure, but it makes sense to me.

Last edited by svenjoypro; 02-20-2010 at 06:03 PM.
svenjoypro is offline   Reply With Quote
Old 02-21-2010, 10:58 AM   #4
bafmagnet
Registered User
 
Join Date: Feb 2010
Location: Johannesburg, South Africa
Posts: 13
Default

Thank you. The first method doesn't seem to find any matches. the second works great but it gives the error: TypeError: Error #1010: A term is undefined and has no properties. at scratchtest4_fla::MainTimeline/start1(). if I move the removeEventListener outside the start1 function the error goes away but obviously the function keeps repeating. I can't understand it.

the script now looks like this:

ActionScript Code:
stop(); var randomArray:Array = new Array(); for (var i=0; i<10; i++) {      randomArray.push(Math.round(Math.random()*10)); } stage.addEventListener(Event.ENTER_FRAME, start1); function start1(e:Event):void {     for (var i=0; i<randomArray.length; i++) {     icon_string["icon_mc_"+i].gotoAndStop(randomArray[i]);     } stage.removeEventListener(Event.ENTER_FRAME, start1); } // var mouseclick:Number = 0; var mask_mc:Sprite = new Sprite(); icon_string.mask = mask_mc; addChild(mask_mc); // stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseD); stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseM); stage.addEventListener(MouseEvent.MOUSE_UP, mouseU); function mouseD(event:MouseEvent):void {     mouseclick = 1; } function mouseM(event:MouseEvent):void {     if (mouseclick == 1) {         mask_mc.graphics.beginFill(0x000000);         mask_mc.graphics.drawEllipse(mouseX, mouseY, 40, 40);         mask_mc.graphics.endFill();     } } function mouseU(event:MouseEvent):void {     mouseclick = 0; } claimbtn.buttonMode = true; claimbtn.addEventListener(MouseEvent.MOUSE_UP, checkticket); function checkticket(event:MouseEvent):void {     randomArray.sort(Array.NUMERIC);     for (var i=0; i<randomArray.length; i++) {         if (randomArray[i] == randomArray[i+1]) {               if (randomArray[i] == randomArray[i+2]) {                    trace("WIN");               }               else {                     i++;               }         }     } }
bafmagnet is offline   Reply With Quote
Old 02-21-2010, 06:07 PM   #5
madgett
is my last name...
 
Join Date: May 2004
Posts: 1,051
Default

There was a bug in my code sorry:
ActionScript Code:
var rannumArr:Array = new Array(rannum, rannum2, rannum3, rannum4, rannum5, rannum6, rannum7, rannum8, rannum9); rannumArr.sort(Array.NUMERIC); var prevnum:int = -1; var match:uint = 0; for each(var n:Number in rannumArr) {     if (n != prevnum) {         match = 1;         prevnum = n;     }     else if (n == prevnum) {         match++;         if (match >= 3)             break;     } } if (match >= 3) trace("There are 3 or more matching icons on number: " + prevnum);
__________________
Anything is possible with Flash, it's just a matter of inventing the possibilities
Certified Swfwizard
madgett is offline   Reply With Quote
Old 02-21-2010, 06:47 PM   #6
svenjoypro
DontJustEnjoyIt,SvEnjoyIt
 
svenjoypro's Avatar
 
Join Date: Sep 2005
Location: California
Posts: 1,389
Default

To me an event listener EnterFrame to be run once doesn't make sense, just make a normal function and call it.
ActionScript Code:
stop(); var randomArray:Array = new Array(); for (var i=0; i<10; i++) {      randomArray.push(Math.round(Math.random()*10)); } start1(); //CALL START1() var mouseclick:Number = 0; var mask_mc:Sprite = new Sprite(); icon_string.mask = mask_mc; addChild(mask_mc); // //stage.addEventListener(Event.ENTER_FRAME, start1); function start1():void { for (var i=0; i<randomArray.length; i++) { icon_string["icon_mc_"+i].gotoAndStop(randomArray[i]); } //stage.removeEventListener(Event.ENTER_FRAME, start1); } stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseD); stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseM); stage.addEventListener(MouseEvent.MOUSE_UP, mouseU); function mouseD(event:MouseEvent):void { mouseclick = 1; } function mouseM(event:MouseEvent):void { if (mouseclick == 1) { mask_mc.graphics.beginFill(0x000000); mask_mc.graphics.drawEllipse(mouseX, mouseY, 40, 40); mask_mc.graphics.endFill(); } } function mouseU(event:MouseEvent):void { mouseclick = 0; } claimbtn.buttonMode = true; claimbtn.addEventListener(MouseEvent.MOUSE_UP, checkticket); function checkticket(event:MouseEvent):void {     randomArray.sort(Array.NUMERIC);     for (var i=0; i<randomArray.length; i++) {         if (randomArray[i] == randomArray[i+1]) {               if (randomArray[i] == randomArray[i+2]) {                    trace("WIN");               }               else {                     i++;               }         }     } }

Last edited by svenjoypro; 02-21-2010 at 06:52 PM.
svenjoypro is offline   Reply With Quote
Old 02-22-2010, 01:58 PM   #7
bafmagnet
Registered User
 
Join Date: Feb 2010
Location: Johannesburg, South Africa
Posts: 13
Default

thank you both very much, working perfectly now.
bafmagnet is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:56 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.