Trouble populating 2D array for Match-3 Game

Hi All,

I'm trying to teach myself the mechanics behind a match-3 game and am having some issues handling my arrays. The code below seems to create my 2D array (at least, it gets through the CreateBoard function...) but when I go to try to populate it, things fall apart:

    var numSymbols:int = 4;
    var symbols:Array = new Array(numSymbols);
    var currentBoard:Array;

    //the symbols being used atm.
    symbols[0] = "triangle";
    symbols[1] = "circle";
    symbols[2] = "square";
    symbols[3] = "ex";

    public function CreateBoard(x:int, y:int):Array {
        //create an array with of x elements. 
            //Each element will be an array of y elements.
        var gameBoard = new Array (x);
        for (var i in gameBoard)
            gameBoard = new Array(y);
        print("created");
        return gameBoard;
    }

    // 'a' is never getting a proper variable passed to it, it seems. 
    public function PopulateBoard(a:Array):Array{
        var toPop:Array = a;
        for (var i in toPop){
            for (var j in toPop*)*
 _toPop *= symbols[Random.Range(0,numSymbols)];*_
 _*}*_
 _*print("populated");*_
 _*return toPop;*_
 _*}*_
 _*//I think I broke it. Just fix this, then start testing the output.*_
 _*function Update(){*_
 _*if (Input.GetButton("Fire1")){*_
 _*currentBoard = CreateBoard(5,4);*_
 _*currentBoard = PopulateBoard(currentBoard);*_
 _*//for (var i in currentBoard) print(currentBoard);*_
 _*}*_
 _*}*_
_*```*_
_*<p>The specific error I'm getting is</p>*_
_*```*_
_*NullReferenceException: Object reference not set to an instance of an object*_
_*Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible)*_
_*Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value)*_
_*Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value)*_
_*gameLogic.PopulateBoard (UnityScript.Lang.Array a) (at Assets/gameLogic.js:24)*_
_*gameLogic.Update () (at Assets/gameLogic.js:35)*_
_*```*_
_*<p>To me it sounds like the 'a' in PopulateBoard isn't getting passed the proper information but, as far as I can tell, it should be...</p>*_
_*<p>I decided to use JS arrays as, from what I understand, unity's built-in ones don't have as much flexibility. Also, I've seen the question here:*_
_*<a href="http://answers.unity3d.com/questions/16431/passing-a-multidimensional-array-in-javascript" rel="nofollow">http://answers.unity3d.com/questions/16431/passing-a-multidimensional-array-in-javascript</a>*_
_*but I'm trying to understand why my approach wouldn't work :(</p>*_
_*<p>Can someone show me the error of my ways?</p>*_
_*<p>Thanks in advance and sorry for the massive wall-o-code</p>*_

1 Answer

1

There's no reason to use Array; it's slow and not typesafe. Just use a 2D array (the answer you reference is outdated). Also you should use ints rather than strings, since it's faster and simpler. You can use enums if you want human-readable values instead of magic numbers.

enum Symbols {Triangle, Circle, Square, Ex}
var currentBoard : int[,];

(As to the actual problem, you're assigning gameBoard over again for every iteration through the array, and the "i" in "for (var i in gameBoard)" is never used.)

oh wow, ok. I really was making things harder than they needed to be. Thanks a bunch!