Exemplo n.º 1
0
    public void RunTestMissingConfig()
    {
        var loggerDouble            = new LoggerDouble <App>();
        var mockConfig              = new Mock <IConfiguration>();
        var mockGameplayCoordinator = new Mock <ICoordinateGameplay>();
        var statsGathererMock       = new Mock <IFinalizeStats>();
        var dummySimulationStats    = new Mock <ISimulationStats>();

        var configurationSection = new Mock <IConfigurationSection>();

        mockConfig.Setup(a => a.GetSection(It.IsAny <string>())).Returns(configurationSection.Object);
        mockGameplayCoordinator.Setup(mock => mock.Play(It.IsAny <GameParameters>())).Returns(dummySimulationStats.Object);

        var objectUnderTest = new App(mockConfig.Object, loggerDouble, mockGameplayCoordinator.Object, statsGathererMock.Object);

        objectUnderTest.Run();

        mockGameplayCoordinator.Verify(mock => mock.Play(
                                           It.Is <GameParameters>(
                                               item => item.FirstPlayerName == "Player 1" &&
                                               item.SecondPlayerName == "Player 2" &&
                                               item.TotalLengthOfSimulation == TimeSpan.FromMinutes(60 * 24)
                                               )), Times.Once());
        statsGathererMock.Verify(mock => mock.GetFinalStats(It.IsAny <GameParameters>(), dummySimulationStats.Object), Times.Once());
    }
Exemplo n.º 2
0
    public void TransitionTestShufflingTakesTimeAndCanTimeOutGame()
    {
        var loggerDouble = new LoggerDouble <PlayWar>();

        var firstPlayerState = new PlayerGameState
        {
            GatherPile = new CardPile {
                Cards = new List <Card> {
                    new Card {
                        Rank = "K", Suit = Constants.Suits.Hearts
                    }
                }
            },
            PlayPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            }
        };
        var secondPlayerState = new PlayerGameState
        {
            GatherPile = new CardPile {
                Cards = new List <Card> {
                    new Card {
                        Rank = "3", Suit = Constants.Suits.Clubs
                    }
                }
            },
            PlayPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            }
        };

        var gameStateMock = new Mock <ITrackIndividualGameState>();

        gameStateMock.Setup(mock => mock.FirstPlayerState).Returns(firstPlayerState);
        gameStateMock.Setup(mock => mock.SecondPlayerState).Returns(secondPlayerState);
        var maxDuration = TimeSpan.FromMinutes(5);

        gameStateMock.Setup(mock => mock.MaxDuration).Returns(maxDuration);
        var timeElapsedInGame = new TimeSpan(hours: 0, minutes: 4, seconds: 59);

        gameStateMock.Setup(mock => mock.TimeElapsed).Returns(() => timeElapsedInGame);
        gameStateMock.Setup(mock => mock.Tick(It.IsAny <TimeSpan>()))
        .Callback <TimeSpan>((timeTaken) => timeElapsedInGame += timeTaken);
        gameStateMock.Setup(mock => mock.TimedOut).Returns(() => timeElapsedInGame > maxDuration);

        var objectUnderTest = new PlayWar(loggerDouble);

        //ensure we haven't timed out yet and the simulation will make us time out
        Assert.True(gameStateMock.Object.TimedOut == false);

        objectUnderTest.Transition(gameStateMock.Object);
        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToShuffle), Times.Once());
        Assert.True(loggerDouble.TraceEntries.Count() == 6);
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Trace, "After transition, player 1 has 1 card(s) in their play pile"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Trace, "After transition, player 1 has 0 card(s) in their gather pile"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Trace, "After transition, player 1 has 0 card(s) in their played cards pile"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Trace, "After transition, player 2 has 1 card(s) in their play pile"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Trace, "After transition, player 2 has 0 card(s) in their gather pile"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Trace, "After transition, player 2 has 0 card(s) in their played cards pile"));
        Assert.True(loggerDouble.DebugEntries.Count() == 4);
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "Moving 1 card(s) from gather pile to play pile for player 1..."));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "Moving 1 card(s) from gather pile to play pile for player 2..."));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "Total time taken so far is 314 seconds"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "Game over after 314 seconds because max mins is 5"));
    }
