Exemple #1
0
        public int PlayGame()
        {
            //only play if players exist.
            if (players == null)
            {
                return(-1);
            }
            //Deal initinal hands
            DealHands();
            bool GameWon = false;
            int  currentPlayer;
            Card playCard = playDeck.GetCard(currentCard++);

            discardedCards.Add(playCard);
            do
            {
                for (currentPlayer = 0; currentPlayer < players.Length; currentPlayer++)
                {
                    WriteLine($"{players[currentPlayer].Name}'s turn.");
                    WriteLine("Current hand:");
                    foreach (Card card in players[currentPlayer].PlayHand)
                    {
                        WriteLine(card);
                    }
                    WriteLine($"Card in play:{playCard}");
                    bool inputOK = false;
                    do
                    {
                        WriteLine("Press T to take card in play or D to draw:");
                        string input = ReadLine();
                        if (input.ToLower() == "t")
                        {
                            WriteLine($"Drawn:{playCard}");
                            if (discardedCards.Contains(playCard))
                            {
                                discardedCards.Remove(playCard);
                            }
                            players[currentPlayer].PlayHand.Add(playCard);
                            inputOK = true;
                        }
                        if (input.ToLower() == "d")
                        {
                            Card newCard;
                            bool cardIsAvailable;
                            do
                            {
                                newCard         = playDeck.GetCard(currentCard++);
                                cardIsAvailable = !discardedCards.Contains(newCard);
                                if (cardIsAvailable)
                                {
                                    foreach (Player testPlayer in players)
                                    {
                                        if (testPlayer.PlayHand.Contains(newCard))
                                        {
                                            cardIsAvailable = false;
                                            break;
                                        }
                                    }
                                }
                            } while (!cardIsAvailable);
                            WriteLine($"Drawn:{newCard}");
                            players[currentPlayer].PlayHand.Add(newCard);
                            inputOK = true;
                        }
                    } while (inputOK == false);
                    WriteLine("New hand:");
                    for (int i = 0; i < players[currentPlayer].PlayHand.Count; i++)
                    {
                        WriteLine($"{i + 1} : " + $"{players[currentPlayer].PlayHand[i]}");
                    }
                    inputOK = false;
                    int choice = -1;
                    do
                    {
                        WriteLine("Choose card to discard:");
                        string input = ReadLine();
                        try
                        {
                            choice = Convert.ToInt32(input);
                            if ((choice > 0) && (choice <= 8))
                            {
                                inputOK = true;
                            }
                        }
                        catch
                        {
                            //...
                        }
                    } while (inputOK == false);
                    playCard = players[currentPlayer].PlayHand[choice - 1];
                    players[currentPlayer].PlayHand.RemoveAt(choice - 1);
                    discardedCards.Add(playCard);
                    WriteLine($"Discarding:{playCard}");
                    WriteLine();
                    GameWon = players[currentPlayer].HasWon();
                    if (GameWon == true)
                    {
                        break;
                    }
                }
            } while (GameWon == false);
            return(currentPlayer);
        }
        private PlayAction PlayCard(IPlayer player, Contract contract, IList<Card> currentTrickCards)
        {
            // Prepare played cards and allowed cards
            var currentPlayerCards = this.playerCards[(int)this.game[player]];
            var allowedCards = new CardsCollection(currentPlayerCards.GetAllowedCards(contract, currentTrickCards));

            // Play card
            var playAction = player.PlayCard(allowedCards.ToList(), currentTrickCards.ToList());

            // Check for invalid card
            if (!allowedCards.Contains(playAction.Card))
            {
                throw new InvalidPlayerActionException(player, string.Format("Invalid card: {0}", playAction.Card));
            }

            // Save belote to team points
            playAction.Belote = false;
            if (playAction.AnnounceBeloteIfAvailable && currentPlayerCards.IsBeloteAllowed(contract, currentTrickCards, playAction.Card))
            {
                switch (this.game[player])
                {
                    case PlayerPosition.South:
                        this.southNorthBelotes++;
                        break;
                    case PlayerPosition.East:
                        this.eastWestBelotes++;
                        break;
                    case PlayerPosition.North:
                        this.southNorthBelotes++;
                        break;
                    case PlayerPosition.West:
                        this.eastWestBelotes++;
                        break;
                }

                playAction.Belote = true;
            }

            // Remove played card from the players cards
            this.playerCards[(int)this.game[player]].Remove(playAction.Card);
            return playAction;
        }