public void GetRelativeDirectionShould_CalculatesRelativeMoveDirectionCorrectly(int firstX, int firstY, int secondX, int secondY, MoveDirection expectedDirection) { var currentBit = new SnakeBit { X = firstX, Y = firstY }; var nextBit = new SnakeBit { X = secondX, Y = secondY }; var direction = currentBit.GetRelativeDirection(nextBit); direction.Should().Be(expectedDirection); }
public void GetRelativeDirectionShould_Throw_ArgumentException_When_NextPositionIsSameAsCurrent() { Action action = () => { var current = new SnakeBit { X = 7, Y = 10 }; var next = new SnakeBit { X = 7, Y = 10 }; current.GetRelativeDirection(next); }; action.Should().Throw <ArgumentException>(); }
public void GetRelativeDirectionShould_Throw_NotSupportedException_When_NextIsDiagonallyPositioned() { Action action = () => { var current = new SnakeBit { X = 7, Y = 10 }; var next = new SnakeBit { X = 8, Y = 11 }; current.GetRelativeDirection(next); }; action.Should().Throw <NotSupportedException>(); }
public static MoveDirection GetRelativeDirection(this SnakeBit current, SnakeBit next) { if (current.X == next.X && current.Y == next.Y) { throw new ArgumentException("Current and next cannot be in the same position"); } if (current.X == next.X) { // Moving vertically return(current.Y < next.Y ? MoveDirection.Up : MoveDirection.Down); } else if (current.Y == next.Y) { // Moving horizontally return(current.X < next.X ? MoveDirection.Left : MoveDirection.Right); } else { throw new NotSupportedException("Cannot move diagonally"); } }