Copy link to clipboard
Copied
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
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 ?
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
for (var i:uint=0; i<FIELD_H; i++) {
mineField=new Array();
for (var j:uint=0; j<FIELD_W; j++) {
mineField.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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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)