public override PlayerAction GetTurn(PlayerTurnContext context)
 {
     var possibleCardsToPlay = this.PlayerActionValidator.GetPossibleCardsToPlay(context, this.Cards);
     var shuffledCards = possibleCardsToPlay.Shuffle();
     var cardToPlay = shuffledCards.First();
     return this.PlayCard(cardToPlay);
 }
        private PlayerAction EndGameTurn(PlayerTurnContext context, ICardTracker tracker, ICollection<Card> myHand)
        {
            this.root = new Node(null, null, context.IsFirstPlayerTurn);

            if (context.IsFirstPlayerTurn && this.root.Children.Count == 0)
            {
                tracker.SetFinalRoundHands(myHand);
                var myCards = myHand.ToList();

                EndgameAnalyzer.Compute(this.root, null, myCards, tracker.OpponentTookWith, context.SecondPlayerRoundPoints, context.FirstPlayerRoundPoints);
            }
            else if (this.root.Children.Count == 0)
            {
                tracker.SetFinalRoundHands(myHand);
                var myCards = myHand.ToList();

                while (tracker.OpponentTookWith.Count < 6)
                {
                    tracker.OpponentTookWith.Add(null);
                }

                EndgameAnalyzer.Compute(this.root, context.FirstPlayedCard, myCards, tracker.OpponentTookWith, context.FirstPlayerRoundPoints, context.SecondPlayerRoundPoints);
            }

            if (!context.IsFirstPlayerTurn)
            {
                this.root = this.root.Children.First(x => x.Card == context.FirstPlayedCard);
            }

            var card = this.root.Children.OrderByDescending(x => x.Wins / (decimal)x.Total).First().Card;

            return PlayerAction.PlayCard(card);
        }
Ejemplo n.º 3
0
        public override PlayerAction GetTurn(PlayerTurnContext context)
        {
            //if (this.PlayerActionValidator.IsValid(PlayerAction.ChangeTrump(), context, this.Cards))
            //{
            //    this.CardsNotInDeck.Add(context.TrumpCard);

            //    this.CardsNotInDeck.RemoveWhere(c => c.Suit == context.TrumpCard.Suit && c.Type == CardType.Nine);

            //    return this.ChangeTrump(context.TrumpCard);
            //}

            //foreach (var card in this.Cards)
            //{
            //    if (card.Type == CardType.King &&
            //        (this.AnnounceValidator.GetPossibleAnnounce(this.Cards, card, context.TrumpCard) == Announce.Forty ||
            //         this.AnnounceValidator.GetPossibleAnnounce(this.Cards, card, context.TrumpCard) == Announce.Twenty))
            //    {
            //        this.Cards.Remove(card);
            //        return this.PlayCard(card);
            //    }
            //}

            var bestCard = this.GetCardToPlay(context);

            return this.PlayCard(bestCard);
        }
        private float MaxHandValue(Card card, PlayerTurnContext context)
        {
            var suit = card.Suit;
            var value = card.GetValue();
            var isTrump = context.TrumpCard.Suit == suit;

            var result = 0f;

            var cardsToTake = this.cardtracker
                .AllCards[suit]
                .Where(x => x.Key < value &&
                    (x.Value == CardTracerState.InOpponentHand || x.Value == CardTracerState.Unknown))
                .ToList();

            if (isTrump)
            {
                cardsToTake
                    .AddRange(this.cardtracker
                        .AllCards
                        .Where(s => s.Key != suit)
                        .SelectMany(c => c.Value)
                        .Where(x => x.Value == CardTracerState.InOpponentHand || x.Value == CardTracerState.Unknown));
            }

            var high = cardsToTake.Count > 0 ? cardsToTake.Max(x => x.Key) : 0;

            result += high;

            if (isTrump)
            {
                result += 10;
            }

            return result / (this.MaxTakeCases(value, suit) - cardsToTake.Count());
        }
 private float PointAndSuitCountParameter(Card card, PlayerTurnContext context, ICollection<Card> allowedCards)
 {
     var cardSuit = card.Suit;
     float cardValue = card.GetValue();
     float coutOfSuitInHand = allowedCards.Count(x => x.Suit == cardSuit);
     return (11f - cardValue) * coutOfSuitInHand;
 }
        public float CardScore(Card card, PlayerTurnContext context, ICollection<Card> allowedCards)
        {
            float result = 0f;
            result += this.PointAndSuitCountParameter(card, context, allowedCards);
            result -= PointAndSuitMultiplier * this.PointAndSuitCountParameter(card, context, allowedCards);

            return result;
        }
 public void CloneShouldReturnDifferentReference()
 {
     var haveStateMock = new Mock<IStateManager>();
     var state = new StartRoundState(haveStateMock.Object);
     var playerTurnContext = new PlayerTurnContext(state, new Card(CardSuit.Heart, CardType.Queen), 12, 0, 0);
     var clonedPlayerTurnContext = playerTurnContext.DeepClone();
     Assert.AreNotSame(playerTurnContext, clonedPlayerTurnContext);
 }
 public void CloneShouldReturnObjectOfTypePlayerTurnContext()
 {
     var haveStateMock = new Mock<IStateManager>();
     var state = new TwoCardsLeftRoundState(haveStateMock.Object);
     var playerTurnContext = new PlayerTurnContext(state, new Card(CardSuit.Diamond, CardType.Ace), 2, 0, 0);
     var clonedPlayerTurnContext = playerTurnContext.DeepClone();
     Assert.IsInstanceOf<PlayerTurnContext>(clonedPlayerTurnContext);
 }
Ejemplo n.º 9
0
 public override void EndTurn(PlayerTurnContext context)
 {
     this.UpdateContextInfo(context);
     var playedCards = this.iAmFirstThisTurn
                           ? new Tuple<Card, Card>(context.FirstPlayedCard, context.SecondPlayedCard)
                           : new Tuple<Card, Card>(context.SecondPlayedCard, context.FirstPlayedCard);
     this.RedrawPlayedCards?.Invoke(this, playedCards);
     base.EndTurn(context);
 }
 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));
 }
        private int NumberOfHandsCardCanTake(Card card, PlayerTurnContext context)
        {
            var count = this.cardTracker.AllCards[card.Suit]
                   .Where(x => (x.Value == CardTracerState.InOpponentHand || x.Value == CardTracerState.Unknown)
                               && x.Key < card.GetValue())
                   .Count();

            return count;
        }
        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));
        }
Ejemplo n.º 13
0
        public override void EndTurn(PlayerTurnContext context)
        {
            Console.SetCursorPosition(20, 9);
            Console.WriteLine($"{context.FirstPlayedCard} - {context.SecondPlayedCard}             ");
            Thread.Sleep(3000);

            Console.SetCursorPosition(20, 9);
            Console.WriteLine($"                  ");
        }
        private float BiggestGainParameter(Card card, PlayerTurnContext context, ICollection<Card> allowedCards)
        {
            var suit = card.Suit;
            float value = card.GetValue();

            var allOfSuit = this.cardtracker.AllCards[suit];
            var maxAvailableForTaking = allOfSuit.Where(x => x.Value != CardTracerState.TakenByOpponent ||
                                                            x.Value != CardTracerState.TakenByPlayer).Max(x => x.Key);
            return (value + maxAvailableForTaking) / BiggestGainDivisor;
        }
Ejemplo n.º 15
0
 public GameAction(Card opponent, Card player, PlayerTurnContext context, ICardTracker cardTracker)
 {
     this.OpponetCard = opponent;
     this.PlayerCard = player;
     this.PlayerTakes = this.CheckIfPlayerTakes(opponent, player, context);
     this.HandValue = opponent.GetValue() + player.GetValue();
     this.CardMaxWorth = this.CardMaxWorthCalculator(player, cardTracker);
     this.OpponentPoints = context.FirstPlayerRoundPoints;
     this.PlayerPoints = context.SecondPlayerRoundPoints;
 }
        public override PlayerAction GetTurn(PlayerTurnContext context)
        {
            // When possible change the trump card as this is always a good move
            if (this.PlayerActionValidator.IsValid(PlayerAction.ChangeTrump(), context, this.Cards))
            {
                return this.ChangeTrump(context.TrumpCard);
            }

            return base.GetTurn(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));
        }
Ejemplo n.º 18
0
        public ICollection<GameAction> GetPossibleTakeCard(PlayerTurnContext context, ICollection<Card> hand)
        {
            var actions = new List<GameAction>();

            foreach (var item in hand)
            {
                actions.Add(new GameAction(context.FirstPlayedCard, item, context, this.cardTracker));
            }

            return actions;
        }
Ejemplo n.º 19
0
        // TODO: Improve close game decision
        private bool CloseGame(PlayerTurnContext context)
        {
            var shouldCloseGame = this.PlayerActionValidator.IsValid(PlayerAction.CloseGame(), context, this.Cards)
                                  && this.Cards.Count(x => x.Suit == context.TrumpCard.Suit) == 5;
            if (shouldCloseGame)
            {
                GlobalStats.GamesClosedByPlayer++;
            }

            return shouldCloseGame;
        }
Ejemplo n.º 20
0
 public override PlayerAction GetTurn(PlayerTurnContext context)
 {
     this.logger.LogLine("--GetTurn; "
                         + $"Trump: {context.TrumpCard}({context.CardsLeftInDeck}); "
                         + $"State: {context.State.GetType().Name.Replace("RoundState", string.Empty)}; "
                         + $"First: {context.FirstPlayedCard}({context.FirstPlayerAnnounce}); "
                         + $"I am first: {context.IsFirstPlayerTurn}");
     var action = base.GetTurn(context);
     this.logger.LogLine($"Playing {action}");
     return action;
 }
 public void AmITheFirstPlayerShouldReturnFalseWhenCardIsAlreadyPlayed()
 {
     var haveStateMock = new Mock<IStateManager>();
     var playerTurnContext = new PlayerTurnContext(
         new FinalRoundState(haveStateMock.Object),
         new Card(CardSuit.Club, CardType.Ace),
         0,
         0,
         0) { FirstPlayedCard = new Card(CardSuit.Diamond, CardType.Ten) };
     Assert.IsFalse(playerTurnContext.IsFirstPlayerTurn);
 }
 public void AmITheFirstPlayerShouldReturnTrueWhenCardIsNotPlayedYet()
 {
     var haveStateMock = new Mock<IStateManager>();
     var playerTurnContext = new PlayerTurnContext(
         new FinalRoundState(haveStateMock.Object),
         new Card(CardSuit.Club, CardType.Ace),
         0,
         0,
         0);
     Assert.IsTrue(playerTurnContext.IsFirstPlayerTurn);
 }
Ejemplo n.º 23
0
 // TODO: Improve choosing best card to play
 private PlayerAction ChooseCard(PlayerTurnContext context)
 {
     var possibleCardsToPlay = this.PlayerActionValidator.GetPossibleCardsToPlay(context, this.Cards);
     return context.State.ShouldObserveRules
                ? (context.IsFirstPlayerTurn
                       ? this.ChooseCardWhenPlayingFirstAndRulesApply(context, possibleCardsToPlay)
                       : this.ChooseCardWhenPlayingSecondAndRulesApply(context, possibleCardsToPlay))
                : (context.IsFirstPlayerTurn
                       ? this.ChooseCardWhenPlayingFirstAndRulesDoNotApply(context, possibleCardsToPlay)
                       : this.ChooseCardWhenPlayingSecondAndRulesDoNotApply(context, possibleCardsToPlay));
 }
Ejemplo n.º 24
0
        private PlayerAction ChooseCardWhenPlayingFirstAndRulesApply(
            PlayerTurnContext context,
            ICollection<Card> possibleCardsToPlay)
        {
            // Find card that will surely win the trick
            var opponentHasTrump =
                this.opponentSuitCardsProvider.GetOpponentCards(
                    this.Cards,
                    this.playedCards,
                    context.CardsLeftInDeck == 0 ? null : context.TrumpCard,
                    context.TrumpCard.Suit).Any();

            var trumpCard = this.GetCardWhichWillSurelyWinTheTrick(
                context.TrumpCard.Suit,
                context.CardsLeftInDeck == 0 ? null : context.TrumpCard,
                opponentHasTrump);
            if (trumpCard != null)
            {
                return this.PlayCard(trumpCard);
            }

            foreach (CardSuit suit in Enum.GetValues(typeof(CardSuit)))
            {
                var possibleCard = this.GetCardWhichWillSurelyWinTheTrick(
                    suit,
                    context.CardsLeftInDeck == 0 ? null : context.TrumpCard,
                    opponentHasTrump);
                if (possibleCard != null)
                {
                    return this.PlayCard(possibleCard);
                }
            }

            // Announce 40 or 20 if possible
            var action = this.TryToAnnounce20Or40(context, possibleCardsToPlay);
            if (action != null)
            {
                return action;
            }

            // Smallest non-trump card
            var cardToPlay =
                possibleCardsToPlay.Where(x => x.Suit != context.TrumpCard.Suit)
                    .OrderBy(x => x.GetValue())
                    .FirstOrDefault();
            if (cardToPlay != null)
            {
                return this.PlayCard(cardToPlay);
            }

            // Smallest card
            cardToPlay = possibleCardsToPlay.OrderBy(x => x.GetValue()).FirstOrDefault();
            return this.PlayCard(cardToPlay);
        }
        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);
        }
Ejemplo n.º 26
0
        public override PlayerAction GetTurn(PlayerTurnContext context)
        {
            var possibleCardsToPlay = this.PlayerActionValidator.GetPossibleCardsToPlay(context, this.Cards);
            var shuffledCards = possibleCardsToPlay.Shuffle();
            var cardToPlay = shuffledCards.First();

            // SecondPlayer == Opponent
            var opponentCardType = context.SecondPlayedCard.Type;
            var opponentCardSuit = context.SecondPlayedCard.Suit;

            return this.PlayCard(cardToPlay);
        }
Ejemplo n.º 27
0
        public override PlayerAction GetTurn(PlayerTurnContext context)
        {
            this.logger.LogLine("--GetTurn; "
                                + $"Trump: {context.TrumpCard}({context.CardsLeftInDeck}); "
                                + $"State: {context.State.GetType().Name.Replace("RoundState", string.Empty)}; "
                                + $"First: {context.FirstPlayedCard}({context.FirstPlayerAnnounce}); "
                                + $"I am first: {context.IsFirstPlayerTurn}");
            var action = base.GetTurn(context);

            this.logger.LogLine($"Playing {action}");
            return(action);
        }
Ejemplo n.º 28
0
        public override void EndTurn(PlayerTurnContext context)
        {
            if (context.FirstPlayedCard != null)
            {
                this.CardsNotInDeck.Add(context.FirstPlayedCard);
            }

            if (context.SecondPlayedCard != null)
            {
                this.CardsNotInDeck.Add(context.SecondPlayedCard);
            }
        }
Ejemplo n.º 29
0
        public Card ChooseCardToPlay(PlayerTurnContext context, ICollection<Card> stalkerCards)
        {
            var trumpSuit = context.TrumpCard.Suit;
            var highestPrioritySuit = 0;
            var priorityValue = int.MaxValue;

            // Get priorities for all suits
            var suitsPriorities = this.GetPriorityForEachSuit(context);

            // Check which suits are available
            var availableSuits = new int[4];
            foreach (var card in stalkerCards)
            {
                var suit = (int)card.Suit;
                availableSuits[suit]++;
            }

            for (var i = 0; i < suitsPriorities.Length; i++)
            {
                if ((int)trumpSuit == i || availableSuits[i] == 0)
                {
                    continue;
                }

                if (suitsPriorities[i] < priorityValue)
                {
                    highestPrioritySuit = i;
                    priorityValue = suitsPriorities[i];
                }
            }

            // Select the cards from the best suit
            var cardsFromBestSuit = stalkerCards.Where(card => card.Suit == (CardSuit)highestPrioritySuit).OrderBy(this.stalkerHelper.GetCardPriority).ToList();
            var cardsFromTrump = stalkerCards.Where(card => card.Suit == trumpSuit).OrderBy(this.stalkerHelper.GetCardPriority).ToList();

            if (!context.State.ShouldObserveRules)
            {
                // Take all nontrump cards without Queen and King waiting for announce
                cardsFromBestSuit = stalkerCards.Where(c => c.Suit != trumpSuit && !(this.stalkerHelper.GetCardPriority(c) == 1 && this.IsCardWaitingForAnnounce(c))).OrderBy(this.stalkerHelper.GetCardPriority).ToList();
            }

            // Sort cards by its priority
            var cardsToChooseFrom = cardsFromBestSuit.Count != 0 ? cardsFromBestSuit : cardsFromTrump;

            if (!context.State.ShouldObserveRules)
            {
                // THIS NUMBER WILL AFFECT THE DECISION OF THE STALKER WHEN IN OPEN STATE
                return priorityValue > CardChooserConstants.OpenGamePriority ? cardsToChooseFrom.LastOrDefault() : cardsToChooseFrom.FirstOrDefault();
            }

            // THIS NUMBER WILL AFFECT THE DECISION OF THE STALKER WHEN IN CLOSED STATE
            return priorityValue < CardChooserConstants.ClosedGamePriority ? cardsToChooseFrom.LastOrDefault() : cardsToChooseFrom.FirstOrDefault();
        }
