8 Replies Latest reply: Jul 31, 2012 1:29 PM by FlashGodThe2nd RSS

    Explain me these codes ...

    milanfiala

      private var mineField:Array=new Array();

       

      public function Main() {

      // mine field creation

      for (var i:uint=0; i<FIELD_H; i++) {

      mineField[i]=new Array();

      for (var j:uint=0; j<FIELD_W; j++) {

      mineField[i].push(0);

      }

      trace("Row "+i+": "+mineField[i]);

      }

      trace("The whole mine field: "+mineField);

      // end of mine field creation

      }

       

      This is a system of multiple array. I copied it from the book of game design. I would like you to explain :

      "Why in the trace ""whole mine field" is 81 zeros?"

       

      Ok, I predict (I understood from the book), that private var mineField array is array of i and j

      ...

      I also understood that mineField[i] is an array containing only the i from loop ("0,1,2,3,4,5,6,7,8")

      ... and mineField[i].push(0) changes all i-s in array to 0 ?

       

      Please, explain, it all mixes in my head together

        • 1. Re: Explain me these codes ...
          FlashGodThe2nd Community Member

          Try to use the debugger and watch the variables.

           

          Take a look at the manual for push. Push does not change it appends a value to an array. Here the Array is created at mineField[i] = new Array() and the zereos are pushed in it some code lines later.

          • 2. Re: Explain me these codes ...
            Ned Murphy MVP

            I don't know that trying to explain it will make it clearer to you, but here's a shot at it...

             

            mineField is being used as a multidimensional array, or in other words, it is an array containing arrays.

             

            In the loop using " i ", each element of mineField is being assigned a new array in this line:  

             

                  mineField[i]=new Array();

             

            For the " j " loop, each element of the new array is being assigned a value of 0 in this line.

             

                  mineField[i].push(0);

             

            The only values being assigned to the arrays is 0, not 0,1,2,3,4,5,6,7,8,9

             

            The last line is tracing 81 zeroes most likely because FIELD_H and FIELD_W are both equal to 9.  Each element of the mineField is an array containing 9 zeroes, so when you tell it to trace the array it traces each element of that array separate by commas.  Since each element of that array is another array, it traces them separated by commas as well.

             

            What that last trace is actually tracing is 9 arrays, each containing 9 zeroes.

            • 3. Re: Explain me these codes ...
              sinious MVP

              I presume FIELD_H and FIELD_W are being set up as const variables somewhere else and you're just pasting relevant code. So I'll reply with the same.

               

              What I think you're looking for is to get 9 rows with 9 columns, each creating a random amount of mines. The value should be 0 for no mine or 1 for a mine. You'd just need to rewrite the push(0) so it generated a 0 or a 1.

               

              e.g.:

               

              private var mineField:Array = new Array();

               

              public function Main()

              {

                        // mine field creation

                        for (var i:uint = 0; i < FIELD_H; i++)

                        {

                                  mineField[i] = new Array();

                                  for (var j:uint = 0; j < FIELD_W; j++)

                                  {

                                            mineField[i].push(Math.round(Math.random()*1));

                                  }

                                  trace("Row " + i + ": " + mineField[i]);

                        }

                        trace("The whole mine field: " + mineField);

                        // end of mine field creation

              }

               

              Now a number between 0 and 1 will populate each slot. If you want it to be another value range like 0 - 100 then simply change it to: Math.round(Math.random()*100)

              • 4. Re: Explain me these codes ...
                milanfiala Community Member

                Ok, so, correct me, please if I am wrong...

                 

                mineField[i]=new Array(); ... this means that each i creates new array

                 

                mineField[i].push(0);     ... this means that each created i is 0 ? (So should here [i] be "j" ?)

                 

                And var mineFiled contains all other sub-minerFields

                 

                Am I right ?

                • 5. Re: Explain me these codes ...
                  sinious MVP

                  mineField[i] = new Array(); // creates a new array inside of the array mineField at index position i

                   

                  mineField[i].push(0); // tells the array mineField[i] to push the value "0" into itself (which is an array)

                   

                  By doing the above you are ONLY adding 0's to the array.

                   

                  Without the loops which might be confusing you, this is really what's happening:

                   

                  var mineField:Array = new Array();

                   

                  mineField[0] = new Array(0,0,0,0,0,0,0,0,0);

                  mineField[1] = new Array(0,0,0,0,0,0,0,0,0);

                  mineField[2] = new Array(0,0,0,0,0,0,0,0,0);

                  mineField[3] = new Array(0,0,0,0,0,0,0,0,0);

                  mineField[4] = new Array(0,0,0,0,0,0,0,0,0);

                  mineField[5] = new Array(0,0,0,0,0,0,0,0,0);

                  mineField[6] = new Array(0,0,0,0,0,0,0,0,0);

                  mineField[7] = new Array(0,0,0,0,0,0,0,0,0);

                  mineField[8] = new Array(0,0,0,0,0,0,0,0,0);

                   

                  trace("The whole mine field: " + mineField);

                  • 6. Re: Explain me these codes ...
                    milanfiala Community Member
                     

                    for (var i:uint=0; i<FIELD_H; i++) {

                         mineField[i]=new Array();

                              for (var j:uint=0; j<FIELD_W; j++) {

                                   mineField[i].push(0);

                    }

                     

                    Ok. And am I right, that each line here is subsidary for the previous one, so to each mineFilend array we get nine 0 (defined by var j = 9 times) (and zero = by push)

                     

                    If I am right, I completely understand

                    • 7. Re: Explain me these codes ...
                      sinious MVP

                      Yes. "i" will start at 0 and go to "FIELD_H" which is presumed to be 9. "j" will do the same and go from 0 to "FIELD_W" which is also presumed to be 9.

                       

                      So:

                       

                      mineField[0][0] = 0;

                      mineField[0][1] = 0;

                      mineField[0][2] = 0;

                      mineField[0][3] = 0;

                      mineField[0][4] = 0;

                      mineField[0][5] = 0;

                      mineField[0][6] = 0;

                      mineField[0][7] = 0;

                      mineField[0][8] = 0; 

                      ....

                      mineField[8][0] = 0;

                      mineField[8][1] = 0;

                      mineField[8][2] = 0;

                      mineField[8][3] = 0;

                      mineField[8][4] = 0;

                      mineField[8][5] = 0;

                      mineField[8][6] = 0;

                      mineField[8][7] = 0;

                      mineField[8][8] = 0;

                      etc

                       

                      Remember it starts at 0 and goes until it's LESS than 9, which is why it stops at 8. That's 9 entries anyhow, 0,1,2,3,4,5,6,7,8

                      • 8. Re: Explain me these codes ...
                        FlashGodThe2nd Community Member

                        mineField[0].push(0)

                         

                        does the same as

                         

                        mineField[0][ mineField[0].length ] = 0

                         

                        as an array is allways zero based, the length of it is allways one number higher than the last index. At least in Flash as3. Other languages can be different.

                         

                        The trace command just gives back all the values stored in the 2 dimensional array. When you look at the debugger at the variables you will see how the array is/was build.