public static void FillPath(Matrix matrix) { int step = 1; MatrixCell position = new MatrixCell(0, 0); MatrixCell cellChange = Direction[0]; while (step <= matrix.Size * matrix.Size) { while (CanMove(matrix, position)) { matrix.SetCellValue(position, step); position += cellChange; step++; } position -= cellChange; if (!IsEmptyNeighbour(matrix, position) && matrix.FindCell(0) != null) { position = matrix.FindCell(0); cellChange = Direction[0]; } else { cellChange = ChangeDirection(cellChange); position += cellChange; } } }
public static bool CanMove(Matrix matrix, MatrixCell cell) { if (!matrix.IsInMatrix(cell) || matrix.GetCellValue(cell) != 0) { return false; } return true; }
public int GetCellValue(MatrixCell cell) { int result = 0; for (int i = 0; i < this.Size; i++) { for (int j = 0; j < this.Size; j++) { if (cell.Row == i && cell.Col == j) { result = this.matrix[i, j]; } } } return result; }
public static bool IsEmptyNeighbour(Matrix matrix, MatrixCell cell) { MatrixCell next = new MatrixCell(0, 0); for (int i = 0; i < Direction.Length; i++) { next = cell + Direction[i]; if (CanMove(matrix, next)) { if (matrix.GetCellValue(next) == 0) { return true; } } } return false; }
public static MatrixCell ChangeDirection(MatrixCell cellChange) { int curr = Array.IndexOf(Direction, cellChange); int next = (curr + 1) % Direction.Length; return Direction[next]; }
public void SetCellValue(MatrixCell cell, int value) { for (int i = 0; i < this.Size; i++) { for (int j = 0; j < this.Size; j++) { if (cell.Row == i && cell.Col == j) { this.matrix[i, j] = value; } } } }
public bool IsInMatrix(MatrixCell cell) { bool rowIsInRange = cell.Row >= 0 && cell.Row < this.Size; bool colIsInRange = cell.Col >= 0 && cell.Col < this.Size; return rowIsInRange && colIsInRange; }