Exemplo n.º 3
0
    public void WarTestAlmostImpossibleCase()
    {
        var loggerDouble = new LoggerDouble <PlayWar>();

        var firstPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Clubs
                    },
                    new Card {
                        Rank = "K", Suit = Constants.Suits.Diamonds
                    },
                    new Card {
                        Rank = "K", Suit = Constants.Suits.Hearts
                    },
                    new Card {
                        Rank = "K", Suit = Constants.Suits.Clubs
                    },
                }
            },
            GatherPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Diamonds
                    },
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            }
        };
        var secondPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Spades
                    },
                    new Card {
                        Rank = "Q", Suit = Constants.Suits.Diamonds
                    },
                    new Card {
                        Rank = "Q", Suit = Constants.Suits.Hearts
                    },
                    new Card {
                        Rank = "Q", Suit = Constants.Suits.Clubs
                    },
                }
            },
            GatherPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Hearts
                    },
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            }
        };

        var gameStateMock = new Mock <ITrackIndividualGameState>();

        gameStateMock.Setup(mock => mock.FirstPlayerState).Returns(firstPlayerState);
        gameStateMock.Setup(mock => mock.SecondPlayerState).Returns(secondPlayerState);

        var objectUnderTest = new PlayWar(loggerDouble);

        bool exceptionThrown = false;

        try
        {
            objectUnderTest.War(gameStateMock.Object, warDepth: 1);
        }
        catch
        {
            exceptionThrown = true;
        }

        Assert.True(exceptionThrown);
    }
Exemplo n.º 4
0
    public void WarTestNotEnoughCardsForWar(int winner)
    {
        var loggerDouble = new LoggerDouble <PlayWar>();

        var extraCard = new Card {
            Rank = "K", Suit = Constants.Suits.Hearts
        };

        var firstPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "5", Suit = Constants.Suits.Diamonds
                    },
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Clubs
                    },
                    new Card {
                        Rank = "7", Suit = Constants.Suits.Spades
                    },
                    new Card {
                        Rank = "4", Suit = Constants.Suits.Diamonds
                    }
                }
            },
            GatherPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            },
        };
        var secondPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "5", Suit = Constants.Suits.Hearts
                    },
                    new Card {
                        Rank = "2", Suit = Constants.Suits.Clubs
                    },
                    new Card {
                        Rank = "Q", Suit = Constants.Suits.Hearts
                    },
                    new Card {
                        Rank = "J", Suit = Constants.Suits.Diamonds
                    }
                }
            },
            GatherPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            },
        };
        PlayerGameState winnerState;
        PlayerGameState loserState;

        if (winner == 1)
        {
            winnerState = firstPlayerState;
            loserState  = secondPlayerState;
        }
        else
        {
            winnerState = secondPlayerState;
            loserState  = firstPlayerState;
        }
        winnerState.GatherPile.Cards.Add(extraCard);

        var gameStateMock = new Mock <ITrackIndividualGameState>();

        gameStateMock.Setup(mock => mock.FirstPlayerState).Returns(firstPlayerState);
        gameStateMock.Setup(mock => mock.SecondPlayerState).Returns(secondPlayerState);

        var objectUnderTest = new PlayWar(loggerDouble);

        Assert.True(firstPlayerState.CannotContinueBecauseCantPlayEnoughCardsForWar == false);
        Assert.True(secondPlayerState.CannotContinueBecauseCantPlayEnoughCardsForWar == false);

        objectUnderTest.War(gameStateMock.Object);

        Assert.True(winnerState.CannotContinueBecauseCantPlayEnoughCardsForWar == false);
        Assert.True(loserState.CannotContinueBecauseCantPlayEnoughCardsForWar == true);

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player {(winner == 1 ? 2 : 1)} does not have enough cards for war..."));
    }
