RE: [Flashcoders] checking combinations
Nice! And I was determined not to post again, but at a certain stage my mental block irresistibly kicked in again: why ingeniously bit-wise encode the combinations just to later decode them for better readabilty? Why not drop this altogether as we won't do any encoding: static var STATE_A:Number = 1; // 1 0 Define the handlers like handlers = {};// or type it/ make it a class handlers[STATE_A] = Delegate.create(this,handleAOnly); // etc. And finally: public function handleUpdate(a:Object,b:Object,c:Object,d:Object) { state = 'STATE_'; if(a.selected){ state += 'A'}; if(b.selected){ state += 'B'}; // etc. handlers[state](); } What's the benefit of encoding/decoding numbers? Please enlighten me! Cheers! -- Andreas Weber motiondraw.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Martin Wood Sent: Thursday, January 26, 2006 11:21 AM To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations to be more complete how i would actually program it is like this : import mx.utils.Delegate; class CombinationHandler { // Declare the values used for each state static var STATE_A:Number = 1; // 1 0 static var STATE_B:Number = 2; // 1 1 static var STATE_C:Number = 4; // 1 2 static var STATE_D:Number = 8; // 1 3 // Either declare the combinations here, or just use // the STATE_A | STATE_B form in the declare handlers function static var COMBINATION_AB:Number = STATE_A | STATE_B; static var COMBINATION_BC:Number = STATE_B | STATE_C; // array of functions to handle particular states // and state combinations private var handlers:Array; public function CombinationHandler() { handlers = new Array(); createHandlers(); } private function createHandlers() { // single case handlers handlers[STATE_A] = Delegate.create(this,handleAOnly); handlers[STATE_B] = Delegate.create(this,handleBOnly); handlers[STATE_C] = Delegate.create(this,handleCOnly); handlers[STATE_D] = Delegate.create(this,handleDOnly); // combination handlers handlers[COMBINATION_AB] = Delegate.create(this,ABhandler); // OR // handlers[STATE_A | STATE_B] = ... } public function handleUpdate(a:Object,b:Object,c:Object,d:Object) { // 'Digitise' the state from the selected flag in a,b,c,d. var state:Number = (a.selected) | (b.selected 1) | (c.selected 2) | (d.selected 3); // call the function handlers[state](); } } I think this way adding and modifying handlers is easy and safe, youre not editing code thats surrounded by support code (like case:, else if etc) so its clear where each response to a particular state is configured. You can leave the declaration of the combinations out of the static declarations at the top of the class like i said and just create the combinations when defining the handler. That way when you add a combination its just one place to edit, not two, but you may have a case for using that information again so the static var could be useful. I dont know :) btw, in all the places you see | (bitwise OR) you can use + because it works out the same, im just used to using the bitwise operators for dealing with bit-based states and state masks. Similary for the shifts () you can change them for their equivalent multiplication. thanks, Martin ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
one reason, you get type checking when you declare the handlers handlers[some string] : contents of 'some string' arent checked handlers[STATE_A | STTE_B] : Compile error : STTE_B is not declared. the other reasons are habit and familiarity :) Im used to seeing and working with code for handling states that uses all that bitwise stuff because of working in other languages where its more common (e.g. c++). If you feel more comfortable with string based solutions thats all good, but like i said, the one point where they differ is that the compiler doesnt check the contents of a string, so a typo can go un-noticed. thanks, Martin Andreas Weber wrote: Nice! And I was determined not to post again, but at a certain stage my mental block irresistibly kicked in again: why ingeniously bit-wise encode the combinations just to later decode them for better readabilty? Why not drop this altogether as we won't do any encoding: static var STATE_A:Number = 1; // 1 0 Define the handlers like handlers = {};// or type it/ make it a class handlers[STATE_A] = Delegate.create(this,handleAOnly); // etc. And finally: public function handleUpdate(a:Object,b:Object,c:Object,d:Object) { state = 'STATE_'; if(a.selected){ state += 'A'}; if(b.selected){ state += 'B'}; // etc. handlers[state](); } What's the benefit of encoding/decoding numbers? Please enlighten me! Cheers! -- Andreas Weber motiondraw.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
Or even shorter: public function handleUpdate(a:Object,b:Object,c:Object,d:Object) { var handlerName:String = 'onUpdate'; if (a.selected){handlerName+= 'A'}; if (b.selected){handlerName+='B'}; // etc. if (this[handlerName]==undefined) { trace(We haven't got a handler called:+handlerName); return; } this[handlerName](); } public function onUpdateA() { // do stuff for A etc. } public function onUpdateAB() { // do stuff for AB etc. } Cheers, Ian On 1/26/06, Andreas Weber [EMAIL PROTECTED] wrote: Nice! And I was determined not to post again, but at a certain stage my mental block irresistibly kicked in again: why ingeniously bit-wise encode the combinations just to later decode them for better readabilty? Why not drop this altogether as we won't do any encoding: static var STATE_A:Number = 1; // 1 0 Define the handlers like handlers = {};// or type it/ make it a class handlers[STATE_A] = Delegate.create(this,handleAOnly); // etc. And finally: public function handleUpdate(a:Object,b:Object,c:Object,d:Object) { state = 'STATE_'; if(a.selected){ state += 'A'}; if(b.selected){ state += 'B'}; // etc. handlers[state](); } What's the benefit of encoding/decoding numbers? Please enlighten me! Cheers! -- Andreas Weber motiondraw.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
This is indeed a pretty cool thread :) - edolecki On 1/26/06, Ian Thomas [EMAIL PROTECTED] wrote: Or even shorter: public function handleUpdate(a:Object,b:Object,c:Object,d:Object) { var handlerName:String = 'onUpdate'; if (a.selected){handlerName+= 'A'}; if (b.selected){handlerName+='B'}; // etc. if (this[handlerName]==undefined) { trace(We haven't got a handler called:+handlerName); return; } this[handlerName](); } public function onUpdateA() { // do stuff for A etc. } public function onUpdateAB() { // do stuff for AB etc. } Cheers, Ian On 1/26/06, Andreas Weber [EMAIL PROTECTED] wrote: Nice! And I was determined not to post again, but at a certain stage my mental block irresistibly kicked in again: why ingeniously bit-wise encode the combinations just to later decode them for better readabilty? Why not drop this altogether as we won't do any encoding: static var STATE_A:Number = 1; // 1 0 Define the handlers like handlers = {};// or type it/ make it a class handlers[STATE_A] = Delegate.create(this,handleAOnly); // etc. And finally: public function handleUpdate(a:Object,b:Object,c:Object,d:Object) { state = 'STATE_'; if(a.selected){ state += 'A'}; if(b.selected){ state += 'B'}; // etc. handlers[state](); } What's the benefit of encoding/decoding numbers? Please enlighten me! Cheers! -- Andreas Weber motiondraw.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] checking combinations
Sounds like you're trying to do something like this, but even still your approach seems overly complex. Maybe a more detailed explaination of what you're trying to do and what combinations are would help. Meanwhile, if I understand you, would something like this work? a = false b = false c = true d = true e = true f = false g = true h = false i = false //etc. myArray = [2,3,6,12,34,54,345,45,76,34] vars = [a,b,c,d,e,f,g,h,i,j] counter = 0; counter_arr = []; function checkVars(){ for(i=0; imyArray.length; i++){ if(this[vars[i]]){ counter_arr.push(counter+=myArray[i]) trace(vars[i]+ in vars array is true.); trace(counter is at: +counter); } } } checkVars(); Jason Merrill | E-Learning Solutions | icfconsulting.com -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of eric dolecki Sent: Wednesday, January 25, 2006 1:03 PM To: Flashcoders mailing list Subject: [Flashcoders] checking combinations I have 4 variables I need to check the states of routinely... and combinations thereof. I am assuming that if I have a counter, and interogate and += them values, I can then check the value of the counter to determine the combinations. psudeo-code: var counter:Number = 0; if ( a ){ counter += 2; } if ( b ){ counter += 3; } if ( c ){ counter += 6; } if ( d ){ counter += 12;} if ( counter == 2 ){ // only a was true } else if ( counter == 3 ){ // only b was true } ... Which is fine, but thats gonna be one honkin' if else statement to catch all the combinations. Is there a better way of doing this that will catch all the possible combinations in an elegant way? - edolecki ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders NOTICE: This message is for the designated recipient only and may contain privileged or confidential information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of this e-mail by you is prohibited. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
i've got to go out now so cant give the full answer i have in my head, but one way of tackling it is to make each state variable's value a power of 2 a = 1 b = 2 c = 4 d = 8 then you know that any combination of them has a unique value. (its basically a 4 bit number) then to handle the dispatch depending on the state combination you can build a function table. functionTable = new Array(); // create a handler for the combination a + b functionTable[a + b] = Delegate.create(this,combinationAB); and when you check the variables just call the function directly from the table : // a,b,c,d are just passed as boolean flags here function handleUpdate(a:boolean,b:boolean,c:boolean,d:boolean) { // taking advantage of a true being 1 in flash // and ideally you would setup these numbers // as static class variables var state = a + (b * 2) + (c * 4) + (d * 8); // call the defined function functionTable[state](); } of course you could do a check first to see if the function is defined and do something like log a warning or whatever is appropriate for your situation. hope that makes sense. martin eric dolecki wrote: I have 4 variables I need to check the states of routinely... and combinations thereof. I am assuming that if I have a counter, and interogate and += them values, I can then check the value of the counter to determine the combinations. psudeo-code: var counter:Number = 0; if ( a ){ counter += 2; } if ( b ){ counter += 3; } if ( c ){ counter += 6; } if ( d ){ counter += 12;} if ( counter == 2 ){ // only a was true } else if ( counter == 3 ){ // only b was true } ... Which is fine, but thats gonna be one honkin' if else statement to catch all the combinations. Is there a better way of doing this that will catch all the possible combinations in an elegant way? - edolecki ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- Martin Wood http://relivethefuture.com/choronzon ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] checking combinations
You can also use: combo=0; if(a){combo+=1000;} if(b){combo+=100;} if(c){combo+=10;} if(d){combo+=1;} Then convert combo to a string and check the character at each position ( charAt(0), charAt(1), etc.) for a one or zero, which will tell you if the associated variable is true or false. -Marc At 10:47 AM 1/25/2006, you wrote: You seem to have something like a 'Round Robin' situation, only more complex as it is not only about pairs, but also about singles/triplets/quadruplets. If each of these possible combinations leads to a different action, the if/else if or switch statement will naturally be very long... To make it quite a bit more readable and thus managable, maybe you could do combo = ''; if ( a ){ combo += 'a'; } if ( b ){ combo += 'b'; } if ( c ){ combo += 'c'; } if ( d ){ combo += 'd'; } switch(combo){ case 'a': // do stuff brek; case 'ab': // do some other stuff break; case 'abc': //etc. } For finding all the 'pairs' see my RoundRobin class below (used to be in the archives, but can't find it anymore) hth -- Andreas Weber motiondraw.com class com.motiondraw.util.RoundRobin{ /* Usage import com.motiondraw.util.RoundRobin numPlayers = 12; // create players players = new Array(); for(var i=0; inumPlayers; i++){ players[i] = String.fromCharCode(65+i); } var rr:RoundRobin = new RoundRobin(players); rr.displaySchedule(); */ public var players:Array; public var schedule:Array; function RoundRobin(players:Array){ init.apply(this, arguments); } function init(players){ this.players = players; var pc = players.slice(0); var n = pc.length; if(n%2){ var odd = true; pc.push('@'); n++; } var nr = n - 1; var r = new Array(); schedule = new Array(); for (var i=0; inr; i++) { r[i] = new Array(); r[i][0] = pc[nr]; for (var j=0; jn-1; j++) { r[i][j+1] = pc[(i+j) % (n-1)]; } } for(var i=0, len=r.length; ilen; i++){ schedule[i] = new Array(); for(var j=0; jnr/2; j++){ if(!(odd j==0)){ schedule[i].push([r[i][j], r[i][nr-j]]); } } } } public function displaySchedule(short:Boolean){ var output = new Array(); for(var i=0, len=schedule.length; ilen; i++){ //trace('--- '+(i+1)+'. round ---'); for(var j=0, len2=schedule[i].length; jlen2; j++){ //trace(' '+j+' '+schedule[i][j][0]+' vs. '+schedule[i][j][1]) output.push(''+schedule[i][j][0]+''+schedule[i][j][1]+''); } } trace(output); trace(output.length); } } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of eric dolecki Sent: Wednesday, January 25, 2006 7:03 PM To: Flashcoders mailing list Subject: [Flashcoders] checking combinations I have 4 variables I need to check the states of routinely... and combinations thereof. I am assuming that if I have a counter, and interogate and += them values, I can then check the value of the counter to determine the combinations. psudeo-code: var counter:Number = 0; if ( a ){ counter += 2; } if ( b ){ counter += 3; } if ( c ){ counter += 6; } if ( d ){ counter += 12;} if ( counter == 2 ){ // only a was true } else if ( counter == 3 ){ // only b was true } ... Which is fine, but thats gonna be one honkin' if else statement to catch all the combinations. Is there a better way of doing this that will catch all the possible combinations in an elegant way? - edolecki ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
Well, lets say you have 4 objects that have selected states fired no events on their own. So there are many different possible combinations for the combined states of those 4 objects. Each possible combination triggers an entirely seperate action. Thats a lot of actions to account for. So its all about detecting the unique combinations, and acting upon them. Perhaps there really is no truly elegant way of doing this... On 1/25/06, Merrill, Jason [EMAIL PROTECTED] wrote: Sounds like you're trying to do something like this, but even still your approach seems overly complex. Maybe a more detailed explaination of what you're trying to do and what combinations are would help. Meanwhile, if I understand you, would something like this work? a = false b = false c = true d = true e = true f = false g = true h = false i = false //etc. myArray = [2,3,6,12,34,54,345,45,76,34] vars = [a,b,c,d,e,f,g,h,i,j] counter = 0; counter_arr = []; function checkVars(){ for(i=0; imyArray.length; i++){ if(this[vars[i]]){ counter_arr.push(counter+=myArray[i]) trace(vars[i]+ in vars array is true.); trace(counter is at: +counter); } } } checkVars(); Jason Merrill | E-Learning Solutions | icfconsulting.com -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of eric dolecki Sent: Wednesday, January 25, 2006 1:03 PM To: Flashcoders mailing list Subject: [Flashcoders] checking combinations I have 4 variables I need to check the states of routinely... and combinations thereof. I am assuming that if I have a counter, and interogate and += them values, I can then check the value of the counter to determine the combinations. psudeo-code: var counter:Number = 0; if ( a ){ counter += 2; } if ( b ){ counter += 3; } if ( c ){ counter += 6; } if ( d ){ counter += 12;} if ( counter == 2 ){ // only a was true } else if ( counter == 3 ){ // only b was true } ... Which is fine, but thats gonna be one honkin' if else statement to catch all the combinations. Is there a better way of doing this that will catch all the possible combinations in an elegant way? - edolecki ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders NOTICE: This message is for the designated recipient only and may contain privileged or confidential information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of this e-mail by you is prohibited. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] checking combinations
Seems to be an ispiring question/problem! Eric - if you'd go with the combo - Strings - these are the 16 possible combinations: ,a,ab,abc,abcd,abd,ac,acd,ad,b,bc,bcd,bd,c,cd,d (first one is an empty string if a b c d are false) I'm not overly proud of how I found the combinations, but sometimes nothing beats brute force :-) arr = ['a','b','c','d']; numElems = arr.length; combos = new Array(); c=0; while(c10){ combo = ''; for(var i=0; inumElems; i++){ if(Math.random() 0.5){ combo += arr[i]; } } inArray = false; for(var i=0, len=combos.length; ilen; i++){ if(combo == combos[i]){ inArray = true; break } } if(!inArray){ combos.push(combo); } c++; } combos.sort(); trace(combos); trace(combos.length); -- Andreas Weber motiondraw.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Martin Wood Sent: Wednesday, January 25, 2006 8:05 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations i've got to go out now so cant give the full answer i have in my head, but one way of tackling it is to make each state variable's value a power of 2 a = 1 b = 2 c = 4 d = 8 then you know that any combination of them has a unique value. (its basically a 4 bit number) then to handle the dispatch depending on the state combination you can build a function table. functionTable = new Array(); // create a handler for the combination a + b functionTable[a + b] = Delegate.create(this,combinationAB); and when you check the variables just call the function directly from the table : // a,b,c,d are just passed as boolean flags here function handleUpdate(a:boolean,b:boolean,c:boolean,d:boolean) { // taking advantage of a true being 1 in flash // and ideally you would setup these numbers // as static class variables var state = a + (b * 2) + (c * 4) + (d * 8); // call the defined function functionTable[state](); } of course you could do a check first to see if the function is defined and do something like log a warning or whatever is appropriate for your situation. hope that makes sense. martin ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
I think which each combination being completely unique, i have no other option than a honkin' switch or if/else. Lots of typing but will work. I can work on making it more readable, but thats about it methinks. On 1/25/06, Andreas Weber [EMAIL PROTECTED] wrote: Seems to be an ispiring question/problem! Eric - if you'd go with the combo - Strings - these are the 16 possible combinations: ,a,ab,abc,abcd,abd,ac,acd,ad,b,bc,bcd,bd,c,cd,d (first one is an empty string if a b c d are false) I'm not overly proud of how I found the combinations, but sometimes nothing beats brute force :-) arr = ['a','b','c','d']; numElems = arr.length; combos = new Array(); c=0; while(c10){ combo = ''; for(var i=0; inumElems; i++){ if(Math.random() 0.5){ combo += arr[i]; } } inArray = false; for(var i=0, len=combos.length; ilen; i++){ if(combo == combos[i]){ inArray = true; break } } if(!inArray){ combos.push(combo); } c++; } combos.sort(); trace(combos); trace(combos.length); -- Andreas Weber motiondraw.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Martin Wood Sent: Wednesday, January 25, 2006 8:05 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations i've got to go out now so cant give the full answer i have in my head, but one way of tackling it is to make each state variable's value a power of 2 a = 1 b = 2 c = 4 d = 8 then you know that any combination of them has a unique value. (its basically a 4 bit number) then to handle the dispatch depending on the state combination you can build a function table. functionTable = new Array(); // create a handler for the combination a + b functionTable[a + b] = Delegate.create(this,combinationAB); and when you check the variables just call the function directly from the table : // a,b,c,d are just passed as boolean flags here function handleUpdate(a:boolean,b:boolean,c:boolean,d:boolean) { // taking advantage of a true being 1 in flash // and ideally you would setup these numbers // as static class variables var state = a + (b * 2) + (c * 4) + (d * 8); // call the defined function functionTable[state](); } of course you could do a check first to see if the function is defined and do something like log a warning or whatever is appropriate for your situation. hope that makes sense. martin ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] checking combinations
Aren't there 256 possible combinations? Or if one value can be null, then 625? What about dab, cab, bac, da, ca, etc? Or are you saying for the sake simplicity, cab is the same combination as abc and dab the same as abd? Jason Merrill | E-Learning Solutions | icfconsulting.com -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of Andreas Weber Sent: Wednesday, January 25, 2006 2:21 PM To: Flashcoders mailing list Subject: RE: [Flashcoders] checking combinations Seems to be an ispiring question/problem! Eric - if you'd go with the combo - Strings - these are the 16 possible combinations: ,a,ab,abc,abcd,abd,ac,acd,ad,b,bc,bcd,bd,c,cd,d (first one is an empty string if a b c d are false) I'm not overly proud of how I found the combinations, but sometimes nothing beats brute force :-) arr = ['a','b','c','d']; numElems = arr.length; combos = new Array(); c=0; while(c10){ combo = ''; for(var i=0; inumElems; i++){ if(Math.random() 0.5){ combo += arr[i]; } } inArray = false; for(var i=0, len=combos.length; ilen; i++){ if(combo == combos[i]){ inArray = true; break } } if(!inArray){ combos.push(combo); } c++; } combos.sort(); trace(combos); trace(combos.length); -- Andreas Weber motiondraw.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Martin Wood Sent: Wednesday, January 25, 2006 8:05 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations i've got to go out now so cant give the full answer i have in my head, but one way of tackling it is to make each state variable's value a power of 2 a = 1 b = 2 c = 4 d = 8 then you know that any combination of them has a unique value. (its basically a 4 bit number) then to handle the dispatch depending on the state combination you can build a function table. functionTable = new Array(); // create a handler for the combination a + b functionTable[a + b] = Delegate.create(this,combinationAB); and when you check the variables just call the function directly from the table : // a,b,c,d are just passed as boolean flags here function handleUpdate(a:boolean,b:boolean,c:boolean,d:boolean) { // taking advantage of a true being 1 in flash // and ideally you would setup these numbers // as static class variables var state = a + (b * 2) + (c * 4) + (d * 8); // call the defined function functionTable[state](); } of course you could do a check first to see if the function is defined and do something like log a warning or whatever is appropriate for your situation. hope that makes sense. martin ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders NOTICE: This message is for the designated recipient only and may contain privileged or confidential information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of this e-mail by you is prohibited. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
dab, abd, adb, bda, dba, ... all the same combination. On 1/25/06, Merrill, Jason [EMAIL PROTECTED] wrote: Aren't there 256 possible combinations? Or if one value can be null, then 625? What about dab, cab, bac, da, ca, etc? Or are you saying for the sake simplicity, cab is the same combination as abc and dab the same as abd? Jason Merrill | E-Learning Solutions | icfconsulting.com -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of Andreas Weber Sent: Wednesday, January 25, 2006 2:21 PM To: Flashcoders mailing list Subject: RE: [Flashcoders] checking combinations Seems to be an ispiring question/problem! Eric - if you'd go with the combo - Strings - these are the 16 possible combinations: ,a,ab,abc,abcd,abd,ac,acd,ad,b,bc,bcd,bd,c,cd,d (first one is an empty string if a b c d are false) I'm not overly proud of how I found the combinations, but sometimes nothing beats brute force :-) arr = ['a','b','c','d']; numElems = arr.length; combos = new Array(); c=0; while(c10){ combo = ''; for(var i=0; inumElems; i++){ if(Math.random() 0.5){ combo += arr[i]; } } inArray = false; for(var i=0, len=combos.length; ilen; i++){ if(combo == combos[i]){ inArray = true; break } } if(!inArray){ combos.push(combo); } c++; } combos.sort(); trace(combos); trace(combos.length); -- Andreas Weber motiondraw.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Martin Wood Sent: Wednesday, January 25, 2006 8:05 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations i've got to go out now so cant give the full answer i have in my head, but one way of tackling it is to make each state variable's value a power of 2 a = 1 b = 2 c = 4 d = 8 then you know that any combination of them has a unique value. (its basically a 4 bit number) then to handle the dispatch depending on the state combination you can build a function table. functionTable = new Array(); // create a handler for the combination a + b functionTable[a + b] = Delegate.create(this,combinationAB); and when you check the variables just call the function directly from the table : // a,b,c,d are just passed as boolean flags here function handleUpdate(a:boolean,b:boolean,c:boolean,d:boolean) { // taking advantage of a true being 1 in flash // and ideally you would setup these numbers // as static class variables var state = a + (b * 2) + (c * 4) + (d * 8); // call the defined function functionTable[state](); } of course you could do a check first to see if the function is defined and do something like log a warning or whatever is appropriate for your situation. hope that makes sense. martin ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders NOTICE: This message is for the designated recipient only and may contain privileged or confidential information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of this e-mail by you is prohibited. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] checking combinations
Well, if only 16 possibilities, then just use a switch statement (not an if-else - too hairy IMO) Jason Merrill | E-Learning Solutions | icfconsulting.com -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of eric dolecki Sent: Wednesday, January 25, 2006 2:50 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations dab, abd, adb, bda, dba, ... all the same combination. On 1/25/06, Merrill, Jason [EMAIL PROTECTED] wrote: Aren't there 256 possible combinations? Or if one value can be null, then 625? What about dab, cab, bac, da, ca, etc? Or are you saying for the sake simplicity, cab is the same combination as abc and dab the same as abd? Jason Merrill | E-Learning Solutions | icfconsulting.com -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of Andreas Weber Sent: Wednesday, January 25, 2006 2:21 PM To: Flashcoders mailing list Subject: RE: [Flashcoders] checking combinations Seems to be an ispiring question/problem! Eric - if you'd go with the combo - Strings - these are the 16 possible combinations: ,a,ab,abc,abcd,abd,ac,acd,ad,b,bc,bcd,bd,c,cd,d (first one is an empty string if a b c d are false) I'm not overly proud of how I found the combinations, but sometimes nothing beats brute force :-) arr = ['a','b','c','d']; numElems = arr.length; combos = new Array(); c=0; while(c10){ combo = ''; for(var i=0; inumElems; i++){ if(Math.random() 0.5){ combo += arr[i]; } } inArray = false; for(var i=0, len=combos.length; ilen; i++){ if(combo == combos[i]){ inArray = true; break } } if(!inArray){ combos.push(combo); } c++; } combos.sort(); trace(combos); trace(combos.length); -- Andreas Weber motiondraw.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Martin Wood Sent: Wednesday, January 25, 2006 8:05 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations i've got to go out now so cant give the full answer i have in my head, but one way of tackling it is to make each state variable's value a power of 2 a = 1 b = 2 c = 4 d = 8 then you know that any combination of them has a unique value. (its basically a 4 bit number) then to handle the dispatch depending on the state combination you can build a function table. functionTable = new Array(); // create a handler for the combination a + b functionTable[a + b] = Delegate.create(this,combinationAB); and when you check the variables just call the function directly from the table : // a,b,c,d are just passed as boolean flags here function handleUpdate(a:boolean,b:boolean,c:boolean,d:boolean) { // taking advantage of a true being 1 in flash // and ideally you would setup these numbers // as static class variables var state = a + (b * 2) + (c * 4) + (d * 8); // call the defined function functionTable[state](); } of course you could do a check first to see if the function is defined and do something like log a warning or whatever is appropriate for your situation. hope that makes sense. martin ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders NOTICE: This message is for the designated recipient only and may contain privileged or confidential information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of this e-mail by you is prohibited. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
What about something like this: var results:Array = [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p ]; function getResult( value1:Boolean, value2:Boolean, value3:Boolean, value4:Boolean ):Number { var resultIndex:Number = 0; if( value1 ) resultIndex |= 8; if( value2 ) resultIndex |= 4; if( value3 ) resultIndex |= 2; if( value4 ) resultIndex |= 1; return results[ resultIndex ]; } trace( getResult( true, true, false, true) ); /* Results Table value1 value2 value3 value4 Results 0 0 0 0 a 0 0 0 1 b 0 0 1 0 c 0 0 1 1 d 0 1 0 0 e 0 1 0 1 f 0 1 1 0 g 0 1 1 1 h 1 0 0 0 i 1 0 0 1 j 1 0 1 0 k 1 0 1 1 l 1 1 0 0 m 1 1 0 1 n 1 1 1 0 o 1 1 1 1 p */ On 1/25/06, eric dolecki [EMAIL PROTECTED] wrote: I have 4 variables I need to check the states of routinely... and combinations thereof. -- Daniel Cascais Tel: +56 (0)2 4589495 Cel: +56 (0)9 9417355 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
Thats interesting. A bit more involved than what I was doing but it is indeed interesting. On 1/25/06, Daniel Cascais [EMAIL PROTECTED] wrote: What about something like this: var results:Array = [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p ]; function getResult( value1:Boolean, value2:Boolean, value3:Boolean, value4:Boolean ):Number { var resultIndex:Number = 0; if( value1 ) resultIndex |= 8; if( value2 ) resultIndex |= 4; if( value3 ) resultIndex |= 2; if( value4 ) resultIndex |= 1; return results[ resultIndex ]; } trace( getResult( true, true, false, true) ); /* Results Table value1 value2 value3 value4 Results 0 0 0 0 a 0 0 0 1 b 0 0 1 0 c 0 0 1 1 d 0 1 0 0 e 0 1 0 1 f 0 1 1 0 g 0 1 1 1 h 1 0 0 0 i 1 0 0 1 j 1 0 1 0 k 1 0 1 1 l 1 1 0 0 m 1 1 0 1 n 1 1 1 0 o 1 1 1 1 p */ On 1/25/06, eric dolecki [EMAIL PROTECTED] wrote: I have 4 variables I need to check the states of routinely... and combinations thereof. -- Daniel Cascais Tel: +56 (0)2 4589495 Cel: +56 (0)9 9417355 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
I guess a switch is a little nicer to look at. On 1/25/06, Merrill, Jason [EMAIL PROTECTED] wrote: Well, if only 16 possibilities, then just use a switch statement (not an if-else - too hairy IMO) Jason Merrill | E-Learning Solutions | icfconsulting.com -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of eric dolecki Sent: Wednesday, January 25, 2006 2:50 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations dab, abd, adb, bda, dba, ... all the same combination. On 1/25/06, Merrill, Jason [EMAIL PROTECTED] wrote: Aren't there 256 possible combinations? Or if one value can be null, then 625? What about dab, cab, bac, da, ca, etc? Or are you saying for the sake simplicity, cab is the same combination as abc and dab the same as abd? Jason Merrill | E-Learning Solutions | icfconsulting.com -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of Andreas Weber Sent: Wednesday, January 25, 2006 2:21 PM To: Flashcoders mailing list Subject: RE: [Flashcoders] checking combinations Seems to be an ispiring question/problem! Eric - if you'd go with the combo - Strings - these are the 16 possible combinations: ,a,ab,abc,abcd,abd,ac,acd,ad,b,bc,bcd,bd,c,cd,d (first one is an empty string if a b c d are false) I'm not overly proud of how I found the combinations, but sometimes nothing beats brute force :-) arr = ['a','b','c','d']; numElems = arr.length; combos = new Array(); c=0; while(c10){ combo = ''; for(var i=0; inumElems; i++){ if(Math.random() 0.5){ combo += arr[i]; } } inArray = false; for(var i=0, len=combos.length; ilen; i++){ if(combo == combos[i]){ inArray = true; break } } if(!inArray){ combos.push(combo); } c++; } combos.sort(); trace(combos); trace(combos.length); -- Andreas Weber motiondraw.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Martin Wood Sent: Wednesday, January 25, 2006 8:05 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations i've got to go out now so cant give the full answer i have in my head, but one way of tackling it is to make each state variable's value a power of 2 a = 1 b = 2 c = 4 d = 8 then you know that any combination of them has a unique value. (its basically a 4 bit number) then to handle the dispatch depending on the state combination you can build a function table. functionTable = new Array(); // create a handler for the combination a + b functionTable[a + b] = Delegate.create(this,combinationAB); and when you check the variables just call the function directly from the table : // a,b,c,d are just passed as boolean flags here function handleUpdate(a:boolean,b:boolean,c:boolean,d:boolean) { // taking advantage of a true being 1 in flash // and ideally you would setup these numbers // as static class variables var state = a + (b * 2) + (c * 4) + (d * 8); // call the defined function functionTable[state](); } of course you could do a check first to see if the function is defined and do something like log a warning or whatever is appropriate for your situation. hope that makes sense. martin ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders NOTICE: This message is for the designated recipient only and may contain privileged or confidential information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of this e-mail by you is prohibited. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] checking combinations
The return type should really be String in this case function getResult( value1:Boolean, value2:Boolean, value3:Boolean, value4:Boolean ):String On 1/25/06, eric dolecki [EMAIL PROTECTED] wrote: Thats interesting. A bit more involved than what I was doing but it is indeed interesting. -- Daniel Cascais Tel: +56 (0)2 4589495 Cel: +56 (0)9 9417355 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] checking combinations
I guess I must be missing something here - I still don't understand the advantage of 'encoding' the conditions as numbers. As I see it, there is no way around evaluating 16 conditions. We can either make this transparent or we can 'elegantly' hide it behind numbers - which we'll have to comment heavily to 'decode' the meaning of the numbers and to make the code readable for humans. It's about reacting to the possible combinations of 4 different events, right? Let's say the events are 'Inited','Clicked','Bedazzeled','Dragged'. We could now just do this: // When event 'Inited' is dispatched, set the flag Inited to true, etc. combo = ''; if(events[i]){ combo += 'Inited';} if(events[i]){ combo += 'Clicked';} if(events[i]){ combo += 'Bedazzeled';} if(events[i]){ combo += 'Dragged';} switch(combo){ case : //do stuff here break; case Bedazzeled: //do stuff here break; case BedazzeledDragged: //do stuff here break; case Clicked: //do stuff here break; case ClickedBedazzeled: //do stuff here break; case ClickedBedazzeledDragged: //do stuff here break; case ClickedDragged: //do stuff here break; case Dragged: //do stuff here break; case Inited: //do stuff here break; case InitedBedazzeled: //do stuff here break; case InitedBedazzeledDragged: //do stuff here break; case InitedClicked: //do stuff here break; case InitedClickedBedazzeled: //do stuff here break; case InitedClickedBedazzeledDragged: //do stuff here break; case InitedClickedDragged: //do stuff here break; case InitedDragged: //do stuff here break; } //-- - Imo this is a very readable solution - no comments needed to explain what the code is doing. Above code is generated and traced to the Output window with this: events = ['Inited','Clicked','Bedazzeled','Dragged']; numElems = events.length; combos = new Array(); // find the combinations c=0; while(cnumElems*numElems){ combo = ''; for(var i=0; inumElems; i++){ if(Math.random() 0.5){ combo += events[i]; } } inArray = false; for(var i=0, len=combos.length; ilen; i++){ if(combo == combos[i]){ inArray = true; break } } if(!inArray){ combos[c++] = combo; } } combos.sort(); // write the code for copy/paste from the output window trace(// When event '+ events[0]+' is dispatched, set the flag +events[0]+ to true, etc.); trace(combo = '';); for(var i=0, len=events.length; ilen; i++){ trace(if(events[i]){ combo += '+events[i]+';}); } trace('\nswitch(combo){'); // write the switch statement for(var i=0, len=combos.length; ilen; i++){ trace('\n case '+combos[i]+':'); trace(' //do stuff here'); trace(' break;'); } trace('}'); Just overwrite the 'events' Array to accommodate your needs. hth -- Andreas Weber motiondraw.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of eric dolecki Sent: Mittwoch, 25. Januar 2006 21:17 To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations Thats interesting. A bit more involved than what I was doing but it is indeed interesting. On 1/25/06, Daniel Cascais [EMAIL PROTECTED] wrote: What about something like this: var results:Array = [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p ]; function getResult( value1:Boolean, value2:Boolean, value3:Boolean, value4:Boolean ):Number { var resultIndex:Number = 0; if( value1 ) resultIndex |= 8; if( value2 ) resultIndex |= 4; if( value3 ) resultIndex |= 2; if( value4 ) resultIndex |= 1; return results[ resultIndex ]; } trace( getResult( true, true, false, true) ); /* Results Table value1 value2 value3 value4 Results 0 0 0 0 a 0 0 0 1 b 0 0 1 0 c 0 0 1 1 d 0 1 0 0 e 0 1 0 1 f 0 1 1 0 g 0 1 1 1 h 1 0 0 0 i 1 0 0 1 j 1 0
RE: [Flashcoders] checking combinations
Perhaps I'm missing something, but IMO bitwise switch is best approach here. I'd say, it looks like a classical case for it (like user rights etc :-). So, 1) map abcd to binary number: - all are false, - all are true, 1100 - a,b are true while c,d false. 2) convert to digital: -0, -15, 1100-3 and so on 3) checking is done, use some function to process if needed 4) in case you consider abc, cba and bca as different combinations (which makes sense only if they all are true), I can suggest use a pattern number/string for positions/order: 1234 means abcd, 2341 means bcda. Combining this with 1-2 points for normal order (abcd) will cover all combinations. Hope this can help. -- Best regards, GregoryN http://GOusable.com Flash components development. Usability services. On 1/25/06, eric dolecki [EMAIL PROTECTED] wrote: I have 4 variables I need to check the states of routinely... and combinations thereof. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] checking combinations
What he said :) Without kidding, using the binary type of variable values and checking what the resulting value is, is the way we do it. Don't ask me how it works, but it does :) (just to help pointing in the right direction) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Martin Wood Sent: woensdag 25 januari 2006 20:05 To: Flashcoders mailing list Subject: Re: [Flashcoders] checking combinations i've got to go out now so cant give the full answer i have in my head, but one way of tackling it is to make each state variable's value a power of 2 a = 1 b = 2 c = 4 d = 8 then you know that any combination of them has a unique value. (its basically a 4 bit number) then to handle the dispatch depending on the state combination you can build a function table. functionTable = new Array(); // create a handler for the combination a + b functionTable[a + b] = Delegate.create(this,combinationAB); and when you check the variables just call the function directly from the table : // a,b,c,d are just passed as boolean flags here function handleUpdate(a:boolean,b:boolean,c:boolean,d:boolean) { // taking advantage of a true being 1 in flash // and ideally you would setup these numbers // as static class variables var state = a + (b * 2) + (c * 4) + (d * 8); // call the defined function functionTable[state](); } of course you could do a check first to see if the function is defined and do something like log a warning or whatever is appropriate for your situation. hope that makes sense. martin eric dolecki wrote: I have 4 variables I need to check the states of routinely... and combinations thereof. I am assuming that if I have a counter, and interogate and += them values, I can then check the value of the counter to determine the combinations. psudeo-code: var counter:Number = 0; if ( a ){ counter += 2; } if ( b ){ counter += 3; } if ( c ){ counter += 6; } if ( d ){ counter += 12;} if ( counter == 2 ){ // only a was true } else if ( counter == 3 ){ // only b was true } ... Which is fine, but thats gonna be one honkin' if else statement to catch all the combinations. Is there a better way of doing this that will catch all the possible combinations in an elegant way? - edolecki ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- Martin Wood http://relivethefuture.com/choronzon ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] checking combinations
Ok, ok I'm clearly outnumbered here - waving white flag :-) And the approach Daniel demonstraded (similar to what Martin outlined) is indeed very elegant and succint. When the actions to take (depending on which combination 'is the case') are rather complex, requiring several lines of code, imo the immediately readable 'string-based' switch still has its merits in terms of readability/managability, but I won't insist - up to Eric to pick the one that best matches the needs of his project. Cheers! -- Andreas Weber motiondraw.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Ben Smeets Sent: Thursday, January 26, 2006 8:19 AM To: Flashcoders mailing list Subject: RE: [Flashcoders] checking combinations What he said :) Without kidding, using the binary type of variable values and checking what the resulting value is, is the way we do it. Don't ask me how it works, but it does :) (just to help pointing in the right direction) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders