Пример #1
0
        private void GameIsOverWhenSnakeCollidesWithBottomWall()
        {
            IFoodFactory foodFactory = null;

            "Given a food factory"
            .x(() => foodFactory = A.Fake <IFoodFactory>());
            "And given a play area with boundaries 5 and 5"
            .x(() => this.testee = new Playarea(new PlayareaSize(5, 5), foodFactory));
            "And given the snake at position row 2 and column 2"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 2)));
            "And given the snake is facing downwards"
            .x(() => this.testee.UpdateSnakeDirection(Direction.Down));

            "When the snake moves"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake should have moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(3, 2)));
            "Then the game state should be running"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Running));

            "When the snake moves"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake should have moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(4, 2)));
            "Then the game state should be running"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Running));

            "When the snake moves into wall"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake should not have moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(4, 2)));
            "Then the game state should be running"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Over));
        }
Пример #2
0
        private void WhenTheSnakeCollidesInFruitItGrows()
        {
            IFoodFactory foodFactory = null;

            "Given a food factory"
            .x(() => foodFactory = A.Fake <IFoodFactory>());
            "And given the food will pops up in front of snake head"
            .x(() => A.CallTo(() => foodFactory.CreateRandomFoodBetweenBoundaries(A <Position> .Ignored)).Returns(new Apple(new Position(5, 4))).Once());
            "And given a play area with boundaries 10 and 5"
            .x(() => this.testee = new Playarea(new PlayareaSize(10, 5), foodFactory));
            "And given the snake at position row 5 and column 2"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(5, 2)));
            "And given the food is in front of the snake"
            .x(() => this.testee.Food.Position.Should().BeEquivalentTo(new Position(5, 4)));
            "And the snake is facing right wards"
            .x(() => this.testee.UpdateSnakeDirection(Direction.Right));

            "When the snake moves into an empty place"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake has moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(5, 3)));
            "and then the snake has not grown"
            .x(() => this.testee.Snake.Body.Count.Should().Be(0));

            "When the snake moves into the food"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake has moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(5, 4)));
            "and then the snake has grown"
            .x(() => this.testee.Snake.Body.Count.Should().Be(1));
        }
Пример #3
0
        private void NewlyCreatedPlaygroundShouldHaveStartGamestate()
        {
            // Act
            this.testee = new Playarea(new PlayareaSize(10, 10), A.Dummy <IFoodFactory>());

            // Assert
            this.testee.CurrentGameState.Should().Be(Game.Start);
        }
Пример #4
0
        private void NavigatePlayarea()
        {
            PlayareaSize playareaSize = new PlayareaSize(Convert.ToInt32(this.NumberOfRows), Convert.ToInt32(this.NumberOfColumns));
            FoodFactory  foodFactory  = new FoodFactory();

            Playarea playarea = new Playarea(playareaSize, foodFactory);

            this.NavigationService.NavigateTo(new PlayareaViewModel(this.NavigationService, playarea, this.DifficultyLevel));
        }
Пример #5
0
        private void WhenDirectionOfSnakeSetPlaygroundShouldHaveRunningGamestate(Direction newFacingDirection)
        {
            // Arrange
            this.testee = new Playarea(new PlayareaSize(10, 10), A.Dummy <IFoodFactory>());

            // Act
            this.testee.UpdateSnakeDirection(newFacingDirection);

            // Assert
            this.testee.CurrentGameState.Should().Be(Game.Running);
        }
Пример #6
0
        private void PlayareaSizeIsAtLeast4RowsAnd4Columns(int numberOfRows, int numberOfColumns)
        {
            // Arrange
            PlayareaSize size    = new PlayareaSize(numberOfRows, numberOfColumns);
            FoodFactory  factory = A.Dummy <FoodFactory>();

            // Act
            this.testee = new Playarea(size, factory);

            // Assert
            this.testee.Size.Should().BeEquivalentTo(new PlayareaSize(4, 4));
        }
Пример #7
0
 public PlayareaViewModel(INavigationService navigationService, Playarea playarea, int difficultyLevel)
 {
     this.NavigationService       = navigationService;
     this.database                = new Database(this.NavigationService.SessionToken);
     this.HighscoreViewModel      = new HighscoreViewModel(navigationService, this.database);
     this.GoToLoginView           = new RelayCommand(this.NavigateToLoginView);
     this.GoToMenuView            = new RelayCommand(this.NavigateToMenuView);
     this.playarea                = playarea;
     this.difficultyLevel         = difficultyLevel;
     this.gameSpeedInMilliSeconds = this.CalculateGameSpeed();
     this.InizializePlayarea();
     this.InizializeTimers();
 }
Пример #8
0
        private void SnakeShouldNotMoveWhenDirectionIsNeverUpdated()
        {
            // Arrange
            PlayareaSize size        = new PlayareaSize(10, 10);
            IFoodFactory foodFactory = A.Dummy <IFoodFactory>();

            this.testee = new Playarea(size, foodFactory);
            Position previousHeadPosition = this.testee.Snake.Head.Position;

            // Act
            this.testee.MoveSnakeWhenAllowed();

            // Assert
            this.testee.Snake.Head.Position.Should().BeEquivalentTo(previousHeadPosition);
        }
