コード例 #1
0
ファイル: Deck.cs プロジェクト: gabriellegawa/Durak
 /// <summary>
 /// Parameterized Constructor - that will initialize Deck object with card
 /// </summary>
 /// <param name="deckSize">deck size</param>
 public Deck(int deckSize)
 {
     // declare new cards object
     cards = new Cards();
     if (deckSize == 36) // check if the deck size == 36
     {
         MinRange = 5;
         for (int suitVal = 0; suitVal < 4; suitVal++)
         {
             for (int rankVal = MinRange; rankVal < 14; rankVal++)
             {
                 cards.Add(new Card((Suit)suitVal, (Rank)rankVal));
             }
         }
     }
     else if (deckSize == 20) // check if the deck size == 20
     {
         MinRange = 9;
         for (int suitVal = 0; suitVal < 4; suitVal++)
         {
             for (int rankVal = MinRange; rankVal < 14; rankVal++)
             {
                 cards.Add(new Card((Suit)suitVal, (Rank)rankVal));
             }
         }
     }
     else if (deckSize == 52) // check if the deck size == 52
     {
         for (int suitVal = 0; suitVal < 4; suitVal++)
         {
             for (int rankVal = MinRange; rankVal < 14; rankVal++)
             {
                 cards.Add(new Card((Suit)suitVal, (Rank)rankVal));
             }
         }
     }
 }
コード例 #2
0
ファイル: Deck.cs プロジェクト: gabriellegawa/Durak
        /// <summary>
        /// Shuffle - randomize the sequence of the deck
        /// </summary>
        public void Shuffle(int deckSize)
        {
            Cards newDeck = new Cards();

            bool[] assigned  = new bool[deckSize];
            Random sourceGen = new Random();

            for (int i = 0; i < deckSize; i++)
            {
                int  sourceCard = 0;
                bool foundCard  = false;
                while (foundCard == false)
                {
                    sourceCard = sourceGen.Next(deckSize);
                    if (assigned[sourceCard] == false)
                    {
                        foundCard = true;
                    }
                }
                assigned[sourceCard] = true;
                newDeck.Add(cards[sourceCard]);
            }
            newDeck.CopyTo(cards);
        }
コード例 #3
0
        /// <summary>
        /// PlayGame
        /// </summary>
        /// <returns>int</returns>
        public int PlayGame()
        {
            // Only play if players exist.
            if (players == null)
            {
                return(-1);
            }
            // Deal initial hands.
            DealHands();

            // Initialize game vars, including an initial card to place on the
            // table: playCard.
            bool GameWon = false;
            int  currentPlayer;
            Card playCard = playDeck.GetCard(currentCard++);

            discardedCards.Add(playCard);

            // Main game loop, continues until GameWon == true.
            do
            {
                // Loop through players in each game round.
                for (currentPlayer = 0; currentPlayer < players.Length;
                     currentPlayer++)
                {
                    // Write out current player, player hand, and the card on the
                    // table.
                    Console.WriteLine("{0}'s turn.", players[currentPlayer].Name);
                    Console.WriteLine("Current hand:");
                    foreach (Card card in players[currentPlayer].PlayHand)
                    {
                        Console.WriteLine(card);
                    }
                    Console.WriteLine("Card in play: {0}", playCard);
                    // Prompt player to pick up card on table or draw a new one.
                    bool inputOK = false;
                    do
                    {
                        Console.WriteLine("Press T to take card in play or D to " +
                                          "draw:");
                        string input = Console.ReadLine();
                        if (input.ToLower() == "t")
                        {
                            // Add card from table to player hand.
                            Console.WriteLine("Drawn: {0}", playCard);

                            // Remove from discarded cards if possible (if deck
                            // is reshuffled it won't be there any more)
                            if (discardedCards.Contains(playCard))
                            {
                                discardedCards.Remove(playCard);
                            }

                            players[currentPlayer].PlayHand.Add(playCard);
                            inputOK = true;
                        }
                        if (input.ToLower() == "d")
                        {
                            // Add new card from deck to player hand.
                            Card newCard;
                            // Only add card if it isn't already in a player hand
                            // or in the discard pile
                            bool cardIsAvailable;
                            do
                            {
                                newCard = playDeck.GetCard(currentCard++);
                                // Check if card is in discard pile
                                cardIsAvailable = !discardedCards.Contains(newCard);
                                if (cardIsAvailable)
                                {
                                    // Loop through all player hands to see if newCard
                                    // is already in a hand.
                                    foreach (Player testPlayer in players)
                                    {
                                        if (testPlayer.PlayHand.Contains(newCard))
                                        {
                                            cardIsAvailable = false;
                                            break;
                                        }
                                    }
                                }
                            } while (!cardIsAvailable);
                            // Add the card found to player hand.
                            Console.WriteLine("Drawn: {0}", newCard);
                            players[currentPlayer].PlayHand.Add(newCard);
                            inputOK = true;
                        }
                    } while (inputOK == false);

                    // Display new hand with cards numbered.
                    Console.WriteLine("New hand:");
                    for (int i = 0; i < players[currentPlayer].PlayHand.Count; i++)
                    {
                        Console.WriteLine("{0}: {1}", i + 1,
                                          players[currentPlayer].PlayHand[i]);
                    }
                    // Prompt player for a card to discard.
                    inputOK = false;
                    int choice = -1;
                    do
                    {
                        Console.WriteLine("Choose card to discard:");
                        string input = Console.ReadLine();
                        try
                        {
                            // Attempt to convert input into a valid card number.
                            choice = Convert.ToInt32(input);
                            if ((choice > 0) && (choice <= 8))
                            {
                                inputOK = true;
                            }
                        }
                        catch
                        {
                            // Ignore failed conversions, just continue prompting.
                        }
                    } while (inputOK == false);

                    // Place reference to removed card in playCard (place the card
                    // on the table), then remove card from player hand and add
                    // to discarded card pile.
                    playCard = players[currentPlayer].PlayHand[choice - 1];
                    players[currentPlayer].PlayHand.RemoveAt(choice - 1);
                    discardedCards.Add(playCard);
                    Console.WriteLine("Discarding: {0}", playCard);

                    // Space out text for players
                    Console.WriteLine();

                    // Check to see if player has won the game, and exit the player
                    // loop if so.
                    GameWon = players[currentPlayer].HasWon();
                    if (GameWon == true)
                    {
                        break;
                    }
                }
            } while (GameWon == false);

            // End game, noting the winning player.
            return(currentPlayer);
        }