private bool Move(Position startPosition, out Position position) { var length = this.possibleDirections.Length; for (int i = 0; i < length; i++) { var possibleDirection = this.possibleDirections[this.currentDirection]; var row = startPosition.Y + possibleDirection.Y; var column = startPosition.X + possibleDirection.X; if (this.matrix.IsValidCoordinate(row, column) && this.matrix[row, column] == 0) { position = new Position(column, row); return true; } this.currentDirection++; if (this.currentDirection == length) { this.currentDirection = 0; } } position = default(Position); return false; }
public void FirstOrDefaultZeroCellWithNotSetMatrix() { var matrix = new Matrix(3); var expected = new Position(0, 0); var actual = matrix.FirstOrDefaultZeroCell(); Assert.AreEqual(expected.X, actual.X); Assert.AreEqual(expected.Y, actual.Y); }
public void Start() { this.matrix = new Matrix(this.Size); Position currentPosition = new Position(0, 0); int cellValue = 0; do { this.currentDirection = 0; do { this.matrix[currentPosition.Y, currentPosition.X] = ++cellValue; } while (this.Move(currentPosition, out currentPosition)); currentPosition = this.matrix.FirstOrDefaultZeroCell(); } while (currentPosition != null); }