public void PlayingCardThatsNotInHandShouldBeInvalid()
        {
            var cards = new List<Card>() { new Card(CardSuit.Diamond, CardType.Jack) };
            var action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Club, CardType.Jack), Announce.None);
            var state = new FinalRoundState();
            var context = new PlayerTurnContext(state, new Card(CardSuit.Club, CardType.Jack), 0);

            Assert.IsFalse(this.validater.IsValid(action, context, cards));
        }
 public void PlayingCardFromHandShouldBeValid()
 {
     var cards = new List<Card>() { new Card(CardSuit.Club, CardType.Ace) };
     var action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Club, CardType.Ace), Announce.None);
     var state = new FinalRoundState();
     var context = new PlayerTurnContext(state, new Card(CardSuit.Club, CardType.Jack), 0);
     
     Assert.IsTrue(validator.IsValid(action, context, cards));
 }
        public PlayerAction Handle(DecisionMakingContext context)
        {
            this.announce = this.GetAnnounces(context);
            if (this.IsMatch(context))
            {
                return this.announce;
            }

            return this.Next.Handle(context);
        }
        public void PlayerChangesTrumpWithQueen_ShouldBeInvalid()
        {
            var cards = new List<Card>() { new Card(CardSuit.Club, CardType.Queen) };
            var action = new PlayerAction(PlayerActionType.ChangeTrump, new Card(CardSuit.Club, CardType.Queen), Announce.None);
            var round = new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer);
            var state = new MoreThanTwoCardsLeftRoundState(round);
            var context = new PlayerTurnContext(state, new Card(CardSuit.Club, CardType.Jack), 0);

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
Exemple #5
0
        public void Action(PlayerAction playerAction)
        {
            if (!this.PlayerActionValidator.IsValid(playerAction, this.currentContext, this.Cards))
            {
                // Invalid player action
                return;
            }

            this.userAction = playerAction;
        }
        public void FourtyAnnounce_ShouldBeValid()
        {
            var cards = new List<Card>() { new Card(CardSuit.Club, CardType.Queen), new Card(CardSuit.Club, CardType.King) };
            var announce = Announce.Fourty;
            var action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Club, CardType.Queen), announce);
            var round = new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer);
            var state = new MoreThanTwoCardsLeftRoundState(round);
            var context = new PlayerTurnContext(state, new Card(CardSuit.Club, CardType.Jack), 0);
            validator.IsValid(action, context, cards);

            Assert.AreEqual(Announce.Fourty, announce);
        }
 public void Testing()
 {
     var action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Club, CardType.King), Announce.None);
     var first = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Club, CardType.Nine), Announce.None);
     var second = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Club, CardType.Ten), Announce.None);
     //// IPlayer first;
     //// IPlayer second;
     //// first.AddCard(new Card(CardSuit.Club, CardType.Nine));
     var firstToPlay = PlayerPosition.FirstPlayer;
     //// var gameRound = new GameRound(first, second, PlayerPosition.SecondPlayer);
     //// IGameRound gameRound;
     //// var context = new PlayerTurnContext(new StartRoundState(gameRound), new Card(CardSuit.Diamond, CardType.Ten), 24);
 }
        public void AnnouncingAtFirstHandShouldBeValid()
        {
            MoreThanTwoCardsLeftRoundState stateOfMiddle = new MoreThanTwoCardsLeftRoundState(new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer));
            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Heart, CardType.King), Announce.Twenty);
            PlayerTurnContext context = new PlayerTurnContext(stateOfMiddle, new Card(CardSuit.Spade, CardType.Jack), 6);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Heart, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.King),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            validator.IsValid(action, context, cards);
            Assert.AreEqual(Announce.None, action.Announce);
        }
        public bool IsValid(PlayerAction action, PlayerTurnContext context, ICollection<Card> playerCards)
        {
            if (context.State.CanAnnounce20Or40)
            {
                action.Announce = this.announceValidator.GetPossibleAnnounce(
                    playerCards,
                    action.Card,
                    context.TrumpCard,
                    context.IsFirstPlayerTurn);
            }

            if (action == null)
            {
                return false;
            }

            if (action.Type == PlayerActionType.PlayCard)
            {
                var canPlayCard = PlayCardActionValidator.CanPlayCard(
                    context.IsFirstPlayerTurn,
                    action.Card,
                    context.FirstPlayedCard,
                    context.TrumpCard,
                    playerCards,
                    context.State.ShouldObserveRules);
                return canPlayCard;
            }

            if (action.Type == PlayerActionType.ChangeTrump)
            {
                var canChangeTrump = ChangeTrumpActionValidator.CanChangeTrump(
                    context.IsFirstPlayerTurn,
                    context.State,
                    context.TrumpCard,
                    playerCards);
                return canChangeTrump;
            }

            // action.Type == PlayerActionType.CloseGame
            var canCloseGame = CloseGameActionValidator.CanCloseGame(context.IsFirstPlayerTurn, context.State);
            return canCloseGame;
        }
        public void TestAnnounceFourty()
        {
            MoreThanTwoCardsLeftRoundState stateOfMiddle = new MoreThanTwoCardsLeftRoundState(new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer));

            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Spade, CardType.King), Announce.Fourty);
            PlayerTurnContext context = new PlayerTurnContext(stateOfMiddle, new Card(CardSuit.Spade, CardType.Jack), 6);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.King),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            // a card is played. Just to avoid context.AmITheFirstPlayer;
            context.FirstPlayedCard = new Card(CardSuit.Club, CardType.King);

            validator.IsValid(action, context, cards);
            Assert.AreEqual(Announce.Fourty, action.Announce);
        }
 protected PlayerAction CloseGame()
 {
     return(PlayerAction.CloseGame());
 }
 protected PlayerAction PlayCard(Card card)
 {
     this.Cards.Remove(card);
     return(PlayerAction.PlayCard(card));
 }
 protected PlayerAction ChangeTrump(Card trumpCard)
 {
     this.Cards.Remove(Card.GetCard(trumpCard.Suit, CardType.Nine));
     this.Cards.Add(trumpCard);
     return(PlayerAction.ChangeTrump());
 }
        public void TestSimpleValidAction()
        {
            // PlayerActionValidater validato2r = new PlayerActionValidater();
            MoreThanTwoCardsLeftRoundState middleState = new MoreThanTwoCardsLeftRoundState(new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer));

            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Heart, CardType.King), Announce.None);
            PlayerTurnContext context = new PlayerTurnContext(middleState, new Card(CardSuit.Spade, CardType.Jack), 6);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Ten),
                new Card(CardSuit.Heart, CardType.King),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Club, CardType.King),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsTrue(validator.IsValid(action, context, cards));
        }
        public void ValidChangeOfTrumpCardShouldBeEvaluatedCorrectly()
        {
            MoreThanTwoCardsLeftRoundState midle = new MoreThanTwoCardsLeftRoundState(new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer));
            PlayerTurnContext context = new PlayerTurnContext(midle, new Card(CardSuit.Diamond, CardType.Ten), 6);
            PlayerAction action = new PlayerAction(PlayerActionType.ChangeTrump, new Card(CardSuit.Club, CardType.Nine), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.Ace),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsTrue(validator.IsValid(action, context, cards));
        }
        public void TestWrongChangeTrumpInTheBegining()
        {
            StartRoundState begin = new StartRoundState(new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer));

            PlayerTurnContext context = new PlayerTurnContext(begin, new Card(CardSuit.Diamond, CardType.Jack), 12);

            PlayerAction action = new PlayerAction(PlayerActionType.ChangeTrump, new Card(CardSuit.Club, CardType.Nine), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.Ace),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
Exemple #17
0
        public override PlayerAction GetTurn(
            PlayerTurnContext context,
            IPlayerActionValidater actionValidater)
        {
            PrintGameInfo(context);
            while (true)
            {
                PlayerAction playerAction = null;

                Console.SetCursorPosition(0, this.row + 1);
                Console.Write(new string(' ', 79));
                Console.SetCursorPosition(0, this.row + 1);
                Console.Write("Turn? [1-{0}]=Card{1}",
                    this.cards.Count,
                    context.AmITheFirstPlayer ? "; [T]=Change trump;        [C]=Close: " : ": ");
                var userActionAsString = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(userActionAsString))
                {
                    Console.WriteLine("Empty turn!                 ");
                    continue;
                }

                if (userActionAsString[0] >= '1'
                    && userActionAsString[0] <= '6')
                {
                    var cardIndex =
                        int.Parse(userActionAsString[0].ToString()) - 1;
                    if (cardIndex >= this.cards.Count)
                    {
                        Console.WriteLine("Invalid card!              ");
                        continue;
                    }

                    var card = this.cards[cardIndex];
                    var possibleAnnounce = Announce.None;

                    if (context.AmITheFirstPlayer)
                    {
                        possibleAnnounce = this.PossibleAnnounce(card, context.TrumpCard);
                        if (possibleAnnounce != Announce.None)
                        {
                            while (true)
                            {
                                Console.SetCursorPosition(0, this.row + 2);
                                Console.Write("Announce {0} [Y]/[N]?         ", possibleAnnounce.ToString());
                                var userInput = Console.ReadLine();
                                if (string.IsNullOrWhiteSpace(userInput))
                                {
                                    Console.WriteLine("Please enter [Y] or [N]         ");
                                    continue;
                                }

                                if (userInput[0] == 'N')
                                {
                                    possibleAnnounce = Announce.None;
                                }
                                else if (userInput[0] == 'Y')
                                {
                                    break;
                                }
                                else
                                {
                                    Console.WriteLine("Please enter [Y] or [N]         ");
                                    continue;
                                }
                            }
                        }
                    }

                    playerAction =
                        new PlayerAction(
                            PlayerActionType.PlayCard,
                            card,
                            possibleAnnounce);
                }
                else if (userActionAsString[0] == 'T')
                {
                    playerAction =
                        new PlayerAction(
                            PlayerActionType.ChangeTrump,
                            null,
                            Announce.None);
                }
                else if (userActionAsString[0] == 'C')
                {
                    playerAction =
                        new PlayerAction(
                            PlayerActionType.CloseGame,
                            null,
                            Announce.None);
                }
                else
                {
                    Console.WriteLine("Invalid turn!                ");
                    continue;
                }

                if (actionValidater.IsValid(playerAction, context, this.cards))
                {
                    if (playerAction.Type == PlayerActionType.PlayCard)
                    {
                        this.cards.Remove(playerAction.Card);
                    }

                    if (playerAction.Type == PlayerActionType.ChangeTrump)
                    {
                        this.cards.Remove(new Card(context.TrumpCard.Suit, CardType.Nine));
                    }

                    this.PrintGameInfo(context);

                    return playerAction;
                }
                else
                {
                    Console.WriteLine("Invalid action!                  ");
                    continue;
                }
            }
        }
        public void TestValidMoveWithTrumpCard()
        {
            // Using this state to enter in context.State.ShouldObserveRules.
            FinalRoundState endRound = new FinalRoundState();

            PlayerTurnContext context = new PlayerTurnContext(endRound, new Card(CardSuit.Spade, CardType.Jack), 0);

            // a card is played. Just to avoid context.AmITheFirstPlayer;
            context.FirstPlayedCard = new Card(CardSuit.Club, CardType.Nine);

            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Spade, CardType.Nine), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Diamond, CardType.King),
                new Card(CardSuit.Spade, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsTrue(validator.IsValid(action, context, cards));
        }
        public void InvalidPlayerCardWithoutTrumpShouldBeEvaluаtedCorrectly()
        {
            FinalRoundState finalState = new FinalRoundState();
            PlayerTurnContext context = new PlayerTurnContext(finalState, new Card(CardSuit.Diamond, CardType.Jack), 0);
            context.FirstPlayedCard = new Card(CardSuit.Club, CardType.Ten);
            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Spade, CardType.Queen), Announce.Twenty);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.King),
                new Card(CardSuit.Spade, CardType.Nine),
                new Card(CardSuit.Spade, CardType.Nine)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
        public void TestInvalidMoveCardWithLowerValueWhenHaveBigger()
        {
            FinalRoundState finalState = new FinalRoundState();

            PlayerTurnContext context = new PlayerTurnContext(finalState, new Card(CardSuit.Diamond, CardType.Jack), 0);

            context.FirstPlayedCard = new Card(CardSuit.Club, CardType.King);

            // You must play card with bigger value.
            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Club, CardType.Nine), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.Ace),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
