public void PlayShouldCallGetTurnAndEndTurnForBothPlayers()
        {
            var firstPlayer = new ValidPlayer();
            var firstPlayerInfo = new RoundPlayerInfo(firstPlayer);
            var secondPlayer = new ValidPlayer();
            var secondPlayerInfo = new RoundPlayerInfo(secondPlayer);
            var stateManager = new StateManager();
            var deck = new Deck();

            SimulateGame(firstPlayerInfo, secondPlayerInfo, deck);

            var trick = new Trick(firstPlayerInfo, secondPlayerInfo, stateManager, deck, GameRulesProvider.Santase);
            var winner = trick.Play();

            Assert.AreEqual(1, firstPlayer.GetTurnCalledCount);
            Assert.AreEqual(1, secondPlayer.GetTurnCalledCount);
            Assert.AreEqual(1, firstPlayer.EndTurnCalledCount);
            Assert.AreEqual(1, secondPlayer.EndTurnCalledCount);

            Assert.IsNotNull(firstPlayer.GetTurnContextObject);
            Assert.IsNotNull(secondPlayer.GetTurnContextObject);
            Assert.IsNotNull(firstPlayer.EndTurnContextObject);
            Assert.IsNotNull(secondPlayer.EndTurnContextObject);

            Assert.IsNotNull(firstPlayer.EndTurnContextObject.FirstPlayedCard);
            Assert.IsNotNull(firstPlayer.EndTurnContextObject.SecondPlayedCard);
            Assert.IsNotNull(secondPlayer.EndTurnContextObject.FirstPlayedCard);
            Assert.IsNotNull(secondPlayer.EndTurnContextObject.SecondPlayedCard);

            Assert.IsTrue(winner == firstPlayerInfo || winner == secondPlayerInfo);
        }
 public void SetStateShouldChangeTheState()
 {
     var stateManager = new StateManager();
     var state = new FinalRoundState(stateManager);
     stateManager.SetState(state);
     Assert.AreEqual(state, stateManager.State);
 }
        public void PlayShouldCallGetTurnOnlyForFirstPlayerWhenTheFirstPlayerGoesOutByAnnounce()
        {
            var firstPlayer = new ValidPlayer();
            var firstPlayerInfo = new RoundPlayerInfo(firstPlayer);
            var secondPlayer = new ValidPlayer();
            var secondPlayerInfo = new RoundPlayerInfo(secondPlayer);
            var stateManager = new StateManager();
            var deck = new Deck();

            // 53 points in firstPlayerInfo.TrickCards
            firstPlayerInfo.TrickCards.Add(new Card(CardSuit.Diamond, CardType.Ace));
            firstPlayerInfo.TrickCards.Add(new Card(CardSuit.Diamond, CardType.Ten));
            firstPlayerInfo.TrickCards.Add(new Card(CardSuit.Spade, CardType.Ace));
            firstPlayerInfo.TrickCards.Add(new Card(CardSuit.Club, CardType.Ace));
            firstPlayerInfo.TrickCards.Add(new Card(CardSuit.Club, CardType.Ten));

            // Add cards for announcing 20
            firstPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.King));
            firstPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.Queen));
            stateManager.SetState(new MoreThanTwoCardsLeftRoundState(stateManager));

            secondPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.Ten));
            secondPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.Ace));

            var trick = new Trick(firstPlayerInfo, secondPlayerInfo, stateManager, deck, GameRulesProvider.Santase);
            var winner = trick.Play();

            Assert.AreEqual(1, firstPlayer.GetTurnCalledCount);
            Assert.AreEqual(0, secondPlayer.GetTurnCalledCount);
            Assert.AreEqual(1, firstPlayer.EndTurnCalledCount);
            Assert.AreEqual(1, secondPlayer.EndTurnCalledCount);
            Assert.AreSame(firstPlayerInfo, winner);

            Assert.IsTrue(firstPlayerInfo.HasAtLeastOneTrick);
            Assert.IsFalse(secondPlayerInfo.HasAtLeastOneTrick);

            Assert.IsTrue(winner.RoundPoints == 73 || winner.RoundPoints == 93);
            Assert.IsTrue(winner.RoundPoints == 73 || winner.RoundPoints == 93);
        }
        public void PlayShouldChangeTheDeckTrumpWhenPlayerPlaysChangeTrumpAction()
        {
            var firstPlayer = new ValidPlayer(PlayerActionType.ChangeTrump);
            var firstPlayerInfo = new RoundPlayerInfo(firstPlayer);
            var secondPlayer = new ValidPlayer();
            var secondPlayerInfo = new RoundPlayerInfo(secondPlayer);
            var stateManager = new StateManager();
            stateManager.SetState(new MoreThanTwoCardsLeftRoundState(stateManager));
            var deck = new Deck();
            var trumpSuit = deck.TrumpCard.Suit;

            var oldTrumpCard = deck.TrumpCard.DeepClone();
            var nineOfTrump = new Card(trumpSuit, CardType.Nine);

            firstPlayerInfo.AddCard(nineOfTrump);
            secondPlayerInfo.AddCard(
                new Card(trumpSuit == CardSuit.Heart ? CardSuit.Club : CardSuit.Heart, CardType.Ace));

            var trick = new Trick(firstPlayerInfo, secondPlayerInfo, stateManager, deck, GameRulesProvider.Santase);
            trick.Play();

            Assert.AreEqual(nineOfTrump, deck.TrumpCard);
            Assert.AreEqual(nineOfTrump, secondPlayer.GetTurnContextObject.TrumpCard);
            Assert.IsTrue(firstPlayerInfo.TrickCards.Contains(oldTrumpCard), "Trick cards should contain oldTrumpCard");
            Assert.IsFalse(firstPlayerInfo.Cards.Contains(nineOfTrump));
            Assert.IsFalse(
                firstPlayer.CardsCollection.Contains(nineOfTrump),
                "Player contains nine of trump after changing trump card");
        }
        public void PlayShouldThrowAnExceptionWhenPlayerReturnsNullAction()
        {
            var firstPlayer = new Mock<IPlayer>();
            firstPlayer.Setup(x => x.GetTurn(It.IsAny<PlayerTurnContext>())).Returns((PlayerAction)null);
            var firstPlayerInfo = new RoundPlayerInfo(firstPlayer.Object);

            var secondPlayer = new Mock<IPlayer>();
            var secondPlayerInfo = new RoundPlayerInfo(secondPlayer.Object);

            var stateManager = new StateManager();
            var deck = new Deck();

            var trick = new Trick(firstPlayerInfo, secondPlayerInfo, stateManager, deck, GameRulesProvider.Santase);
            trick.Play();
        }
        public void PlayShouldThrowAnExceptionWhenPlayerPlaysInvalidCard()
        {
            var firstPlayer = new Mock<IPlayer>();
            firstPlayer.Setup(x => x.GetTurn(It.IsAny<PlayerTurnContext>()))
                .Returns(PlayerAction.PlayCard(new Card(CardSuit.Club, CardType.Ace)));
            var firstPlayerInfo = new RoundPlayerInfo(firstPlayer.Object);

            var secondPlayer = new Mock<IPlayer>();
            var secondPlayerInfo = new RoundPlayerInfo(secondPlayer.Object);

            var stateManager = new StateManager();
            var deck = new Deck();

            firstPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.King));
            secondPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.Ace));

            var trick = new Trick(firstPlayerInfo, secondPlayerInfo, stateManager, deck, GameRulesProvider.Santase);
            trick.Play();
        }
        public void PlayShouldThrowAnExceptionWhenClosingTheGameAndNineOfTrumpsIsMissing()
        {
            var firstPlayer = new ValidPlayer(PlayerActionType.ChangeTrump);
            var firstPlayerInfo = new RoundPlayerInfo(firstPlayer);
            var secondPlayer = new ValidPlayer();
            var secondPlayerInfo = new RoundPlayerInfo(secondPlayer);
            var stateManager = new StateManager();
            stateManager.SetState(new MoreThanTwoCardsLeftRoundState(stateManager));
            var deck = new Deck();
            var trumpSuit = deck.TrumpCard.Suit;

            firstPlayerInfo.AddCard(new Card(trumpSuit, CardType.Jack));
            secondPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.Ace));

            var trick = new Trick(firstPlayerInfo, secondPlayerInfo, stateManager, deck, GameRulesProvider.Santase);
            trick.Play();
        }
        public void PlayShouldProvideCorrectPlayerTurnContextToPlayers()
        {
            var firstPlayer = new ValidPlayer();
            var firstPlayerInfo = new RoundPlayerInfo(firstPlayer);
            var secondPlayer = new ValidPlayer();
            var secondPlayerInfo = new RoundPlayerInfo(secondPlayer);
            var stateManager = new StateManager();
            var deck = new Deck();

            firstPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.King));
            firstPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.Queen));
            stateManager.SetState(new MoreThanTwoCardsLeftRoundState(stateManager));

            secondPlayerInfo.AddCard(new Card(CardSuit.Diamond, CardType.Ten));
            secondPlayerInfo.AddCard(new Card(CardSuit.Diamond, CardType.Ace));

            var trick = new Trick(firstPlayerInfo, secondPlayerInfo, stateManager, deck, GameRulesProvider.Santase);
            trick.Play();

            Assert.IsTrue(firstPlayer.GetTurnContextObject.IsFirstPlayerTurn);
            Assert.IsFalse(secondPlayer.GetTurnContextObject.IsFirstPlayerTurn);
            Assert.IsTrue(secondPlayer.GetTurnContextObject.FirstPlayerAnnounce != Announce.None);
            Assert.IsNotNull(secondPlayer.GetTurnContextObject.FirstPlayedCard);
            Assert.AreEqual(CardSuit.Heart, secondPlayer.GetTurnContextObject.FirstPlayedCard.Suit);

            Assert.IsTrue(
                secondPlayer.GetTurnContextObject.FirstPlayerRoundPoints == 20
                || secondPlayer.GetTurnContextObject.FirstPlayerRoundPoints == 40);
        }
        public void PlayShouldCorrectlyDetermineTheWinner()
        {
            var firstPlayer = new ValidPlayer();
            var firstPlayerInfo = new RoundPlayerInfo(firstPlayer);
            var secondPlayer = new ValidPlayer();
            var secondPlayerInfo = new RoundPlayerInfo(secondPlayer);
            var stateManager = new StateManager();
            var deck = new Deck();

            firstPlayerInfo.AddCard(new Card(CardSuit.Heart, CardType.Nine));
            secondPlayerInfo.AddCard(new Card(deck.TrumpCard.Suit, CardType.Jack));

            var trick = new Trick(firstPlayerInfo, secondPlayerInfo, stateManager, deck, GameRulesProvider.Santase);
            var winner = trick.Play();

            Assert.IsTrue(winner == secondPlayerInfo);
            Assert.AreEqual(2, winner.RoundPoints);
            Assert.AreEqual(2, winner.TrickCards.Count);
            Assert.IsTrue(winner.TrickCards.Contains(new Card(CardSuit.Heart, CardType.Nine)));
            Assert.IsTrue(winner.TrickCards.Contains(new Card(deck.TrumpCard.Suit, CardType.Jack)));
            Assert.AreEqual(0, firstPlayerInfo.TrickCards.Count);

            Assert.AreEqual(0, firstPlayer.EndTurnContextObject.FirstPlayerRoundPoints);
            Assert.AreEqual(2, firstPlayer.EndTurnContextObject.SecondPlayerRoundPoints);
            Assert.AreEqual(0, secondPlayer.EndTurnContextObject.FirstPlayerRoundPoints);
            Assert.AreEqual(2, secondPlayer.EndTurnContextObject.SecondPlayerRoundPoints);

            Assert.AreEqual(0, firstPlayer.GetTurnContextObject.FirstPlayerRoundPoints);
            Assert.AreEqual(0, firstPlayer.GetTurnContextObject.SecondPlayerRoundPoints);
            Assert.AreEqual(0, secondPlayer.GetTurnContextObject.FirstPlayerRoundPoints);
            Assert.AreEqual(0, secondPlayer.GetTurnContextObject.SecondPlayerRoundPoints);
        }
        public void PlayShouldCloseTheGameWhenPlayerPlaysCloseGameAction()
        {
            var firstPlayer = new ValidPlayer(PlayerActionType.CloseGame);
            var firstPlayerInfo = new RoundPlayerInfo(firstPlayer);
            var secondPlayer = new ValidPlayer();
            var secondPlayerInfo = new RoundPlayerInfo(secondPlayer);
            var stateManager = new StateManager();
            stateManager.SetState(new MoreThanTwoCardsLeftRoundState(stateManager));
            var deck = new Deck();

            SimulateGame(firstPlayerInfo, secondPlayerInfo, deck);

            var trick = new Trick(firstPlayerInfo, secondPlayerInfo, stateManager, deck, GameRulesProvider.Santase);
            trick.Play();

            Assert.IsTrue(firstPlayerInfo.GameCloser);
            Assert.IsFalse(secondPlayerInfo.GameCloser);
            Assert.IsInstanceOf<FinalRoundState>(stateManager.State);
            Assert.IsInstanceOf<FinalRoundState>(secondPlayer.GetTurnContextObject.State);
        }
 public void DefaultStateShouldBeStartRoundState()
 {
     var stateManager = new StateManager();
     Assert.AreEqual(typeof(StartRoundState), stateManager.State.GetType());
 }