Ejemplo n.º 30
0
        private PlayerAction ChooseCardWhenPlayingFirstAndRulesDoNotApply(
            PlayerTurnContext context,
            ICollection<Card> possibleCardsToPlay)
        {
            // Announce 40 or 20 if possible
            var action = this.TryToAnnounce20Or40(context, possibleCardsToPlay);
            if (action != null)
            {
                return action;
            }

            // If the player is close to the win => play trump card which will surely win the trick
            var opponentBiggestTrumpCard =
                this.opponentSuitCardsProvider.GetOpponentCards(
                    this.Cards,
                    this.playedCards,
                    context.TrumpCard,
                    context.TrumpCard.Suit).OrderByDescending(x => x.GetValue()).FirstOrDefault();
            var myBiggestTrumpCard =
                possibleCardsToPlay.Where(x => x.Suit == context.TrumpCard.Suit)
                    .OrderByDescending(x => x.GetValue())
                    .FirstOrDefault();

            if (context.FirstPlayerRoundPoints >= 66 - myBiggestTrumpCard?.GetValue())
            {
                if (opponentBiggestTrumpCard == null
                    || myBiggestTrumpCard.GetValue() > opponentBiggestTrumpCard.GetValue())
                {
                    return this.PlayCard(myBiggestTrumpCard);
                }
            }

            // Smallest non-trump card from the shortest opponent suit
            var cardToPlay =
                possibleCardsToPlay.Where(x => x.Suit != context.TrumpCard.Suit)
                    .OrderBy(
                        x =>
                        this.opponentSuitCardsProvider.GetOpponentCards(
                            this.Cards,
                            this.playedCards,
                            context.TrumpCard,
                            x.Suit).Count)
                    .ThenBy(x => x.GetValue())
                    .FirstOrDefault();
            if (cardToPlay != null)
            {
                return this.PlayCard(cardToPlay);
            }

            // Should never happen
            cardToPlay = possibleCardsToPlay.OrderBy(x => x.GetValue()).FirstOrDefault();
            return this.PlayCard(cardToPlay);
        }
        public override PlayerAction GetTurn(PlayerTurnContext context)
        {
            // When possible change the trump card as this is always a good move
            if (this.PlayerActionValidator.IsValid(PlayerAction.ChangeTrump(), context, this.Cards))
            {
                return this.ChangeTrump(context.TrumpCard);
            }

            var possibleCardsToPlay = this.PlayerActionValidator.GetPossibleCardsToPlay(context, this.Cards);
            var shuffledCards = possibleCardsToPlay.Shuffle();
            var cardToPlay = shuffledCards.First();
            return this.PlayCard(cardToPlay);
        }
Ejemplo n.º 32
0
 public virtual PlayerAction GetTurn(PlayerTurnContext context)
 {
     return(this.Player.GetTurn(context));
 }
Ejemplo n.º 33
0
 public PlayerAction GetTurn(PlayerTurnContext context, IPlayerActionValidater actionValidator)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 34
0
 public void EndTurn(PlayerTurnContext context)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 35
0
 public abstract PlayerAction GetTurn(
     PlayerTurnContext context,
     IPlayerActionValidator actionValidater);
Ejemplo n.º 36
0
 public virtual void EndTurn(PlayerTurnContext context)
 {
 }
Ejemplo n.º 37
0
 public override void EndTurn(PlayerTurnContext context)
 {
     this.logger.LogLine(
         $"End of turn {context.FirstPlayedCard}({context.FirstPlayerAnnounce}) - {context.SecondPlayedCard}");
     base.EndTurn(context);
 }
Ejemplo n.º 38
0
 public abstract PlayerAction GetTurn(PlayerTurnContext context);
Ejemplo n.º 39
0
 public virtual void EndTurn(PlayerTurnContext context)
 {
     this.Player.EndTurn(context);
 }