static void FindCellToStartFrom( int[,] arr, Position position ) { position.X = 0; position.Y = 0; int arraySize = arr.GetLength(0); for (int i = 0; i < arraySize; i++) { for (int j = 0; j < arraySize; j++) { if (arr[i, j] == 0) { position.X = i; position.Y = j; return; } } } }
static bool CalculateNextDirectionIfPossible( int[,] arr, Position position , DirectionVector currentDirection) { int nextX; int nextY; int arraySize = arr.GetLength(0); for (int i = 0; i < DirectionVector.DirectionsCount; i++) { nextX = position.X + currentDirection.Dx; nextY = position.Y + currentDirection.Dy; if (IsInRange(arraySize, nextX, nextY) && arr[nextX, nextY] == 0) { return true; } currentDirection.Next(); } return false; }
public static int[,] CalculateMatrix(int arraySize) { int[,] matrix = new int[arraySize, arraySize]; int maxValue = arraySize * arraySize; Position position = new Position(); DirectionVector direction = new DirectionVector(); for (int value = 1; value <= maxValue; value++) { matrix[position.X, position.Y] = value; if (CalculateNextDirectionIfPossible(matrix, position, direction)) { position.X += direction.Dx; position.Y += direction.Dy; } else { FindCellToStartFrom(matrix, position); direction.Reset(); } } return matrix; }
public void TestInitialization() { Position position = new Position(); Assert.AreEqual(position.X == 0 && position.Y == 0, true, "Begin values not assigned correctly"); }