Пример #1
0
        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;
            }
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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.");
            }
        }
Пример #8
0
        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);
            });
        }
Пример #9
0
 public abstract void TurnSnake(DirectionOfMove newDirection);
Пример #10
0
 public void TurnSnake(DirectionOfMove newDirectionOfMove)
 {
     Head.ChangeDirection(newDirectionOfMove);
 }
Пример #11
0
 private static bool IsValidDirectionOfMove(DirectionOfMove directionOfMove)
 {
     return(directionOfMove == DirectionOfMove.Up || directionOfMove == DirectionOfMove.Down ||
            directionOfMove == DirectionOfMove.Right || directionOfMove == DirectionOfMove.Left);
 }
Пример #12
0
 public override void TurnSnake(DirectionOfMove newDirection)
 {
     _snake.TurnSnake(newDirection);
 }