/// <summary> /// return the card at the param index and remove it from the deck /// </summary> /// <param name="cardIndex">The cards index</param> /// <returns>The Card at this index</returns> public PlayingCard DrawCard(int cardIndex = 0) { if (cardIndex >= 0 && cardIndex <= 51) { PlayingCard tempCard = cards[cardIndex]; cards.RemoveAt(cardIndex); return(cards[cardIndex]); // card number 0 to SIZE_OF_DECK } else { throw new IndexOutOfRangeException(string.Format(" * Card index must be between {0} and {1}", 0, SIZE_OF_DECK - 1)); } }
/// <summary> /// return the card at the param index and remove it from the deck /// </summary> /// <param name="cardIndex">The cards index</param> /// <returns>The Card at this index</returns> public PlayingCard DrawCard(int cardIndex = 0) { if (cardIndex >= 0 && cardIndex < cards.Count) { //cards[cardIndex].FaceUp = true; PlayingCard tempCard = cards[cardIndex]; cards.RemoveAt(cardIndex); return(tempCard); // card number 0 to SIZE_OF_STANDARD_DECK } else { throw new IndexOutOfRangeException(string.Format(" * Card index must be between {0} and {1}", 0, SIZE_OF_STANDARD_DECK - 1)); } }
public void Deal() { Started = DateTime.Now; for (LoopsCompleted = 0; LoopsCompleted < repeat; LoopsCompleted++) { deck.Shuffle(); PlayingCard card = deck.Deal(); if (card.Suit == CardSuit.Hearts) { count++; } deck.Add(card); } Ended = DateTime.Now; }
public void Shuffle() { PlayingCard[] cardArray = cardStack.ToArray(); cardStack.Clear(); for (int i = 0; i < cardArray.Length; i++) { int swapIndex = rnd.Next(0, cardArray.Length); PlayingCard temp = cardArray[swapIndex]; cardArray[swapIndex] = cardArray[i]; cardArray[i] = temp; } foreach (PlayingCard card in cardArray) { cardStack.Push(card); } }
/// <summary> /// Initializes the deck with sequence of cards, also sets some static properties /// and gets the random number generator ready /// </summary> private void Initialize() { if ((int)DeckFlags.Large == m_DeckSize) { m_SuitSize = 13; //incase forgot to add +4 to the flags controlling suit size } PlayingCard.baseRank = (Rank)Util.CalculateBaseRank(m_SuitSize); //can be either 1, 2, 6 or 10 PlayingCard newCard = new PlayingCard(Suit.Club, PlayingCard.baseRank, m_SuitSize); for (int i = 0; i <= m_DeckSize; i++) { PlayingCard nextCard = (PlayingCard)newCard.Clone(); Add(nextCard); newCard++; } }
/// <summary> /// Used to shuffle the deck of cards. Deletes the contents of a PlayingCards /// collection and recreates a new one with random cards /// </summary> public void Shuffle() { Clear(); m_Count = 0; // loop is used to create cards and put them into newDeck // on each iteration, the class variable m_Deck is updated with the contents // of the newDeck array, as shown in the textbook for (int Counter = 0; Counter <= m_DeckSize; Counter++) { PlayingCard pCard = null; do { uint floor = (uint)PlayingCard.baseRank; uint ceiling = (uint)Util.CalculateOffsetSuitSize(m_SuitSize); uint myRank = 0; uint mySuit = RangedRandom.GenerateUnsignedNumber(4, 0); if (PlayingCard.isAceHigh && (int)DeckFlags.Large != m_DeckSize) { ceiling++; floor--; myRank = RangedRandom.GenerateUnsignedNumber(floor, ceiling, (uint)m_Entropy) + 1; } else { myRank = RangedRandom.GenerateUnsignedNumber(ceiling, (uint)m_Entropy) + 1; } pCard = new PlayingCard((Suit)mySuit, (Rank)myRank, m_SuitSize, true); } while (IsCardAlreadyInDeck(pCard));// if an existing card with the same suit and rank is there, don't add it Add(pCard); m_Count++; } //unfortunately the shuffle loop has a limitation that, when given a deck with an abnormal size //and aces high, the loop cannot accomodate the gap in ranks, so just set the rank ceiling to 14 //and change numeric 14 cards to aces afterwards if (PlayingCard.isAceHigh && (int)DeckFlags.Large != m_DeckSize) { foreach (PlayingCard card in this) { if (14 == (uint)card.rank) { card.rank = Rank.Ace; } } } Turnover(); }
/// <summary> /// Looks for a sequence that makes a flush in a random deck, user for testing /// </summary> static void TryFlush() { Deck myDeck = new Deck(); myDeck.Shuffle(); int numberOfDraws = (int)Math.Round((double)(Deck.SIZE_OF_DECK / 5), 0); // Can draw 5 cards 10 times in a deck of 52 bool isFlush = false; for (int i = 0; i < numberOfDraws; i++) { try { int sameSuitCount = 0; PlayingCard tempCard = myDeck.GetCard(i * 5); CardSuit tempSuit = tempCard.Suit; for (int j = 1; j <= 4; j++) { if (myDeck.GetCard(i * 5 + j).Suit == tempSuit) // same suit as temp card { sameSuitCount++; } if (sameSuitCount == 4) // the other 4 cards are the same suit as the first of the 5 draws { isFlush = true; Console.WriteLine("*******************\n"); Console.WriteLine("Flush!!!"); for (int z = 0; z < 5; z++) // printing out the last 5 cards { Console.WriteLine(myDeck.GetCard(i * 5 + j - z).ToString()); } return; // exit out of the method } } } catch (IndexOutOfRangeException ioe) { Console.WriteLine(ioe.Message); } } if (isFlush == false) // if no flush in this shuffle, show this msg { Console.WriteLine("No Flush"); } }
/// <summary> /// Default constructor for a deck of cards. /// </summary> public Deck() { foreach (CardRank rank in (CardRank[])Enum.GetValues(typeof(CardRank))) // for each rank in the Rank enum { foreach (CardSuit suit in (CardSuit[])Enum.GetValues(typeof(CardSuit))) // for each suit in the Suit enum { cards.Add(new PlayingCard(suit, rank)); } } // move the aces in front of the kings, that is the order or ranks in Durak for (int cardIndex = 0; cardIndex < 4; cardIndex++) { PlayingCard cardToMove = cards[0]; cards.RemoveAt(0); cards.Add(cardToMove); } }
/// <summary> /// CompareTo - used to sort cards based on value comparison /// </summary> /// <param name="obj">Generic object to base comparison on</param> /// <returns></returns> public int CompareTo(object obj) { // Check if a comparison object exists if (obj == null) { throw new ArgumentNullException("Unable to compare a card with an absent object."); } PlayingCard compareCard = obj as PlayingCard; if (compareCard != null) { // Account for value as more import than rank to accomodate use of trumps in Durak and other card games using trumps int thisSort = this.myValue * 10 + (int)this.mySuit; int compareCardSort = compareCard.myValue * 10 + (int)compareCard.mySuit; return(thisSort.CompareTo(compareCardSort)); } else { throw new NotImplementedException(); } }
public CardDeck() { Array suitChoices = Enum.GetValues(typeof(CardSuit)); foreach (CardSuit s in suitChoices) { for (int r = PlayingCard.Ace; r <= PlayingCard.King; r++) { PlayingCard card; if (r < PlayingCard.Jack) { card = new PlayingCard(r, s); } else { card = new FaceCard(r, s); } card.FaceUp = true; cardStack.Push(card); } } }
public void Add(PlayingCard card) { card.FaceUp = true; cardStack.Push(card); }
/// <summary> /// PlayGame - The flow of the game, which cycles through each player, shows them a list of their current cards, and then prompts them to make their move /// </summary> /// <returns></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; PlayingCard 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++) { // Draw starting trump card PlayingCard initialCardDrawn = playDeck.GetCard(currentCard++); CardSuit trumpSuit = initialCardDrawn.Suit; Console.WriteLine("\nThe starting trump suit is {0}.\n", trumpSuit); // Write out current player, player hand, and the card on the // table. Console.WriteLine("\n{0}'s turn.", players[currentPlayer].Name); Console.WriteLine("********************"); Console.WriteLine("Current hand:"); foreach (PlayingCard card in players[currentPlayer].PlayHand) { Console.WriteLine(card); } Console.WriteLine("Initial card drawn from top of the deck: {0}", initialCardDrawn); // Prompt player to pick up card on table or draw a new one. bool inputOK = false; do { Console.WriteLine("Press T to throw card or S to skip"); string input = Console.ReadLine(); if (input.ToLower() == "t") { // Add card from table to player hand. Console.WriteLine("Thrown: {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.Remove(playCard); inputOK = true; } if (input.ToLower() == "s") { // Add new card from deck to player hand. PlayingCard 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); }
public void AddCardAtBottom(PlayingCard card) { // put new card at botton of deck cards.Insert(cards.Count, card); //System.Diagnostics.Debug.WriteLine(cards.Count); }
public override void Add(PlayingCard card) { drop.Add(card); }