static Matrix() { int directionsCount = Location.DirectionsCount; deltas = new Location[directionsCount]; for (int i = 0; i < directionsCount; i++) { deltas[i] = new Location((Direction)i); } }
/// <summary> /// Fills the matrix with consecutive integer values /// that correspond to the route of traversal. /// </summary> public void Traverse() { this.Clear(); int counter = 1; Position position = new Position(0, 0); Location delta = new Location(Direction.Southeast); while (true) { this.matrix[position.Row, position.Col] = counter; if (!this.CanContinue(position)) { bool newPositionFound = this.TryFindNewPosition(out position); if (newPositionFound) { counter++; this.matrix[position.Row, position.Col] = counter; delta.Direction = Direction.Southeast; } else { break; } } while (!this.CanGoToPosition(position.Row + delta.Row, position.Col + delta.Col)) { delta.UpdateDirectionClockwise(); } position.Update(delta); counter++; } }