Ejemplo n.º 1
0
        private static void MakeTurn(Player player1, Player player2, Deck deck)
        {
            //give card
            var card = MovementUtil.MakeTurn(player1, player2, deck, null);

            //Check for additional point -> (20 or 40)
            //TODO Idea for modification: Player choose if he wants to call his announce. If has more than one announce can choose which one wants to play.
            if (SixtySixUtil.HasForty(player1.Cards, card, deck))
            {
                SixtySixUtil.CallForty(player1);
                player1.HasTwentyForty.Add(new Card()
                {
                    Suit = card.Suit, Value = card.Value == CardValue.QUEEN ? CardValue.KING : CardValue.QUEEN
                });
            }
            else if (SixtySixUtil.HasTwenty(player1.Cards, card, deck))
            {
                SixtySixUtil.CallTwenty(player1);
                player1.HasTwentyForty.Add(new Card()
                {
                    Suit = card.Suit, Value = card.Value == CardValue.QUEEN ? CardValue.KING : CardValue.QUEEN
                });
            }

            var otherCard = MovementUtil.MakeTurn(player2, player1, deck, card);
            var handScore = (int)card.Value + (int)otherCard.Value;

            deck.ThrownCards.Add(card);
            deck.ThrownCards.Add(otherCard);

            // player1 plays first, so if first card wins, then the first player wins
            if (SixtySixUtil.WinsFirstCard(card, otherCard, deck.TrumpSuit))
            {
                if (!(player1.IsSilent && player2.IsSilent))
                {
                    Console.WriteLine("Winning card {0}", card);
                }
                player1.Score         += handScore;
                player1.HasWonLastHand = true;
                player2.HasWonLastHand = false;
                SixtySixUtil.DrawCard(player1, deck);
                SixtySixUtil.DrawCard(player2, deck);
            }
            else
            {
                if (!(player1.IsSilent && player2.IsSilent))
                {
                    Console.WriteLine("Winning card {0}", otherCard);
                }
                player2.Score         += handScore;
                player2.HasWonLastHand = true;
                player1.HasWonLastHand = false;
                SixtySixUtil.DrawCard(player2, deck);
                SixtySixUtil.DrawCard(player1, deck);
            }
        }
