Esempio n. 1
0
        private static Card GiveCardIfOtherHasPlayedPhase2(Player player, Deck deck, Card playedFromOther)
        {
            //var rand = new Random(System.DateTime.Now.Millisecond);
            var playerCards  = player.Cards;
            var playerTrumps = playerCards.Where(x => x.Suit == playedFromOther.Suit);

            // if other player has played trump
            if (playedFromOther.Suit == deck.TrumpSuit)
            {
                if (playerTrumps != null && playerTrumps.Count() > 0)
                {
                    if (playerTrumps.FirstOrDefault(x => x.Value > playedFromOther.Value) != null)
                    {
                        return(playerTrumps.Max());
                    }
                    else
                    {
                        return(playerTrumps.Min());
                    }
                }
                else
                {
                    return(playerCards.Min());
                }
            }
            else
            {
                if (SixtySixUtil.HasAnsweringCard(player, playedFromOther))
                {
                    var answeringCards = SixtySixUtil.GetHandAnsweringCards(player, playedFromOther);

                    if (answeringCards.FirstOrDefault(x => x.Value > playedFromOther.Value) != null)
                    {
                        return(player.Cards.Max());
                    }
                    else
                    {
                        return(playerTrumps.Min());
                    }
                }
                else
                {
                    if (playedFromOther.Value == CardValue.ACE || playedFromOther.Value == CardValue.TEN && playerTrumps.Count() > 0)
                    {
                        return(playerTrumps.Min());
                    }
                    else
                    {
                        return(playerCards.Min());
                    }
                }
            }
        }
Esempio n. 2
0
        /*
         * TODO
         *      Handle reaching 66 (Note: handle end of game by cannling)
         */
        private void HandleGiveCardCommand(object parameter)
        {
            //This check handle the case in which input player gives card before the AI player, when the AI player is on turn
            if (this.Opponent.HasWonLastHand && this.Opponent.SelectedCard == null)
            {
                return;
            }

            if (Deck.Cards.Count == 0)
            {
                this.TrumpCard = null;
            }

            this.Player.Messages   = null;
            this.Opponent.Messages = null;


            if (parameter != null)
            {
                var playerCard = (CardViewModel)parameter;

                if (this.Opponent.SelectedCard != null &&
                    SixtySixUtil.HasToAnswerWithMatching(this.Deck, this.Opponent.SelectedCard.ToCard()) &&
                    SixtySixUtil.HasAnsweringCard(this.Player.ToPlayer(), this.Opponent.SelectedCard.ToCard()))
                {
                    var playedFromOther = this.Opponent.SelectedCard.ToCard();
                    var answeringCards  = SixtySixUtil.GetHandAnsweringCards(this.Player.ToPlayer(), playedFromOther);
                    if (!answeringCards.Contains(playerCard.ToCard()))
                    {
                        this.BoardMessage = "Player, you have to answer with matching card!!!";
                        return;
                    }
                }

                this.Player.SelectedCard = (CardViewModel)parameter;
                if (this.Opponent.SelectedCard == null)
                {
                    HandleCallingAnnounce(this.Player, this.Deck);
                    if (HandleEndOfDeal(this.Player, this.Opponent, this.Deck))
                    {
                        return;
                    }
                }
            }

            bool winsFirstCard;

            if (this.Opponent.SelectedCard == null)
            {
                var opponentCard = CardViewModel.ConvertToCardViewModel(AIMovementUtil.MakeTurn(this.Opponent.ToPlayer(), this.Player.ToPlayer(), this.Deck, this.Player.SelectedCard.ToCard()));
                this.Opponent.SelectedCard = opponentCard;
                winsFirstCard = SixtySixUtil.WinsFirstCard(this.Player.SelectedCard.ToCard(), this.Opponent.SelectedCard.ToCard(), this.Deck.TrumpSuit);
            }
            else
            {
                winsFirstCard = !SixtySixUtil.WinsFirstCard(this.Opponent.SelectedCard.ToCard(), this.Player.SelectedCard.ToCard(), this.Deck.TrumpSuit);
            }

            var handScore = (int)this.Player.SelectedCard.Value + (int)this.Opponent.SelectedCard.Value;



            if (winsFirstCard)
            {
                this.Player.HasWonLastHand   = true;
                this.Opponent.HasWonLastHand = false;


                this.BoardMessage = "Player wins";
                //the player holds the hand
                this.Player.Score = this.Player.Score + handScore;

                var newPlayerCard = SixtySixUtil.DrawCard(this.Player.ToPlayer(), this.Deck);
                if (newPlayerCard != null)
                {
                    this.Player.Cards.Add(CardViewModel.ConvertToCardViewModel(newPlayerCard));
                    var newOpponentCard = SixtySixUtil.DrawCard(this.Opponent.ToPlayer(), this.Deck);
                    if (newOpponentCard != null)
                    {
                        this.Opponent.Cards.Add(CardViewModel.ConvertToCardViewModel(newOpponentCard));
                    }
                }

                Task.Delay(1000).ContinueWith(_ =>
                {
                    if (HandleEndOfDeal(this.Player, this.Opponent, this.Deck))
                    {
                        this.Player.SelectedCard   = null;
                        this.Opponent.SelectedCard = null;
                        return;
                    }
                    this.Player.SelectedCard   = null;
                    this.Opponent.SelectedCard = null;
                });
            }
            else
            {
                //the opponent hold the hand
                this.Opponent.HasWonLastHand = true;
                this.Player.HasWonLastHand   = false;

                this.BoardMessage   = "Opponent wins";
                this.Opponent.Score = this.Opponent.Score + handScore;

                var newOpponentCard = SixtySixUtil.DrawCard(this.Opponent.ToPlayer(), this.Deck);
                if (newOpponentCard != null)
                {
                    this.Opponent.Cards.Add(CardViewModel.ConvertToCardViewModel(newOpponentCard));
                    var newPlayerCard = SixtySixUtil.DrawCard(this.Player.ToPlayer(), this.Deck);
                    if (newPlayerCard != null)
                    {
                        this.Player.Cards.Add(CardViewModel.ConvertToCardViewModel(newPlayerCard));
                    }
                }

                CardViewModel opponentCard = null;

                if (HandleEndOfDeal(this.Opponent, this.Player, this.Deck))
                {
                    return;
                }

                Task.Delay(1000).ContinueWith(_ =>
                {
                    this.Player.SelectedCard   = null;
                    this.Opponent.SelectedCard = null;
                });

                if (this.Player.SelectedCard == null)
                {
                    ChangeTrumpCardLogic(this.Opponent);
                }

                opponentCard = CardViewModel.ConvertToCardViewModel(AIMovementUtil.MakeTurn(this.Opponent.ToPlayer(), this.Player.ToPlayer(), this.Deck));
                this.Opponent.Cards.Remove(opponentCard);


                Task.Delay(1500).ContinueWith(_ =>
                {
                    if (Deck.Cards.Count == 0)
                    {
                        this.TrumpCard = null;
                    }

                    this.Opponent.SelectedCard = opponentCard;
                    if (this.Player.SelectedCard == null)
                    {
                        HandleCallingAnnounce(this.Opponent, this.Deck);
                        HandleEndOfDeal(this.Opponent, this.Player, this.Deck);
                    }
                });
            }
        }