PDA

View Full Version : 2D Array for a game board



Stax
December 14th, 2006, 05:48 PM
Hi,

I'm currently in the early stage of making a small game of the type "if you align 3 identical beads vertically or horizontally they disapear and get replaced by new ones".

I'm at the pont of filling my 2D Array, to me, my code makes sense and should work, but for some reason the output is really odd.

The main porblem is that my in my 2D array gameBoard[i][j] for some reason the adresses past 1 in the i column do not get defined even tho I'm pretty sure I'm defining them correctly.

If you're wondering about the ifs and the continues, when I fill my board, I dont want any identical pieces 3 times in a row so a valid combination is not already on the board when the user first starts the game.

Anyways look at the trace of this code, its really odd.



var boardSize:Number = 8;
var gameBoard:Array = new Array(new Array(boardSize), new Array(boardSize));
//
//
//Fills the board using the number of different types of beads
fillBoard = function (types:Number) {
for (var i = 0; i<boardSize; i++) {
for (var j = 0; j<boardSize; j++) {
//Make sure the initial filling wont put combinations on the initial layout
while (true) {
var randomType = Math.floor(Math.random()*types)+1;
if (i>=2 && (gameBoard[i-2][j] == gameBoard[i-1][j] == randomType)) {
continue;
}
if (i>=1 && i<=(boardSize-2) && (gameBoard[i-1][j] == randomType == gameBoard[i+1][j])) {
continue;
}
if (i<=boardSize-3 && (randomType == gameBoard[i+1][j] == gameBoard[i+2][j])) {
continue;
}
if (j>=2 && (gameBoard[i][j-2] == gameBoard[i][j-1] == randomType)) {
continue;
}
if (j>=1 && j<=(boardSize-2) && (gameBoard[i][j-1] == randomType == gameBoard[i][j+1])) {
continue;
}
if (j<=boardSize-3 && (randomType == gameBoard[i][j+1] == gameBoard[i][j+2])) {
continue;
} else {
gameBoard[i][j] = randomType;
trace("i-> "+i+" , j-> "+j+" ==== "+gameBoard[i][j]+" and the random number that was assigned is: "+randomType);
break;
}
}
//End of while
}
}
trace(gameBoard[0][0]+" "+gameBoard[0][1]+" "+gameBoard[0][2]+" "+gameBoard[0][3]+" "+gameBoard[0][4]+" "+gameBoard[0][5]+" "+gameBoard[0][6]+" "+gameBoard[0][7]);
trace(gameBoard[1][0]+" "+gameBoard[1][1]+" "+gameBoard[1][2]+" "+gameBoard[1][3]+" "+gameBoard[1][4]+" "+gameBoard[1][5]+" "+gameBoard[1][6]+" "+gameBoard[1][7]);
trace(gameBoard[2][0]+" "+gameBoard[2][1]+" "+gameBoard[2][2]+" "+gameBoard[2][3]+" "+gameBoard[2][4]+" "+gameBoard[2][5]+" "+gameBoard[2][6]+" "+gameBoard[2][7]);
trace(gameBoard[3][0]+" "+gameBoard[3][1]+" "+gameBoard[3][2]+" "+gameBoard[3][3]+" "+gameBoard[3][4]+" "+gameBoard[3][5]+" "+gameBoard[3][6]+" "+gameBoard[3][7]);
trace(gameBoard[4][0]+" "+gameBoard[4][1]+" "+gameBoard[4][2]+" "+gameBoard[4][3]+" "+gameBoard[4][4]+" "+gameBoard[4][5]+" "+gameBoard[4][6]+" "+gameBoard[4][7]);
trace(gameBoard[5][0]+" "+gameBoard[5][1]+" "+gameBoard[5][2]+" "+gameBoard[5][3]+" "+gameBoard[5][4]+" "+gameBoard[5][5]+" "+gameBoard[5][6]+" "+gameBoard[5][7]);
trace(gameBoard[6][0]+" "+gameBoard[6][1]+" "+gameBoard[6][2]+" "+gameBoard[6][3]+" "+gameBoard[6][4]+" "+gameBoard[6][5]+" "+gameBoard[6][6]+" "+gameBoard[6][7]);
trace(gameBoard[7][0]+" "+gameBoard[7][1]+" "+gameBoard[7][2]+" "+gameBoard[7][3]+" "+gameBoard[7][4]+" "+gameBoard[7][5]+" "+gameBoard[7][6]+" "+gameBoard[7][7]);
};
//executes the board filling
fillBoard(6);

the big trace at the end is onl so I can have a clear representation of the board in my output to verify if my code is working properly Ie :

2 3 4 6 3 6
1 2 1 5 5 4
4 4 3 2 1 3
1 2 1 2 5 4
5 4 3 5 1 3

(just thought I could do this huge trace with a forloop, but I dont feel like rewrting it right now)

If you see anything please share some advice

Thanks !

zellers
December 14th, 2006, 10:50 PM
I am going to look this over, I should probably have an answer tommorow.....

bombsledder
December 14th, 2006, 10:58 PM
set up some more traces, especially while defining the variables you could trace the i,k variables like this

trace("i->"+i+" "+"k->"+k)

and make sure your defining the end of the map

Stax
December 15th, 2006, 01:53 PM
thanks shifty, I'm still looking into it myself, but it seems like I'm doing everything by the book according to the macromedia help files...

If you find anything please tell

Stax
December 15th, 2006, 01:56 PM
And what do you mean by the end of the map Bomb ?

Stax
December 15th, 2006, 04:02 PM
Got it,
A way that works to declare 2d arrays that work :



var gameBoard=new Array(boardSize);
for(var i=0;i<gameBoard.length;i++) {
gameBoard[i] = new Array(boardSize);
}

bombsledder
December 15th, 2006, 04:29 PM
you send the main tiles at the end of the map werent loading right? so i just ment that you should add some more traces and make sure that you are adding the tiles to the end of the map