Exemplo n.º 5
0
    public void WarTestSimpleWarWhenBothNeedToShuffle()
    {
        var loggerDouble        = new LoggerDouble <PlayWar>();
        var singleGameStatsMock = new Mock <ISingleGameStats>();

        var firstPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "5", Suit = Constants.Suits.Diamonds
                    },
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Clubs
                    },
                }
            },
            GatherPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "7", Suit = Constants.Suits.Spades
                    },
                    new Card {
                        Rank = "4", Suit = Constants.Suits.Diamonds
                    },
                    new Card {
                        Rank = "K", Suit = Constants.Suits.Hearts
                    }
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            },
        };

        // player 2 should always lose because he's always gonna play the 2 in the 2nd war
        var secondPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "5", Suit = Constants.Suits.Hearts
                    },
                    new Card {
                        Rank = "Q", Suit = Constants.Suits.Hearts
                    },
                    new Card {
                        Rank = "J", Suit = Constants.Suits.Diamonds
                    },
                    new Card {
                        Rank = "3", Suit = Constants.Suits.Clubs
                    }
                }
            },
            GatherPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "2", Suit = Constants.Suits.Clubs
                    },
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            },
        };

        var gameStateMock = new Mock <ITrackIndividualGameState>();

        gameStateMock.Setup(mock => mock.FirstPlayerState).Returns(firstPlayerState);
        gameStateMock.Setup(mock => mock.SecondPlayerState).Returns(secondPlayerState);

        var player1WarWinsByDepthTracker = new List <int>()
        {
            0, 0, 0, 0, 0, 0, 0, 0
        };

        singleGameStatsMock.Setup(mock => mock.Player1WarWinsByDepth).Returns(player1WarWinsByDepthTracker);
        gameStateMock.Setup(mock => mock.Stats).Returns(singleGameStatsMock.Object);

        var objectUnderTest = new PlayWar(loggerDouble);

        objectUnderTest.War(gameStateMock.Object);

        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToPlayCard), Times.Exactly(2));
        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToPlayCardsFaceDownForAWar), Times.Once());
        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToShuffle), Times.Once());
        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToGatherCards), Times.Once());

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 1 wins"));
        Assert.True(player1WarWinsByDepthTracker[1] == 1);
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"War: 5♦ vs 5♥..."));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "War with depth 0 on this turn."));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "Moving 3 card(s) from gather pile to play pile for player 1..."));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "Moving 1 card(s) from gather pile to play pile for player 2..."));
        Assert.True(loggerDouble.DebugEntries.Count() == 14);
        Assert.True(firstPlayerState.GatherPile.Cards.Count == 10);
        Assert.True(firstPlayerState.PlayedCards.Cards.Count == 0);
        Assert.True(firstPlayerState.PlayPile.Cards.Count == 0);
        Assert.True(secondPlayerState.GatherPile.Cards.Count == 0);
        Assert.True(secondPlayerState.PlayedCards.Cards.Count == 0);
        Assert.True(secondPlayerState.PlayPile.Cards.Count == 0);
    }