Exemple #21
0
        public override PlayerAction GetTurn(PlayerTurnContext context)
        {
            this.iAmFirstThisTurn = context.IsFirstPlayerTurn;
            this.UpdateContextInfo(context);
            this.currentContext = context;
            while (this.userAction == null)
            {
                Task.Delay(50);
            }

            lock (this.userAction)
            {
                PlayerAction action;
                switch (this.userAction.Type)
                {
                    case PlayerActionType.PlayCard:
                        action = this.PlayCard(this.userAction.Card);
                        this.RedrawCards?.Invoke(this, this.Cards);
                        this.RedrawPlayerPlayedCard?.Invoke(this, this.userAction.Card);
                        break;
                    case PlayerActionType.ChangeTrump:
                        action = this.ChangeTrump(context.TrumpCard);
                        this.RedrawCards?.Invoke(this, this.Cards);
                        break;
                    case PlayerActionType.CloseGame:
                        action = this.CloseGame();
                        this.GameClosed?.Invoke(this, EventArgs.Empty);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException(nameof(this.userAction.Type));
                }

                this.userAction = null;
                return action;
            }
        }
        public void TestWithIncorrectTrumpCard()
        {
            FinalRoundState finalState = new FinalRoundState();

            PlayerTurnContext context = new PlayerTurnContext(finalState, new Card(CardSuit.Diamond, CardType.Jack), 0);

            context.FirstPlayedCard = new Card(CardSuit.Club, CardType.Ten);

            // You have a needed suit card but played trump. False -> Must play Club
            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Diamond, CardType.Nine), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.King),
                new Card(CardSuit.Spade, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
        public void FourtyAnnounceWhenPlayerIsNotFirstShouldChangeAnnounceToNone()
        {
            var cards = new List<Card>() { new Card(CardSuit.Club, CardType.Queen), new Card(CardSuit.Club, CardType.King), new Card(CardSuit.Heart, CardType.Jack) };
            var announce = Announce.Fourty;
            var action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Heart, CardType.Jack), announce);
            var round = new GameRound(new Player(), new Player(), PlayerPosition.SecondPlayer);
            var state = new MoreThanTwoCardsLeftRoundState(round);
            var context = new PlayerTurnContext(state, new Card(CardSuit.Club, CardType.Jack), 0);
            this.validater.IsValid(action, context, cards);

            Assert.AreEqual(Announce.None, action.Announce);
        }
        public void TestWrongChangeTrumpIfYouHaveNotNeededCard()
        {
            MoreThanTwoCardsLeftRoundState midle = new MoreThanTwoCardsLeftRoundState(new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer));

            PlayerTurnContext context = new PlayerTurnContext(midle, new Card(CardSuit.Diamond, CardType.Jack), 6);

            PlayerAction action = new PlayerAction(PlayerActionType.ChangeTrump, new Card(CardSuit.Club, CardType.Nine), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.Ace),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Ace)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
        public void TestOtherCardWhenNoHaveTrump()
        {
            FinalRoundState finalState = new FinalRoundState();

            PlayerTurnContext context = new PlayerTurnContext(finalState, new Card(CardSuit.Diamond, CardType.Jack), 0);

            context.FirstPlayedCard = new Card(CardSuit.Club, CardType.Ten);

            // Have a club, but played Spade. False. Must play Club
            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Spade, CardType.Queen), Announce.Twenty);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.King),
                new Card(CardSuit.Spade, CardType.Nine),
                new Card(CardSuit.Spade, CardType.Nine)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
        public void TestInvalidPlayedCard()
        {
            FinalRoundState endState = new FinalRoundState();

            PlayerTurnContext context = new PlayerTurnContext(endState, new Card(CardSuit.Spade, CardType.Jack), 0);

            context.FirstPlayedCard = new Card(CardSuit.Club, CardType.Ten);

            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Heart, CardType.Queen), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.King),
                new Card(CardSuit.Spade, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
        public void PlayingWithCardThatIsNotInHandShouldBeInvalid()
        {
            MoreThanTwoCardsLeftRoundState middleState = new MoreThanTwoCardsLeftRoundState(new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer));
            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Diamond, CardType.Ace), Announce.None);
            PlayerTurnContext context = new PlayerTurnContext(middleState, new Card(CardSuit.Spade, CardType.Jack), 6);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Ten),
                new Card(CardSuit.Heart, CardType.King),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Club, CardType.King),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
        public void InvalidClosingGameShouldBeEvaluatedCorrectly()
        {
            StartRoundState begin = new StartRoundState(new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer));
            PlayerTurnContext context = new PlayerTurnContext(begin, new Card(CardSuit.Diamond, CardType.Jack), 12);
            PlayerAction action = new PlayerAction(PlayerActionType.CloseGame, new Card(CardSuit.Club, CardType.Nine), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.Ace),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }
        public void ValidActionWithTrumpCardShouldBeEvaluatedCorrectly()
        {
            FinalRoundState endRound = new FinalRoundState();
            PlayerTurnContext context = new PlayerTurnContext(endRound, new Card(CardSuit.Spade, CardType.Jack), 0);
            context.FirstPlayedCard = new Card(CardSuit.Club, CardType.Nine);
            PlayerAction action = new PlayerAction(PlayerActionType.PlayCard, new Card(CardSuit.Spade, CardType.Nine), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Diamond, CardType.King),
                new Card(CardSuit.Spade, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsTrue(validator.IsValid(action, context, cards));
        }
        public bool IsValid(PlayerAction action, PlayerTurnContext context, IList<Card> playerCards)
        {
            if (context.AmITheFirstPlayer)
            {
                action.Announce = Announce.None;
            }

            if (action.Type == PlayerActionType.PlayCard)
            {
                if (!playerCards.Contains(action.Card))
                {
                    return false;
                }

                if (action.Announce != Announce.None)
                {
                    if (action.Card.Type != CardType.Queen
                        && action.Card.Type != CardType.King)
                    {
                        action.Announce = Announce.None;
                    }

                    // TODO: Check for another card
                }

                if (context.State.ShouldObserveRules)
                {
                    if (!context.AmITheFirstPlayer)
                    {
                        var firstCard = context.FirstPlayedCard;
                        var ourCard = action.Card;

                        if (firstCard.Suit != ourCard.Suit)
                        {
                            if (ourCard.Suit != context.TrumpCard.Suit)
                            {
                                var hasTrump = playerCards.Any(c => c.Suit == context.TrumpCard.Suit);
                                if (hasTrump)
                                {
                                    return false;
                                }
                            }
                        }
                        else
                        {
                            if (ourCard.GetValue() < firstCard.GetValue())
                            {
                                var hasBigger = playerCards.Any(c => c.GetValue() > firstCard.GetValue());
                                if (hasBigger)
                                {
                                    return false;
                                }
                            }
                        }
                    }
                }
            }

            if (action.Type == PlayerActionType.CloseGame && !(context.State is FinalRoundState))
            {
                if (!context.State.CanClose || !context.AmITheFirstPlayer)
                {
                    return false;
                }
            }

            if (action.Type == PlayerActionType.ChangeTrump)
            {
                if (!context.State.CanChangeTrump || !context.AmITheFirstPlayer)
                {
                    return false;
                }

                if (!playerCards.Contains(new Card(context.TrumpCard.Suit, CardType.Nine)))
                {
                    return false;
                }
            }

            return true;
        }
        public void TestInvalidClosingGameInMiddleRound()
        {
            MoreThanTwoCardsLeftRoundState middle = new MoreThanTwoCardsLeftRoundState(new GameRound(new Player(), new Player(), PlayerPosition.FirstPlayer));

            PlayerTurnContext context = new PlayerTurnContext(middle, new Card(CardSuit.Diamond, CardType.Jack), 6);

            context.FirstPlayedCard = new Card(CardSuit.Club, CardType.King);

            // Cannot Close game because it's already played a card.
            PlayerAction action = new PlayerAction(PlayerActionType.CloseGame, new Card(CardSuit.Club, CardType.Nine), Announce.None);
            IList<Card> cards = new List<Card>()
            {
                new Card(CardSuit.Spade, CardType.Queen),
                new Card(CardSuit.Spade, CardType.King),
                new Card(CardSuit.Heart, CardType.Queen),
                new Card(CardSuit.Club, CardType.Ace),
                new Card(CardSuit.Club, CardType.Nine),
                new Card(CardSuit.Diamond, CardType.Nine)
            };

            Assert.IsFalse(validator.IsValid(action, context, cards));
        }