コード例 #1
0
        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);
        }
コード例 #2
0
        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>();
        }
コード例 #3
0
        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>();
        }
コード例 #4
0
        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");
            }
        }