Ejemplo n.º 2
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());
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public CardsBoardViewModel()
        {
            this.Player   = new PlayerViewModel(false);
            this.Opponent = new PlayerViewModel(true, PlayStrategy.MCTS);
            this.Deck     = CardsDeckUtil.InitializeDeck();
            CardsDeckUtil.ShuffleDeck(this.Deck);
            var player   = this.Player.ToPlayer();
            var opponent = this.Opponent.ToPlayer();

            SixtySixUtil.DealCards(this.Deck, player, opponent);
            this.Player   = PlayerViewModel.ConvertToPlayerViewModel(player);
            this.Opponent = PlayerViewModel.ConvertToPlayerViewModel(opponent);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        private static void HandleCallingAnnounce(PlayerViewModel player, Deck deck)
        {
            var card = player.SelectedCard;

            if (card != null)
            {
                if (SixtySixUtil.HasForty(player.ToPlayer().Cards, card.ToCard(), deck))
                {
                    player.Score   += Constants.FORTY_ANNOUNCEMENT;
                    player.Messages = "Forty!!!";
                }
                else if (SixtySixUtil.HasTwenty(player.ToPlayer().Cards, card.ToCard(), deck))
                {
                    player.Score   += Constants.TWENTY_ANNOUNCEMENT;
                    player.Messages = "Twenty!!!";
                }
            }
        }
Ejemplo n.º 6
0
        /*
         * In the context of this method player1 has always win last game
         *
         */
        public static void PlayOneDeal(Deck deck, Player player1, Player player2)
        {
            //we have to deal the cards.
            SixtySixUtil.DealCards(deck, player1, player2);
            int turnNumber = 1;

            do
            {
                if (!(player1.IsSilent && player2.IsSilent))
                {
                    Console.WriteLine("TURN: {0}", turnNumber++);
                    Console.WriteLine("TRUMP: {0}!!! {1} cards in the deck.", deck.Cards.Count() > 0 ? deck.Cards.Last().ToString() : deck.TrumpSuit.ToString(), deck.Cards.Count());
                    Console.WriteLine("-" + player1.ToString() + " has " + player1.Score + " points");
                    Console.WriteLine("-" + player2.ToString() + " has " + player2.Score + " points");
                    Console.WriteLine();
                }
                if (player1.HasWonLastHand)
                {
                    MakeTurn(player1, player2, deck);

                    if (SixtySixUtil.IsSixtySixReached(player1, player2))
                    {
                        break;
                    }
                }
                else if (player2.HasWonLastHand)
                {
                    MakeTurn(player2, player1, deck);

                    if (SixtySixUtil.IsSixtySixReached(player2, player1))
                    {
                        break;
                    }
                }

                if (!(player1.IsSilent && player2.IsSilent))
                {
                    Console.WriteLine("=============================================================================");
                }
            } while (player1.Cards.Count() > 0 && player2.Cards.Count() > 0);

            CardsDeckUtil.CollectCardsInDeck(deck, player1, player2);
            Console.Clear();
        }
Ejemplo n.º 7
0
 private void ChangeTrumpCardLogic(PlayerViewModel player)
 {
     if (SixtySixUtil.CanSwap(CardViewModel.ConvertListOfCardViewModelsToListOFCard(player.Cards), this.Deck))
     {
         var card = player.Cards.FirstOrDefault(x => { return(x.Suit == this.Deck.TrumpSuit && x.Value == CardValue.NINE); });
         if (card != null && player.Cards.Contains(card))
         {
             player.Cards.Add(this.TrumpCard);
             this.Deck.Cards.Remove(this.TrumpCard.ToCard());
             this.TrumpCard = card;
             player.Cards.Remove(card);
             player.Messages = "Change Trump card!!!";
         }
     }
     else
     {
         this.BoardMessage = "Cannot Change The Trump card!!!";
     }
 }
Ejemplo n.º 8
0
        public static Card MakeTurn(Player player, Deck deck, Card playedFromOther = null)
        {
            if (!player.IsSilent)
            {
                Console.WriteLine();
                if (playedFromOther != null)
                {
                    Console.WriteLine("Other player played: " + playedFromOther);
                }
                Console.WriteLine("Your Hand: " + player.ToStringPlayerCards());
            }
            Card card = null;

            do
            {
                Console.WriteLine("Ender the choosen card in format <<<cardValue cardSuit>>>");
                String input = Console.ReadLine();
                var    parts = input.Split(null);
                var    value = ParseInputToCardValue(parts[0]);
                var    suit  = ParseInputToCardSuit(parts[1]);

                card = new Card()
                {
                    Value = value, Suit = suit
                };
            } while (card == null || card.Suit == 0);

            if (SixtySixUtil.HasForty(player.Cards, card, deck))
            {
                SixtySixUtil.CallForty(player);
            }
            else if (SixtySixUtil.HasTwenty(player.Cards, card, deck))
            {
                SixtySixUtil.CallTwenty(player);
            }

            player.GiveCard(card);

            return(card);
        }
Ejemplo n.º 9
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);
                    }
                });
            }
        }
