mBlocks is a 2-dimensional array of Block objects. Every time my application runs, it runs the InitGridNumbers function. Sometimes, it will get stuck in an infinite loop. Other times, it builds and runs without issues.
public function InitGridNumbers():void
{
var tempRow:Array;
var tempColumn:Array;
var tempNum:int;
for (var i:int = 0; i < mNumRows; i++)
{
tempRow = GetRow(i);
for (var j:int = 0; j < mNumColumns; j++)
{
// if number is unassigned
if (tempRow[j] == 0)
{
var cantMoveOn:Boolean = true;
while (cantMoveOn)
{
tempNum = Math.random() * mNumColumns + 1;
if (!CheckRow(i, tempNum) && !CheckColumn(j, tempNum))
cantMoveOn = false;
}
mBlocks[i][j].SetNumber(tempNum);
}
}
}
}
public function CheckRow(rowNum:int, checkNum:int):Boolean
{
var tempRow:Array = GetRow(rowNum);
for (var i:int = 0; i < mNumColumns; i++)
{
if (checkNum == tempRow[i])
return true;
}
return false;
}
public function CheckColumn(columnNum:int, checkNum:int):Boolean
{
var tempColumn:Array = GetColumn(columnNum);
for (var i:int = 0; i < mNumColumns; i++)
{
if (checkNum == tempColumn[i])
return true;
}
return false;
}
public function GetRow(rowNum:int):Array
{
var rowArray:Array = new Array(mNumRows);
for (var i:int = 0; i < mNumRows; i++)
rowArray[i] = mBlocks[rowNum][i].mNumber;
return rowArray;
}
public function GetColumn(columnNum:int):Array
{
var columnArray:Array = new Array(mNumColumns);
for (var i:int = 0; i < mNumColumns; i++)
columnArray[i] = mBlocks[i][columnNum].mNumber;
return columnArray;
}