Exemple #1
0
 public static int GetDeckSplittingIndex(Player player)
 {
     if (player.IsAIPlayer)
     {
         return(AIMovementUtil.GetDeckSplittingIndex());
     }
     else
     {
         return(InputPlayerMovementUtil.GetDeckSplittingIndex());
     }
 }
Exemple #2
0
 public static Card MakeTurn(Player player, Player other, Deck deck, Card playedFromOther = null)
 {
     if (player.IsAIPlayer)
     {
         return(AIMovementUtil.MakeTurn(player, other, deck, playedFromOther));
     }
     else
     {
         return(InputPlayerMovementUtil.MakeTurn(player, deck, playedFromOther));
     }
 }
        private bool HandleEndOfDeal(PlayerViewModel player, PlayerViewModel opponent, Deck deck)
        {
            if (player.HasWonLastHand && player.Score >= Constants.TOTAL_SCORE)
            {
                deck.IsEndOfGame = true;

                var enginePlayer   = player.ToPlayer();
                var engineOpponent = opponent.ToPlayer();
                CardsDeckUtil.CollectCardsInDeck(deck, enginePlayer, engineOpponent);

                player.Cards = new ObservableCollection <CardViewModel>();

                player.WinsCount     += SixtySixUtil.GetNumberOfWins(opponent.ToPlayer());
                player.Score          = 0;
                opponent.Score        = 0;
                this.TrumpCard        = null;
                player.SelectedCard   = null;
                opponent.SelectedCard = null;

                player.HasWonLastDeal   = true;
                opponent.HasWonLastDeal = false;
                player.ThrownCards      = new ObservableCollection <CardViewModel>();
                opponent.Cards          = new ObservableCollection <CardViewModel>();
                opponent.ThrownCards    = new ObservableCollection <CardViewModel>();


                if (enginePlayer.HasWonLastDeal)
                {
                    var splitIndex = AIMovementUtil.GetDeckSplittingIndex();
                    CardsDeckUtil.SplitDeck(deck, splitIndex); //one of the players should split the deck
                    SixtySixUtil.DealCards(deck, engineOpponent, enginePlayer);
                }
                else if (engineOpponent.HasWonLastDeal)
                {
                    //TODO Get User Input
                    var splitIndex = 10;                       //MovementUtil.GetDeckSplittingIndex(engineOpпonent);
                    CardsDeckUtil.SplitDeck(deck, splitIndex); //one of the players should split the deck
                    SixtySixUtil.DealCards(deck, enginePlayer, engineOpponent);
                }

                player   = PlayerViewModel.ConvertToPlayerViewModel(enginePlayer);
                opponent = PlayerViewModel.ConvertToPlayerViewModel(engineOpponent);

                player.Messages   = "WIN";
                opponent.Messages = "LOSE";

                return(true);
            }
            return(false);
        }
        /*
         * 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);
                    }
                });
            }
        }