Be a Supporter!

Comparing array.

  • 222 Views
  • 6 Replies
New Topic Respond to this Topic
MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Comparing array. 2011-08-03 23:58:26 Reply

So I've been trying to make a combo system in my game, and I need to check the current combo.

The current combo are stored in array of integers. And I quickly realized that I can't compare arrays anything like I want to.

var a:Array = [1, 2, 3];
trace(a == [1, 2, 3]);

That come out false for a reason I don't understand, so how would I properly compare an array?


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
PSvils
PSvils
  • Member since: Feb. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Game Developer
Response to Comparing array. 2011-08-04 00:49:48 Reply

Gotta loop through each array and check each value individually:

equal = true;
for (i=0; i<a.length;i++)
{
     if (a[i] != b[i])
     {
          equal = false;
          break;
     }
}

if (equal)
{
    trace ("yo arrays are crunk fo shizzle");
}
MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Comparing array. 2011-08-04 01:29:14 Reply

Well that's going to be annoying with like 20 combos.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
Version2
Version2
  • Member since: Sep. 24, 2003
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to Comparing array. 2011-08-04 01:43:04 Reply

At 8/4/11 01:29 AM, MintPaw wrote: Well that's going to be annoying with like 20 combos.

Nah, just use a multi-dimensional array to store your combos, then loop through that. Some pseudocode off the top of my head on what it might look like:

var comboArr:Array = [[1, 2, 3], [2, 1, 3], [3, 2, 1]]
var comboToCheck:Array;

for each(var combo:Array in comboArr){
     for(var i = 0; i < combo.length; i++){
          if (comboToCheck[i] != combo[i]){
               //do your check here
               //if combo matches, save it and execute after loops or something
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Comparing array. 2011-08-04 03:23:49 Reply

Arrays are passed by reference, not by value.
Comparing references will figure out if both reference the same object, not if the members of each object have the same values.

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * comparing arrays
	 * @author milchreis
	 */
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			var a:Array = [1, 2, 3];
			var b:Array = [1, 2, 3];
			var c:Array = ["a", "b", "c"];
			var d:Array = [1, 2, 3, 4];
			
			trace (areEqualArrays(a, a));
			trace (areEqualArrays(a, b));
			trace (areEqualArrays(a, c));
			trace (areEqualArrays(a, d));
			trace (areEqualArrays(c, d));
		}
		
		private function areEqualArrays(a:Array, b:Array):Boolean
		{
			if (a == null || b == null) return false;
			var l:int = a.length;
			if (l != b.length) return false;
			for (var i:int = l - 1; i >= 0; --i)
			{
				if (a[i] != b[i]) return false;
			}
			return true;
		}
	}
}
VigilanteNighthawk
VigilanteNighthawk
  • Member since: Feb. 13, 2003
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Comparing array. 2011-08-04 10:32:08 Reply

I know this isn't quite the answer you were looking for, but I'm thinking that using arrays for comparison at run time is going to be a bit processor intensive. An alternative approach would be to use something similar to this to store the combinations: http://en.wikipedia.org/wiki/Trie#As_rep lacement_of_other_data_structures Searching the tree should much faster than comparing against each array. You can still store each combination into an array at compile time and load the tree from them at runtime.


The Internet is like a screwdriver. You can use it to take an engine apart and understand it, or you can see how far you can stick it in your ear until you hit resistance.

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Comparing array. 2011-08-04 13:52:58 Reply

At 8/4/11 03:23 AM, milchreis wrote: Arrays are passed by reference, not by value.

Ah ok, I get it now, I'm just pretty much going to steal the function you got there.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature