I’m making a program that draws a grid of boxes and the color of the box must be different to those adjacent to the box.
My code compares the current box’s color to those to the left and top. If any of them matches, it picks another random number (as the color). For the tiles on the first row/column, I make my grid’s array indexes with negative numbers -1 so that it will not match.
What I have:
private function fillArray():void {
grid = new Array();
grid[-1] = new Array(-1,-1,-1,-1,-1); //paddles the grid[-1][0 to 4] with -1
for (var i = 0; i < HEIGHT; i++) {
grid[i] = new Array();
grid[i][-1] = -1; // paddles the -1 row with -1
for (var j = 0; j < WIDTH; j++) {
while (grid[i-1][j] == grid[i][j] || grid[i][-j] == grid[i][j]) { //while the current box is the same as those to the left or top
grid[i][j] = Math.floor(Math.random() * COLORS) + 1; //random number time!
}
}
}
}
This has a timeout error and grid is all -1. I don’t know what I am doing wrong here. Thanks for any help!
Should this line read:
Edit:
I really don’t understand why you need to start with negative indexes. It just makes it confusing for no reason. I rewrote your script changing just a couple minor things which you should be able to tweak yourself (the random color is just a random variation of red instead of actually random).