/// <summary> /// Find(recursively) the shortest empty place for the new figure angleUL and save it corrdinates in global field /// bestRow and bestCol /// </summary> /// <param name="matrix">the matrix field where the figures are placed</param> /// <param name="startRow">row cordinates of the new figure</param> /// <param name="startCol">col cordinates of the new figure</param> public static void FillAngleUl(Cell[,] matrix, int startRow, int startCol) { if ((startRow < 1) || (startRow > matrix.GetLength(0) - 1) || (startCol < 1) || (startCol > matrix.GetLength(1) - 1)) //borders of the matrix for angleUL { return; } if (matrix[startRow, startCol].isVisited) //if cell is visited return { return; } else { matrix[startRow, startCol].isVisited = true; currentVisitedCells.Add(new KeyValuePair<int, int>(startRow, startCol)); } if (matrix[startRow, startCol].isFree && matrix[startRow, startCol - 1].isFree && matrix[startRow - 1, startCol].isFree)//if empty place found { int currenrBestRow = startRow; int currentBestCol = startCol; int currentLength = Math.Abs(startRow - rowToPlace) + Math.Abs(startCol - colToPlace); if (currentLength < bestLength) //fix only if current length is shorter than the shortest(bestLength) { bestLength = currentLength; bestRow = currenrBestRow; bestCol = currentBestCol; } return; } FillAngleUl(matrix, startRow + 1, startCol); //down direction FillAngleUl(matrix, startRow - 1, startCol); //up direction FillAngleUl(matrix, startRow, startCol - 1); //left direction FillAngleUl(matrix, startRow, startCol + 1); //right direction }
public static void FillCellMatrix(Cell[,] matrix) { for (int i = 0, len = matrix.GetLength(0); i < len; i += 1) { for (int j = 0; j < len; j += 1) { matrix[i, j] = new Cell(); } } }