Ejemplo n.º 10
0
        private static Card GiveCardIfFirst(Player player, Deck deck)
        {
            var rand        = new Random(System.DateTime.Now.Millisecond);
            var playerCards = player.Cards;

            if (SixtySixUtil.HasForty(playerCards, deck))
            {
                return(player.Cards.First(x => x.Suit == deck.TrumpSuit && (x.Value == CardValue.KING || x.Value == CardValue.QUEEN)));
            }
            else if (SixtySixUtil.HasTwenty(playerCards, deck))
            {
                return(player.Cards.First(x => x.Suit != deck.TrumpSuit && (x.Value == CardValue.KING || x.Value == CardValue.QUEEN)));
            }
            //if player has Ace trump and will reach 66
            else if (playerCards.FirstOrDefault(x => x.Suit == deck.TrumpSuit && x.Value == CardValue.ACE) != null && player.Score + (int)CardValue.ACE >= Constants.TOTAL_SCORE)
            {
                return(playerCards.First(x => x.Suit == deck.TrumpSuit && x.Value == CardValue.ACE));
            }
            //if Ace trump is thrown and player has 10 trump and will reach 66
            else if (playerCards.FirstOrDefault(x => x.Suit == deck.TrumpSuit && x.Value == CardValue.TEN) != null && deck.ThrownCards.Contains(new Card(CardValue.ACE, deck.TrumpSuit)) && player.Score + (int)CardValue.TEN >= Constants.TOTAL_SCORE)
            {
                return(playerCards.First(x => x.Suit == deck.TrumpSuit && x.Value == CardValue.TEN));
            }
            else
            {
                // give the lowest card
                if (deck.Cards.Count > 0 && !deck.IsClosed)
                {
                    // 9 not trump
                    if (playerCards.FirstOrDefault(x => x.Value == CardValue.NINE && x.Suit != deck.TrumpSuit) != null)
                    {
                        return(playerCards.First(x => x.Value == CardValue.NINE && x.Suit != deck.TrumpSuit));
                    }
                    // Jack not trump
                    else if (playerCards.FirstOrDefault(x => x.Value == CardValue.JACK && x.Suit != deck.TrumpSuit) != null)
                    {
                        return(playerCards.First(x => x.Value == CardValue.JACK && x.Suit != deck.TrumpSuit));
                    }
                    // Queen not trump
                    else if (playerCards.FirstOrDefault(x => x.Value == CardValue.QUEEN && x.Suit != deck.TrumpSuit) != null)
                    {
                        return(playerCards.First(x => x.Value == CardValue.QUEEN && x.Suit != deck.TrumpSuit));
                    }
                    // KING not trump
                    else if (playerCards.FirstOrDefault(x => x.Value == CardValue.KING && x.Suit != deck.TrumpSuit) != null)
                    {
                        return(playerCards.First(x => x.Value == CardValue.KING && x.Suit != deck.TrumpSuit));
                    }
                    // supposed to be in this case only if the player has only 10 and Aces then just play a random card
                    else
                    {
                        return(playerCards[rand.Next(playerCards.Count)]);
                    }
                }
                else
                {
                    // the cards in deck are left OR the game is closed
                    var aces = playerCards.Where(x => x.Value == CardValue.ACE);
                    var tenthsWhichAcesAreThrown = playerCards.Where(x => x.Value == CardValue.TEN && deck.ThrownCards.Contains(new Card(CardValue.ACE, x.Suit)));
                    if (aces.FirstOrDefault(x => x.Suit == deck.TrumpSuit) != null)
                    {
                        return(aces.First(x => x.Suit == deck.TrumpSuit));
                    }
                    else if (tenthsWhichAcesAreThrown.FirstOrDefault(x => x.Suit == deck.TrumpSuit) != null && deck.ThrownCards.Contains(new Card(CardValue.ACE, deck.TrumpSuit)))
                    {
                        return(tenthsWhichAcesAreThrown.First(x => x.Suit == deck.TrumpSuit));
                    }
                    else if (aces.Count() > 0)
                    {
                        return(aces.First());
                    }
                    else if (tenthsWhichAcesAreThrown.Count() > 0)
                    {
                        return(tenthsWhichAcesAreThrown.First());
                    }
                    else
                    {
                        return(playerCards[rand.Next(playerCards.Count)]);
                    }
                }
            }
        }