Exemplo n.º 6
0
    public void WarTestSimpleWar(int winner)
    {
        var loggerDouble        = new LoggerDouble <PlayWar>();
        var singleGameStatsMock = new Mock <ISingleGameStats>();

        var winningCard = new Card {
            Rank = "K", Suit = Constants.Suits.Hearts
        };
        var losingCard = new Card {
            Rank = "3", Suit = Constants.Suits.Clubs
        };

        var firstPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "5", Suit = Constants.Suits.Diamonds
                    },
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Clubs
                    },
                    new Card {
                        Rank = "7", Suit = Constants.Suits.Spades
                    },
                    new Card {
                        Rank = "4", Suit = Constants.Suits.Diamonds
                    },
                    winner == 1 ? winningCard : losingCard
                }
            },
            GatherPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            },
        };
        var secondPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "5", Suit = Constants.Suits.Hearts
                    },
                    new Card {
                        Rank = "2", Suit = Constants.Suits.Clubs
                    },
                    new Card {
                        Rank = "Q", Suit = Constants.Suits.Hearts
                    },
                    new Card {
                        Rank = "J", Suit = Constants.Suits.Diamonds
                    },
                    winner == 2 ? winningCard : losingCard
                }
            },
            GatherPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            },
        };

        var gameStateMock = new Mock <ITrackIndividualGameState>();

        gameStateMock.Setup(mock => mock.FirstPlayerState).Returns(firstPlayerState);
        gameStateMock.Setup(mock => mock.SecondPlayerState).Returns(secondPlayerState);

        var player1WarWinsByDepthTracker = new List <int>()
        {
            0, 0, 0, 0, 0, 0, 0, 0
        };
        var player2WarWinsByDepthTracker = new List <int>()
        {
            0, 0, 0, 0, 0, 0, 0, 0
        };

        singleGameStatsMock.Setup(mock => mock.Player1WarWinsByDepth).Returns(player1WarWinsByDepthTracker);
        singleGameStatsMock.Setup(mock => mock.Player2WarWinsByDepth).Returns(player2WarWinsByDepthTracker);
        gameStateMock.Setup(mock => mock.Stats).Returns(singleGameStatsMock.Object);

        var objectUnderTest = new PlayWar(loggerDouble);

        objectUnderTest.War(gameStateMock.Object);

        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToPlayCard), Times.Exactly(2));
        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToPlayCardsFaceDownForAWar), Times.Once());
        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToShuffle), Times.Never());
        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToGatherCards), Times.Once());

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player {winner} wins"));
        Assert.True(player1WarWinsByDepthTracker[1] == (winner == 1 ? 1 : 0));
        Assert.True(player2WarWinsByDepthTracker[1] == (winner == 2 ? 1 : 0));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"War: 5♦ vs 5♥..."));
        if (winner == 1)
        {
            Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"War: {winningCard} vs {losingCard}..."));
        }
        else
        {
            Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"War: {losingCard} vs {winningCard}..."));
        }
        var player1verb = (winner == 1 ? "saves" : "loses");
        var player2verb = (winner == 2 ? "saves" : "loses");

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "War with depth 0 on this turn."));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 1 {player1verb}: 5♦"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 1 {player1verb}: A♣"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 1 {player1verb}: 7♠"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 1 {player1verb}: 4♦"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 2 {player2verb}: 5♥"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 2 {player2verb}: 2♣"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 2 {player2verb}: Q♥"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 2 {player2verb}: J♦"));
        Assert.True(loggerDouble.DebugEntries.Count() == 12);
        Assert.True(firstPlayerState.GatherPile.Cards.Count == (winner == 1 ? 10 : 0));
        Assert.True(firstPlayerState.PlayedCards.Cards.Count == 0);
        Assert.True(firstPlayerState.PlayPile.Cards.Count == 0);
        Assert.True(secondPlayerState.GatherPile.Cards.Count == (winner == 2 ? 10 : 0));
        Assert.True(secondPlayerState.PlayedCards.Cards.Count == 0);
        Assert.True(secondPlayerState.PlayPile.Cards.Count == 0);
    }
Exemplo n.º 7
0
    public void WarTestWinnerAfterSingleDepthWar(int winner)
    {
        var loggerDouble        = new LoggerDouble <PlayWar>();
        var singleGameStatsMock = new Mock <ISingleGameStats>();

        var winningCard = new Card {
            Rank = "K", Suit = Constants.Suits.Hearts
        };
        var losingCard = new Card {
            Rank = "3", Suit = Constants.Suits.Clubs
        };

        var firstPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile {
                Cards = new List <Card> {
                    winner == 1 ? winningCard : losingCard
                }
            },
            GatherPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Clubs
                    },
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Spades
                    },
                    new Card {
                        Rank = "A", Suit = Constants.Suits.Hearts
                    },
                }
            }
        };
        var secondPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile {
                Cards = new List <Card> {
                    winner == 2 ? winningCard : losingCard
                }
            },
            GatherPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile
            {
                Cards = new List <Card> {
                    new Card {
                        Rank = "2", Suit = Constants.Suits.Clubs
                    },
                    new Card {
                        Rank = "2", Suit = Constants.Suits.Spades
                    },
                    new Card {
                        Rank = "2", Suit = Constants.Suits.Hearts
                    },
                }
            }
        };

        var gameStateMock = new Mock <ITrackIndividualGameState>();

        gameStateMock.Setup(mock => mock.FirstPlayerState).Returns(firstPlayerState);
        gameStateMock.Setup(mock => mock.SecondPlayerState).Returns(secondPlayerState);

        var player1WarWinsByDepthTracker = new List <int>()
        {
            0, 0, 0, 0, 0, 0, 0, 0
        };
        var player2WarWinsByDepthTracker = new List <int>()
        {
            0, 0, 0, 0, 0, 0, 0, 0
        };

        singleGameStatsMock.Setup(mock => mock.Player1WarWinsByDepth).Returns(player1WarWinsByDepthTracker);
        singleGameStatsMock.Setup(mock => mock.Player2WarWinsByDepth).Returns(player2WarWinsByDepthTracker);
        gameStateMock.Setup(mock => mock.Stats).Returns(singleGameStatsMock.Object);

        var objectUnderTest = new PlayWar(loggerDouble);

        objectUnderTest.War(gameStateMock.Object, warDepth: 1);

        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToPlayCard), Times.Once());
        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToGatherCards), Times.Once());

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player {winner} wins"));
        Assert.True(player1WarWinsByDepthTracker[1] == (winner == 1 ? 1 : 0));
        Assert.True(player2WarWinsByDepthTracker[1] == (winner == 2 ? 1 : 0));
        if (winner == 1)
        {
            Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"War: {winningCard} vs {losingCard}..."));
        }
        else
        {
            Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"War: {losingCard} vs {winningCard}..."));
        }
        var player1verb = (winner == 1 ? "saves" : "loses");
        var player2verb = (winner == 2 ? "saves" : "loses");

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 1 {player1verb}: A♣"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 1 {player1verb}: A♥"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 1 {player1verb}: A♠"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 2 {player2verb}: 2♣"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 2 {player2verb}: 2♥"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player 2 {player2verb}: 2♠"));
        Assert.True(loggerDouble.DebugEntries.Count() == 8);
        Assert.True(firstPlayerState.GatherPile.Cards.Count == (winner == 1 ? 8 : 0));
        Assert.True(firstPlayerState.PlayedCards.Cards.Count == 0);
        Assert.True(firstPlayerState.PlayPile.Cards.Count == 0);
        Assert.True(secondPlayerState.GatherPile.Cards.Count == (winner == 2 ? 8 : 0));
        Assert.True(secondPlayerState.PlayedCards.Cards.Count == 0);
        Assert.True(secondPlayerState.PlayPile.Cards.Count == 0);
    }
