Skip to main content
Known Participant
July 31, 2012
Answered

Explain me these codes ...

  • July 31, 2012
  • 3 replies
  • 1358 views

private var mineField:Array=new Array();

public function Main() {

// mine field creation

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

mineField=new Array();

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

mineField.push(0);

}

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

}

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 is an array containing only the i from loop ("0,1,2,3,4,5,6,7,8")

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

Please, explain, it all mixes in my head together

This topic has been closed for replies.
Correct answer Ned Murphy

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=new Array();

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

      mineField.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 replies

sinious
Legend
July 31, 2012

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 = new Array();

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

                    {

                              mineField.push(Math.round(Math.random()*1));

                    }

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

          }

          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)

Ned Murphy
Ned MurphyCorrect answer
Legend
July 31, 2012

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=new Array();

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

      mineField.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.

Known Participant
July 31, 2012

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

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

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

And var mineFiled contains all other sub-minerFields

Am I right ?

sinious
Legend
July 31, 2012

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

mineField.push(0); // tells the array mineField 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);

July 31, 2012

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 = new Array() and the zereos are pushed in it some code lines later.