0

I'm working on a match-3 style puzzle game using Flixel, and so I'm working on checking each row and column to see if there is a match at any given time. However, I have 6 different pieces (as of right now) that are active, and each piece is identified by an integer. Given that, I can check, for each and every single piece, by doing something like this:

public function matchingCheck():void
    {
        if (piecesArray[0][1] == 1 && piecesArray[1][1] == 1 && piecesArray[2][1] == 1) {
            FlxG.log("Yay!");
        }
    }

However, this is rather unwieldy and would basically cause way too much repetition for my liking.

At the very least, I would like to be able to check if the values in these arrays are equal to one another, without having to specify which value it is. At the very best, I'd love to be able to check an entire row for three (or more) adjacent pieces, but I will settle for doing that part manually.

Thanks for your help!


EDIT: Nevermind, my edit didn't work. It was just checking if piecesArray[2][1] == 1, which makes me a sad panda.


EDIT 2: I've selected the correct answer below - it's not exactly what I used, but it definitely got me started. Thanks Apocalyptic0n3!

JonLim
  • 1,393
  • 2
  • 14
  • 23

2 Answers2

1

You could cut down on that code a little bit by using another function

private function checkValid( arrayOfItemsToCheck:Array, value:* ):Boolean {
    for ( var i:Number = 0; i < arrayOfItemsToCheck.length; i++ ) {
        if ( arrayOfItemsToCheck[i] != value ) {
            return false;
        }
    }
    return true;
}

Then you just do this in your if statement:

if ( checkValid( [ piecesArray[0][1], piecesArray[1][1], piecesArray[2][1] ], 1 ) ) {
            FlxG.log("Yay!");
}

That does assume all items need to be equal to 1, though. It's still a lot of code, but it cuts out one set of "= 1 &&" for each check.

Josh
  • 8,079
  • 3
  • 24
  • 49
  • Wouldn't the `checkValid` function be essentially the same as my code in the post edit, where I equate all of them to 1? Not trying to sound rude, just wondering if they achieve the same thing. :) – JonLim Nov 01 '12 at 21:03
  • It achieves the same thing, sure. It's far more extensible, however. You could use that function to check anything like this and would remove a lot of repeatable code. I also know that some people are not fond of that particular form of checking (it's a matter of preference, but some don't like having more than one equal per line, myself included). Just so you know, I posted my comment less ~90 seconds after your edit. I didn't see it until after I had posted. – Josh Nov 01 '12 at 21:08
  • Ah, no worries! And yes, that makes sense. Thanks, will have to experiment and think about this. – JonLim Nov 01 '12 at 21:11
  • Yeah, I wasn't sure if it would (and didn't feel like checking). That form is usually meant for setting multiple properties to the same value. The only time I see it used, really, is `scaleX = scaleY = 1.5` – Josh Nov 01 '12 at 21:33
  • I think one of the modifications to your original function I'm going to try is to cycle through all of the possible values, just to eliminate having to set what the value is. Will try it and report back! – JonLim Nov 02 '12 at 14:14
  • You could always pass in an array of possible values, do a loop within a loop and create a score for each possible value (how many match the value) and return true if any of those values are equal to the length of `arrayOfItemsToCheck`. That would maintain its flexibility and give it the functionality you mention. – Josh Nov 02 '12 at 16:09
0

How about something like this which would tell you both if a match existed and what match it was:

public function checkForMatch():void{
    var rows:int = piecesArray.length;
    for(var i:int=0; i<rows; i++){
        var match:int = checkRow(piecesArray[i]);
        if(match > -1) {
            FlxG.log("Yay you matched " + match);
        }
    }
}

private function ckeckRow(row:Array):int{
    if(row[0] == row[1] == row[2]){
        return row[0];
    }
    return -1;
}
Jason Reeves
  • 1,716
  • 1
  • 10
  • 13