Пример #9
0
        private void SnakeCannotMoveWhenTheGameIsOver()
        {
            IFoodFactory foodFactory = null;

            "Given a food factory"
            .x(() => foodFactory = A.Dummy <IFoodFactory>());
            "And given a play area"
            .x(() => this.testee = new Playarea(new PlayareaSize(4, 5), foodFactory));
            "And given the snake at position row 2 and column 1"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 2)));
            "And the game state is start"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Start));

            "When the direction of the snake is updated for the first time"
            .x(() => this.testee.UpdateSnakeDirection(Direction.Right));
            "Then the game state should be running"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Running));

            "When the snake moves"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake should have moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 3)));
            "Then the game state should be running"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Running));

            "When the snake moves"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake should have moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 4)));
            "Then the game state should be running"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Running));

            "When the snake moves outside of playarea"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake should not have moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 4)));
            "And then the game is over"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Over));

            "When the snake tries to move"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "And when the game is over"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Over));
            "Then the snake should not have moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 4)));
        }
Пример #10
0
        private void SnakeCannotBeResetUnlessTheGameIsOver()
        {
            IFoodFactory foodFactory = null;

            "Given a food factory"
            .x(() => foodFactory = A.Fake <IFoodFactory>());
            "And given a play area with size 4 by 4"
            .x(() => this.testee = new Playarea(new PlayareaSize(4, 4), foodFactory));
            "And given the snake at position row 2 and column 2"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 2)));
            "And the game state is start"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Start));

            "When the direction of the snake is updated for the first time"
            .x(() => this.testee.UpdateSnakeDirection(Direction.Right));
            "Then the game state should be running"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Running));

            "When the snake tries to be reset without being GameOver"
            .x(() => this.testee.RestartGame());
            "Then nothing happens"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 2)));

            "when the snake moves"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake should have moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 3)));
            "When the snake tries to be reset without being GameOver"
            .x(() => this.testee.RestartGame());
            "Then nothing happens"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 3)));

            "When the snake moves"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake should not have moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 3)));
            "And then the game is over"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Over));

            "When the snake tries to be reset with the game state being GameOVer"
            .x(() => this.testee.RestartGame());
            "Then the snake is reset"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(2, 2)));
        }
Пример #11
0
        private void GameIsOverWhenSnakeCollidesWithItself()
        {
            IFoodFactory foodFactory = null;

            "Given a food factory"
            .x(() => foodFactory = A.Fake <IFoodFactory>());
            "And given the food will pops up in front of snake head"
            .x(() => A.CallTo(() => foodFactory.CreateRandomFoodBetweenBoundaries(A <Position> .Ignored))
               .ReturnsLazily(this.CalculateNewFoodPosition));
            "And given a play area with boundaries 10 and 12"
            .x(() => this.testee = new Playarea(new PlayareaSize(10, 12), foodFactory));
            "And given the snake at position row 5 and column 6"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(5, 6)));
            "And given the food is actually in front of the snake"
            .x(() => this.testee.Food.Position.Should().BeEquivalentTo(new Position(5, 7)));
            "And the snake is facing rightwards"
            .x(() => this.testee.UpdateSnakeDirection(Direction.Right));


            "When the snake moves into the food"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake has moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(5, 7)));
            "and then the snake has grown"
            .x(() => this.testee.Snake.Body.Count.Should().Be(1));

            "When the food is actually in front of the snake"
            .x(() => this.testee.Food.Position.Should().BeEquivalentTo(new Position(5, 8)));
            "And the snake moves into the food"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake has moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(5, 8)));
            "and then the snake has grown"
            .x(() => this.testee.Snake.Body.Count.Should().Be(2));

            "When the food is actually in front of the snake"
            .x(() => this.testee.Food.Position.Should().BeEquivalentTo(new Position(5, 9)));
            "When the snake moves into the food"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake has moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(5, 9)));
            "and then the snake has grown"
            .x(() => this.testee.Snake.Body.Count.Should().Be(3));

            "When the food is actually in front of the snake"
            .x(() => this.testee.Food.Position.Should().BeEquivalentTo(new Position(5, 10)));
            "When the snake moves into the food"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake has moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(5, 10)));
            "and then the snake has grown"
            .x(() => this.testee.Snake.Body.Count.Should().Be(4));

            "When the snake changes direction by 90 degrees"
            .x(() => this.testee.UpdateSnakeDirection(Direction.Up));
            "And when the snake moves"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake has moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(4, 10)));

            "When the snake changes direction by 90 degrees"
            .x(() => this.testee.UpdateSnakeDirection(Direction.Left));
            "And when the snake moves"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake has moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(4, 9)));

            "When the snake changes direction by 90 degrees"
            .x(() => this.testee.UpdateSnakeDirection(Direction.Down));
            "And when the snake moves"
            .x(() => this.testee.MoveSnakeWhenAllowed());
            "Then the snake should have collided with itself and the Game is over"
            .x(() => this.testee.CurrentGameState.Should().Be(Game.Over));
            "Then the snake has not moved"
            .x(() => this.testee.Snake.Head.Position.Should().BeEquivalentTo(new Position(4, 9)));
        }
Пример #12
0
 public void SetUp()
 {
     this.testee = null;
 }