Ejemplo n.º 1
0
        /// <summary>
        /// Makes the move for a computer player (should only be called by a timer)
        /// </summary>
        private void makeComputerMove(bool computerOnlyOverride)
        {
            if (game.NumberOfPlayingPlayers == 1)
            {
                // Close the game view
                gameView.Close();
                return;
            }
            // Stop if for some reason this method got called when it's actually a human
            if (game.CurrentPlayer.Type == Player.PlayerType.Human && !computerOnlyOverride)
            {
                return;
            }

            // Set a flag to check if it should be smart (easier than referencing the type all the time)
            bool smart = game.CurrentPlayer.Type == Player.PlayerType.SmartComputer;

            // Make cards easier to access
            List <Card> cards = game.CurrentGamePlayer.Cards;



            // Store cards that can be played in a list
            List <Card> playableCards = new List <Card>(Game.MAXUNOCARDS);


            if (smart)
            {
                // Look for cards the same color
                foreach (Card c in cards)
                {
                    if (CanPlayCard(c) && game.CurrentColor == c.Color)
                    {
                        playableCards.Add(c);
                    }
                }

                // If no cards of the same color were found, look for any with the same face value
                if (playableCards.Count <= 0)
                {
                    foreach (Card c in cards)
                    {
                        if (CanPlayCard(c) && game.CurrentFace == c.Face)
                        {
                            playableCards.Add(c);
                        }
                    }
                }

                // If still no cards are found, look for any wilds to play
                if (playableCards.Count <= 0)
                {
                    foreach (Card c in cards)
                    {
                        if (CanPlayCard(c) && c.Color == Card.CardColor.Wild)
                        {
                            playableCards.Add(c);
                        }
                    }
                }
            }
            else
            {
                // Look for any cards that can be played
                foreach (Card c in cards)
                {
                    if (CanPlayCard(c))
                    {
                        playableCards.Add(c);
                    }
                }
            }



            // Choose a card to play
            if (playableCards.Count > 0)
            {
                Random random = new Random();
                Card   selectedCard;

                if (smart)
                {
                    // Smart player should choose the highest-value card (as its good to get rid of more points if using Uno scoring)
                    sortCards(playableCards);
                    selectedCard = playableCards.Last();
                }
                else
                {
                    // Choose a card randomly to play
                    selectedCard = playableCards[random.Next(0, playableCards.Count)];
                }


                // If the card's a wild, randomly choose a color
                if (selectedCard.Color == Card.CardColor.Wild)
                {
                    if (smart)
                    {
                        List <Card.CardColor>            colorsToChoose = new List <Card.CardColor>();
                        Dictionary <Card.CardColor, int> colorCounts    = new Dictionary <Card.CardColor, int>();
                        Card.CardColor greatestColor;

                        // Reset the color counts
                        for (int i = 0; i < Card.NUMBEROFCOLORS - 1; i++)
                        {
                            colorCounts.Add((Card.CardColor)i, 0);
                        }

                        // Add 1 to the count of the color for each card
                        foreach (Card c in cards)
                        {
                            if (c.Color != Card.CardColor.Wild)
                            {
                                colorCounts[c.Color]++;
                            }
                        }

                        // Set the greatest color to the first one
                        greatestColor = (Card.CardColor) 0;

                        // Look for the greatest color
                        for (int i = 1; i < (Card.NUMBEROFCOLORS - 1); i++)
                        {
                            if (colorCounts[greatestColor] < colorCounts[(Card.CardColor)i])
                            {
                                greatestColor = (Card.CardColor)i;
                            }
                        }

                        // If more than one color has the highest number of cards, choose it
                        for (int i = 0; i < (Card.NUMBEROFCOLORS - 1); i++)
                        {
                            if (colorCounts[(Card.CardColor)i] == colorCounts[greatestColor])
                            {
                                colorsToChoose.Add((Card.CardColor)i);
                            }
                        }

                        // Randomly choose an appropriate color
                        game.WildColor = colorsToChoose[random.Next(0, colorsToChoose.Count)];
                    }
                    else
                    {
                        // Randomly choose a color
                        game.WildColor = Card.IntToCardColor(random.Next(0, 4));
                    }
                }

                // Play the card
                SelectCard(selectedCard);
            }
            else
            {
                // Pickup a card if there's nothing else to play
                PickupCard(true);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Choose a card for the current player to play
        /// </summary>
        public CardPlayStatus PlaySelectedCard(Card card)
        {
            // Check if the card  is allowed to be played
            CardPlayStatus status = CanPlayCardStatus(card);

            // Ask for the color for a wild card
            if (card.Color == Card.CardColor.Wild)
            {
                if (game.CurrentPlayer.Type == Player.PlayerType.Human)
                {
                    // Check if the card can be played before asking for wild colour
                    CardPlayStatus wildCheckStatus = CanPlayCardStatus(card);
                    if (wildCheckStatus != CardPlayStatus.Success)
                    {
                        return(wildCheckStatus);
                    }

                    // Show the color chooser dialog form
                    WildColorChooser wildColorChooser = new WildColorChooser();
                    wildColorChooser.ShowDialog();

                    // Check if a colour was chosen, or if the action was cancelled
                    if (wildColorChooser.DialogResult == DialogResult.OK)
                    {
                        // Remember the chosen wild color
                        game.WildColor = wildColorChooser.Color;
                    }
                    else
                    {
                        // Return that the user cancelled playing the card
                        return(CardPlayStatus.Cancelled);
                    }
                }
                else
                {
                    Random random = new Random();
                    game.WildColor = Card.IntToCardColor(random.Next(0, 3));
                }
            }

            // Check that the card is allowed
            if (status == CardPlayStatus.Success)
            {
                // Move it to the discard pile
                game.DiscardPile.Add(card);
                game.CurrentGamePlayer.Cards.Remove(card);

                // Perform action on action cards
                performAction(card);

                // Add to number of cards played statistic
                game.CurrentGamePlayer.NumberOfCardsPlayed++;

                // If the player is now finished, give them a rank
                if (game.CurrentGamePlayer.Finished)
                {
                    game.CurrentGamePlayer.FinishRank = game.NumberOfFinishedPlayers - 1;
                }

                if (!game.Options.EnableTeams && game.NumberOfFinishedPlayers == game.NumberOfPlayers - 1)
                {
                    // Show the final results
                    Program.NewSortedPlayersView(game);

                    // Setting this bool to true to end the game without dialog box
                    gameView.closeGameWithoutDialog = true;

                    // Close the game view
                    gameView.Close();
                    return(status);
                }
                else if (game.Options.EnableTeams && game.NumberOfFinishedPlayers > 0)
                {
                    int count = 0;
                    foreach (DictionaryEntry curgamePlayer in game.PlayersCards)
                    {
                        Player gamePlayer    = (Player)curgamePlayer.Key;
                        Player currentPlayer = game.CurrentPlayer;
                        if (gamePlayer.Name == currentPlayer.Name)
                        {
                            game.WinningPlayer = count;
                        }
                        count++;
                    }

                    Program.NewSortedPlayersView(game);

                    gameView.closeGameWithoutDialog = true;

                    gameView.Close();
                    return(status);
                }
                else
                {
                    // Setup next player, and update the game view
                    nextPlayer();
                    gameView.ReDraw();
                }
            }

            return(status);
        }