public void ChangeDirection(DirectionOfMove newDirection) { if (IsValidDirectionOfMove(newDirection) == false) { throw new ArgumentOutOfRangeException(nameof(DirectionOfMove), "Invalid direction of move value."); } switch (newDirection) { case DirectionOfMove.Right when DirectionOfMove != DirectionOfMove.Left: DirectionOfMove = newDirection; break; case DirectionOfMove.Up when DirectionOfMove != DirectionOfMove.Down: DirectionOfMove = newDirection; break; case DirectionOfMove.Left when DirectionOfMove != DirectionOfMove.Right: DirectionOfMove = newDirection; break; case DirectionOfMove.Down when DirectionOfMove != DirectionOfMove.Up: DirectionOfMove = newDirection; break; } }
public override void TurnSnake(DirectionOfMove newDirection) { if (_doesSnakeMoved) { Monitor.Enter(_snake); _snake.TurnSnake(newDirection); Monitor.Exit(_snake); _doesSnakeMoved = false; } }
public void ChangeDirectionToUpValidConditionsTest() { // arrange const DirectionOfMove expected = DirectionOfMove.Up; var snakeElement = new SnakeElement(1, 1, DirectionOfMove.Right); // act snakeElement.ChangeDirection(expected); snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DirectionOfMove); }
public void ChangeDirectionToDownWhenCurrentIsUpTest() { // arrange const DirectionOfMove expected = DirectionOfMove.Up; var snakeElement = new SnakeElement(1, 1, DirectionOfMove.Up); // act snakeElement.ChangeDirection(DirectionOfMove.Down); snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DirectionOfMove); }
public void ChangeDirectionToLeftWhenCurrentIsRightTest() { // arrange const DirectionOfMove expected = DirectionOfMove.Right; var snakeElement = new SnakeElement(1, 1, DirectionOfMove.Right); // act snakeElement.ChangeDirection(DirectionOfMove.Left); snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DirectionOfMove); }
public void ChangeDirectionToDownValidConditionsTest() { // arrange const DirectionOfMove expected = DirectionOfMove.Down; var snakeElement = new SnakeElement(0, 0, DirectionOfMove.Left); // act snakeElement.ChangeDirection(expected); snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DirectionOfMove); }
public SnakeElement(int distanceFromLeft, int distanceFromTop, DirectionOfMove directionOfMove) { DistanceFromLeft = distanceFromLeft; DistanceFromTop = distanceFromTop; if (IsValidDirectionOfMove(directionOfMove)) { DirectionOfMove = directionOfMove; } else { throw new ArgumentOutOfRangeException(nameof(DirectionOfMove), "Invalid direction of move value."); } }
public void CreateSnakeElementBehindDirectionIsDownTest() { // arrange const DirectionOfMove expectedDirection = DirectionOfMove.Down; const int expectedDistanceFromLeft = 1; const int expectedDistanceFromTop = 1; var snakeElement = new SnakeElement(expectedDistanceFromLeft, expectedDistanceFromTop + 1, expectedDirection); // act var newSnakeElement = snakeElement.CreateSnakeElementBehind(); // assert Assert.Multiple(() => { Assert.AreEqual(expectedDistanceFromLeft, newSnakeElement.DistanceFromLeft); Assert.AreEqual(expectedDistanceFromTop, newSnakeElement.DistanceFromTop); Assert.AreEqual(expectedDirection, newSnakeElement.DirectionOfMove); }); }
public abstract void TurnSnake(DirectionOfMove newDirection);
public void TurnSnake(DirectionOfMove newDirectionOfMove) { Head.ChangeDirection(newDirectionOfMove); }
private static bool IsValidDirectionOfMove(DirectionOfMove directionOfMove) { return(directionOfMove == DirectionOfMove.Up || directionOfMove == DirectionOfMove.Down || directionOfMove == DirectionOfMove.Right || directionOfMove == DirectionOfMove.Left); }
public override void TurnSnake(DirectionOfMove newDirection) { _snake.TurnSnake(newDirection); }