public void Traverse() { this.Clear(); int counter = 1; Position position = new Position(0, 0); Delta delta = new Delta(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++; } }
private bool TryFindNewPosition(out Position newPosition) { newPosition = new Position(0, 0); for (int row = 0; row < this.matrix.GetLength(0); row++) { for (int col = 0; col < this.matrix.GetLength(1); col++) { if (this.matrix[row, col] == 0) { newPosition.Row = row; newPosition.Col = col; return true; } } } return false; }
private bool CanContinue(Position position) { for (int i = 0; i < deltas.Length; i++) { if (this.CanGoToPosition(position.Row + deltas[i].Row, position.Col + deltas[i].Col)) { return true; } } return false; }