Exemplo n.º 8
0
    public void WarTestSimpleCase(int winner)
    {
        var loggerDouble = new LoggerDouble <PlayWar>();

        var winningCard = new Card {
            Rank = "K", Suit = Constants.Suits.Hearts
        };
        var losingCard = new Card {
            Rank = "3", Suit = Constants.Suits.Clubs
        };

        var firstPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile {
                Cards = new List <Card> {
                    winner == 1 ? winningCard : losingCard
                }
            },
            GatherPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            }
        };
        var secondPlayerState = new PlayerGameState
        {
            PlayPile = new CardPile {
                Cards = new List <Card> {
                    winner == 2 ? winningCard : losingCard
                }
            },
            GatherPile = new CardPile {
                Cards = new List <Card> {
                }
            },
            PlayedCards = new CardPile {
                Cards = new List <Card> {
                }
            }
        };

        var gameStateMock = new Mock <ITrackIndividualGameState>();

        gameStateMock.Setup(mock => mock.FirstPlayerState).Returns(firstPlayerState);
        gameStateMock.Setup(mock => mock.SecondPlayerState).Returns(secondPlayerState);

        var objectUnderTest = new PlayWar(loggerDouble);

        objectUnderTest.War(gameStateMock.Object);

        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToPlayCard), Times.Once());
        gameStateMock.Verify(mock => mock.Tick(Constants.GamePlayParameters.TimeToGatherCards), Times.Once());

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player {winner} wins"));
        if (winner == 1)
        {
            Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"War: {winningCard} vs {losingCard}..."));
        }
        else
        {
            Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"War: {losingCard} vs {winningCard}..."));
        }
        Assert.True(loggerDouble.DebugEntries.Count() == 2);
        Assert.True(firstPlayerState.GatherPile.Cards.Count == (winner == 1 ? 2 : 0));
        Assert.True(firstPlayerState.PlayedCards.Cards.Count == 0);
        Assert.True(firstPlayerState.PlayPile.Cards.Count == 0);
        Assert.True(secondPlayerState.GatherPile.Cards.Count == (winner == 2 ? 2 : 0));
        Assert.True(secondPlayerState.PlayedCards.Cards.Count == 0);
        Assert.True(secondPlayerState.PlayPile.Cards.Count == 0);
    }
    public void PlayTestWhenSimulationEndsAfterTwoTieGamesInARow()
    {
        var loggerDouble                   = new LoggerDouble <CoordinateGameplay>();
        var gamePlayMock                   = new Mock <IPlayWar>();
        var pointsCalculatorMock           = new Mock <IPointsCalculator>();
        var individualGameStateTrackerMock = new Mock <ITrackIndividualGameState>();
        var timerMock           = new Mock <IDateTime>();
        var statsCompilerMock   = new Mock <ICompileStats>();
        var singleGameStatsMock = new Mock <ISingleGameStats>();

        var dummyStart         = new DateTime(year: 2011, month: 8, day: 23, hour: 16, minute: 0, second: 0);
        var dummySimulationEnd = new DateTime(year: 2011, month: 8, day: 23, hour: 16, minute: 0, second: 3); // 3 seconds later

        timerMock.SetupSequence(mock => mock.Now)
        .Returns(dummyStart)
        .Returns(dummySimulationEnd);

        // use a real game state to easily initialize a full deck
        var realGameState = new TrackIndividualGameState().InitializeGameState();
        var fakeGameState = new Mock <ITrackIndividualGameState>();

        var player1DummyState = realGameState.FirstPlayerState;
        var player2DummyState = realGameState.SecondPlayerState;

        fakeGameState.Setup(mock => mock.FirstPlayerState).Returns(player1DummyState);
        fakeGameState.Setup(mock => mock.SecondPlayerState).Returns(player2DummyState);
        fakeGameState.SetupSequence(mock => mock.TimeElapsed)
        .Returns(TimeSpan.FromMinutes(4))     // first game 4 minutes
        .Returns(TimeSpan.FromMinutes(4))     // but TimeElapsed is invoked ...
        .Returns(TimeSpan.FromMinutes(4))     // three times
        .Returns(TimeSpan.FromMinutes(7))     // second game 7 minutes
        .Returns(TimeSpan.FromMinutes(7))     // but TimeElapsed gets invoked ...
        .Returns(TimeSpan.FromMinutes(7));    // trhee times
        fakeGameState.Setup(mock => mock.Stats).Returns(singleGameStatsMock.Object);

        gamePlayMock.Setup(mock => mock.PlayerHasLost(It.IsAny <PlayerGameState>())).Returns(false);

        fakeGameState.SetupSequence(mock => mock.TimedOut)
        .Returns(false)
        .Returns(false)
        .Returns(true)
        .Returns(false)
        .Returns(true);

        individualGameStateTrackerMock.Setup(mock => mock.InitializeGameState()).Returns(fakeGameState.Object);

        pointsCalculatorMock.Setup(mock => mock.GetPoints(It.IsAny <List <Card> >())).Returns(48); // always return a tie

        var objectUnderTest = new CoordinateGameplay(loggerDouble, gamePlayMock.Object, pointsCalculatorMock.Object,
                                                     individualGameStateTrackerMock.Object, timerMock.Object, statsCompilerMock.Object);

        var dummySimulationDuration = TimeSpan.FromMinutes(10);
        var dummyGameParams         = new GameParameters
        {
            FirstPlayerName         = "Isaac",
            SecondPlayerName        = "Daddy",
            TotalLengthOfSimulation = dummySimulationDuration
        };

        var result = objectUnderTest.Play(dummyGameParams);

        Assert.True(result.TotalSimulationTime == (dummySimulationEnd - dummyStart));

        Assert.True(loggerDouble.DebugEntries.Count() == 8);
        Assert.True(loggerDouble.InformationEntries.Count() == 1);

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"{ dummyGameParams.FirstPlayerName} has 0 wins, " +
                                               $"{dummyGameParams.SecondPlayerName} has 0 wins, 1 ties"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"{ dummyGameParams.FirstPlayerName} has 0 wins, " +
                                               $"{dummyGameParams.SecondPlayerName} has 0 wins, 2 ties"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "This game is a tie."));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "Game has timed out after 240 seconds."));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, "Game has timed out after 420 seconds."));

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Information, "Simulation took 3 seconds..."));

        gamePlayMock.Verify(mock => mock.War(It.IsAny <ITrackIndividualGameState>(), It.IsAny <int>()), Times.Exactly(5));

        gamePlayMock.Verify(mock => mock.Transition(It.IsAny <ITrackIndividualGameState>()), Times.Exactly(5));

        pointsCalculatorMock.Verify(mock => mock.GetPoints(It.IsAny <List <Card> >()), Times.Exactly(4));

        singleGameStatsMock.VerifySet(mock => mock.Winner         = 0, Times.Exactly(2));
        singleGameStatsMock.VerifySet(mock => mock.SimulationTime = It.IsAny <TimeSpan>(), Times.Exactly(2));
        statsCompilerMock.Verify(mock => mock.UpdateStats(It.IsAny <ISimulationStats>(), It.IsAny <ISingleGameStats>()), Times.Exactly(2));
    }
    public void PlayTestWhenOnePlayerLosesImmediatelyAndThenWeTimeOut(int winner)
    {
        var loggerDouble                   = new LoggerDouble <CoordinateGameplay>();
        var gamePlayMock                   = new Mock <IPlayWar>();
        var pointsCalculatorMock           = new Mock <IPointsCalculator>();
        var individualGameStateTrackerMock = new Mock <ITrackIndividualGameState>();
        var timerMock           = new Mock <IDateTime>();
        var statsCompilerMock   = new Mock <ICompileStats>();
        var singleGameStatsMock = new Mock <ISingleGameStats>();

        var dummyStart         = new DateTime(year: 2011, month: 8, day: 23, hour: 16, minute: 0, second: 0);
        var dummySimulationEnd = new DateTime(year: 2011, month: 8, day: 23, hour: 16, minute: 0, second: 3); // 3 seconds later

        timerMock.SetupSequence(mock => mock.Now)
        .Returns(dummyStart)
        .Returns(dummySimulationEnd);

        var fakeGameState = new Mock <ITrackIndividualGameState>();

        var player1DummyState = new PlayerGameState {
        };
        var player2DummyState = new PlayerGameState {
        };

        fakeGameState.Setup(mock => mock.FirstPlayerState).Returns(player1DummyState);
        fakeGameState.Setup(mock => mock.SecondPlayerState).Returns(player2DummyState);
        var dummyDuration = TimeSpan.FromMinutes(5);

        fakeGameState.Setup(mock => mock.TimeElapsed).Returns(dummyDuration);
        fakeGameState.Setup(mock => mock.Stats).Returns(singleGameStatsMock.Object);

        gamePlayMock.Setup(mock => mock.PlayerHasLost(player1DummyState)).Returns(winner == 2);
        gamePlayMock.Setup(mock => mock.PlayerHasLost(player2DummyState)).Returns(winner == 1);

        individualGameStateTrackerMock.Setup(mock => mock.InitializeGameState()).Returns(fakeGameState.Object);

        var objectUnderTest = new CoordinateGameplay(loggerDouble, gamePlayMock.Object, pointsCalculatorMock.Object,
                                                     individualGameStateTrackerMock.Object, timerMock.Object, statsCompilerMock.Object);

        var dummySimulationDuration = TimeSpan.FromMinutes(4);
        var dummyGameParams         = new GameParameters
        {
            FirstPlayerName         = "Isaac",
            SecondPlayerName        = "Daddy",
            TotalLengthOfSimulation = dummySimulationDuration
        };

        var result = objectUnderTest.Play(dummyGameParams);

        Assert.True(result.TotalSimulationTime == (dummySimulationEnd - dummyStart));

        Assert.True(loggerDouble.DebugEntries.Count() == 3);
        Assert.True(loggerDouble.InformationEntries.Count() == 1);

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"{ dummyGameParams.FirstPlayerName} has { (winner == 1 ? 1 : 0)} wins, " +
                                               $"{dummyGameParams.SecondPlayerName} has {(winner == 2 ? 1 : 0)} wins, 0 ties"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player {winner} has won this game!"));

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Information, "Simulation took 3 seconds..."));

        //make sure we only played war once
        gamePlayMock.Verify(mock => mock.War(It.IsAny <ITrackIndividualGameState>(), It.IsAny <int>()), Times.Once());

        //make sure we never had to transition
        gamePlayMock.Verify(mock => mock.Transition(It.IsAny <ITrackIndividualGameState>()), Times.Never());

        //make sure there was no need to calculate points
        pointsCalculatorMock.Verify(mock => mock.GetPoints(It.IsAny <List <Card> >()), Times.Never());

        singleGameStatsMock.VerifySet(mock => mock.Winner         = winner, Times.Once());
        singleGameStatsMock.VerifySet(mock => mock.SimulationTime = It.IsAny <TimeSpan>(), Times.Once());
        statsCompilerMock.Verify(mock => mock.UpdateStats(It.IsAny <ISimulationStats>(), It.IsAny <ISingleGameStats>()), Times.Once());
    }
    public void PlayTestWhenWithTimeoutButAWinner(int winner)
    {
        var loggerDouble                   = new LoggerDouble <CoordinateGameplay>();
        var gamePlayMock                   = new Mock <IPlayWar>();
        var pointsCalculatorMock           = new Mock <IPointsCalculator>();
        var individualGameStateTrackerMock = new Mock <ITrackIndividualGameState>();
        var timerMock           = new Mock <IDateTime>();
        var statsCompilerMock   = new Mock <ICompileStats>();
        var singleGameStatsMock = new Mock <ISingleGameStats>();

        // use a real game state to easily initialize a full deck
        var realGameState = new TrackIndividualGameState().InitializeGameState();
        var fakeGameState = new Mock <ITrackIndividualGameState>();

        var player1DummyState = realGameState.FirstPlayerState;
        var player2DummyState = realGameState.SecondPlayerState;

        fakeGameState.Setup(mock => mock.FirstPlayerState).Returns(player1DummyState);
        fakeGameState.Setup(mock => mock.SecondPlayerState).Returns(player2DummyState);
        var dummyDuration = TimeSpan.FromMinutes(10);

        fakeGameState.Setup(mock => mock.TimeElapsed).Returns(dummyDuration);

        gamePlayMock.Setup(mock => mock.PlayerHasLost(It.IsAny <PlayerGameState>())).Returns(false);

        fakeGameState.Setup(mock => mock.TimedOut).Returns(true);
        fakeGameState.Setup(mock => mock.Stats).Returns(singleGameStatsMock.Object);

        individualGameStateTrackerMock.Setup(mock => mock.InitializeGameState()).Returns(fakeGameState.Object);

        pointsCalculatorMock.SetupSequence(mock => mock.GetPoints(It.IsAny <List <Card> >()))
        .Returns(winner == 1 ? 49 : 47)
        .Returns(winner == 2 ? 49 : 47);

        var objectUnderTest = new CoordinateGameplay(loggerDouble, gamePlayMock.Object, pointsCalculatorMock.Object,
                                                     individualGameStateTrackerMock.Object, timerMock.Object, statsCompilerMock.Object);

        var dummySimulationDuration = TimeSpan.FromMinutes(10);
        var dummyGameParams         = new GameParameters
        {
            FirstPlayerName         = "Isaac",
            SecondPlayerName        = "Daddy",
            TotalLengthOfSimulation = dummySimulationDuration
        };

        objectUnderTest.Play(dummyGameParams);

        Assert.True(loggerDouble.DebugEntries.Count() == 4);
        Assert.True(loggerDouble.InformationEntries.Count() == 1);

        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"{ dummyGameParams.FirstPlayerName} has {(winner == 1 ? 1 : 0)} wins, " +
                                               $"{dummyGameParams.SecondPlayerName} has {(winner == 2 ? 1 : 0)} wins, 0 ties"));
        Assert.True(loggerDouble.HasBeenLogged(LogLevel.Debug, $"Player {winner} has won this game!"));

        gamePlayMock.Verify(mock => mock.War(It.IsAny <ITrackIndividualGameState>(), It.IsAny <int>()), Times.Once());
        gamePlayMock.Verify(mock => mock.Transition(It.IsAny <ITrackIndividualGameState>()), Times.Once());
        pointsCalculatorMock.Verify(mock => mock.GetPoints(It.IsAny <List <Card> >()), Times.Exactly(2));
        singleGameStatsMock.VerifySet(mock => mock.Winner         = winner, Times.Once());
        singleGameStatsMock.VerifySet(mock => mock.SimulationTime = It.IsAny <TimeSpan>(), Times.Once());
        statsCompilerMock.Verify(mock => mock.UpdateStats(It.IsAny <ISimulationStats>(), It.IsAny <ISingleGameStats>()), Times.Once());
    }