A class to model a single playing card.
Example #1
0
        /// <summary>
        /// Creates a deck.
        /// </summary>
        /// <returns> Returns a deck. </returns>
        /// <param name="shuffle"> Whether to shuffle the deck. </param>
        /// <param name="jokers"> Whether to add jokers. </param>
        public static List<Card> PopulateDeck(bool shuffle = false, bool jokers = false)
        {
            List<Card> deck = new List<Card>(); // Creates an instance of the deck
            // Goes through each suit
            for (int i = 0; i < 4; i++)
            {
                // Goes throguh each card for that suit
                for (int j = 0; j < 13; j++)
                {
                    Card c = new Card((Value)j + 1, (Suit)i + 1); // Create the card needed
                    deck.Add(c); // Add that card to the deck
                }
            }

            // If jokers are in the deck, add two of them
            if (jokers)
            {
                deck.Add(new Card(Value.Null, Suit.Null));
                deck.Add(new Card(Value.Null, Suit.Null));
            }

            // If the deck needs to be shuffled, shuffle it
            if (shuffle)
            {
                deck = Shuffle(deck);
            }

            return deck;
        }
Example #2
0
 /// <summary>
 /// Método Privado:
 /// Método responsável por criar naipe de cartas.
 /// </summary>
 private void createNaipe(string name)
 {
     for (int i = 1; i <= 13; i++)
     {
         Card newCard = new Card();
         newCard.index = i;
         newCard.naipe = name;
         _stack.Add(newCard);
     }
 }
Example #3
0
 /// <summary>
 /// Inserir carta no baralho de descarte.
 /// </summary>
 /// <param name="card">Carta que deve ser inserida no baralho de descarte.</param>
 /// <returns></returns>
 /// <example>
 /// Exemplo de uso:
 /// <code> myDeck.discard(myCard); </code>  
 /// </example>
 public void discard(Card card)
 {
     _discardStack.Add(card);
 }
Example #4
0
 /**
  * Checks to see if the collection contains a particular card, by
  * calling the ArrayList.Contains ()
  */
 public bool Contains(Card card)
 {
     return InnerList.Contains(card);
 }
Example #5
0
 public void Remove(Card card)
 {
     List.Remove(card);
 }
Example #6
0
 /// <summary>
 /// Compares whether the current object is less than a passed card.
 /// </summary>
 /// <param name="c"> The card to be compared against. </param>
 /// <returns> Whether the current object is less than the passed card. </returns>
 public bool LessThan(Card c)
 {
     return (this.GreaterThan(c) || this.Equals(c)) ? false : true;
 }
Example #7
0
 public void Add(Card card)
 {
     List.Add(card);
 }
Example #8
0
        /// <summary>
        /// Compares whether the current object is greater than a passed card.
        /// </summary>
        /// <param name="c"> The card to be compared against. </param>
        /// <returns> Whether the current object is greater than the passed card. </returns>
        public bool GreaterThan(Card c)
        {
            // Return that this card is lower if they are equal
            if (this == c)
            {
                return false;
            }

            // If they have different suits
            if (this.SuitVal != c.SuitVal)
            {
                // If this cards suit is greater, return true
                if (this.SuitVal > c.SuitVal)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // If the suit values are the same and ace is high
            if (Settings.AceHigh)
            {
                // If this card is an ace and card c is not
                if (this.Value == Value.Ace && c.Value != Value.Ace)
                {
                    return true;
                }

                // If card c is an ace and this is not
                if (this.Value != Value.Ace && c.Value == Value.Ace)
                {
                    return false;
                }

                // If they are both aces, return false that its lower
                if (this.Value == Value.Ace && c.Value == Value.Ace)
                {
                    return false;
                }
            }

            // Which has the better number value is returned
            if ((int)this.Value > (int)c.Value)
            {
                return true;
            }
            else if ((int)this.Value < (int)c.Value)
            {
                return false;
            }
            else
            {
                return false;
            }
        }
Example #9
0
        public void EqualsTest()
        {
            // arrange
            Card c = new Card(1, 1);
            Card cCopy = c;
            Card d = new Card(1, 2);

            bool expectedOne = true;    // This is for 1, 3 and 6
            bool expectedTwo = false;   // This is for 2, 4 and 5

            // act
            bool actualOne = c.Equals(cCopy);
            bool actualTwo = c.Equals(d);

            bool actualThree = c == cCopy;
            bool actualFour = c == d;

            bool actualFive = c != cCopy;
            bool actualSix = c != d;

            // assert
            Assert.AreEqual(expectedOne, actualOne);
            Assert.AreEqual(expectedTwo, actualTwo);

            Assert.AreEqual(expectedOne, actualThree);
            Assert.AreEqual(expectedTwo, actualFour);

            Assert.AreEqual(expectedTwo, actualFive);
            Assert.AreEqual(expectedOne, actualSix);
        }
Example #10
0
        public void DealingTest()
        {
            // arrange
            Whist.Whist game = new Whist.Whist();
            for (int i = 0; i < 5; i++)
            {
                game.AddPlayer(new ConsolePlayer());
            }

            var expected = new Card(Value.Nine, Suit.Spades);  // The last Players Last Card

            // act
            game.Deal(false, 7); // The deck is not shuffled so we can track where the cards go

            // assert
            Assert.AreEqual(expected, game.Players[4].Hand[6]);
        }
Example #11
0
        public void CreateDeckTest()
        {
            // arrange
            List<Card> deck = new List<Card>();

            // V = Value, S = Suit
            var expectedSH = Suit.Hearts;    // H = Hearts
            var expectedVH = Value.King;
            var expectedSS = Suit.Spades;    // S = Spades
            var expectedVS = Value.King;
            var expectedSD = Suit.Diamonds;  // D = Diamonds
            var expectedVD = Value.King;
            var expectedSC = Suit.Clubs;     // C = Clubs
            var expectedVC = Value.King;

            var expectedJK = new Card(Value.Null, Suit.Null);

            // act
            deck = CardFactory.PopulateDeck();
            var actualSH = deck[51].Suit;  // H = Hearts
            var actualVH = deck[51].Value;
            var actualSS = deck[38].Suit;  // S = Spades
            var actualVS = deck[38].Value;
            var actualSD = deck[25].Suit;  // D = Diamonds
            var actualVD = deck[25].Value;
            var actualSC = deck[12].Suit;  // C = Clubs
            var actualVC = deck[12].Value;

            // assert
            Assert.AreEqual(expectedSH, actualSH);
            Assert.AreEqual(expectedVH, actualVH);

            Assert.AreEqual(expectedSS, actualSS);
            Assert.AreEqual(expectedVS, actualVS);

            Assert.AreEqual(expectedSD, actualSD);
            Assert.AreEqual(expectedVD, actualVD);

            Assert.AreEqual(expectedSC, actualSC);
            Assert.AreEqual(expectedVC, actualVC);

            // act
            deck = CardFactory.PopulateDeck(false, true); // Re populates, with jokers
            actualSH = deck[51].Suit;  // H = Hearts
            actualVH = deck[51].Value;
            actualSS = deck[38].Suit;  // S = Spades
            actualVS = deck[38].Value;
            actualSD = deck[25].Suit;  // D = Diamonds
            actualVD = deck[25].Value;
            actualSC = deck[12].Suit;  // C = Clubs
            actualVC = deck[12].Value;
            var actualJK1 = deck[52];
            var actualJK2 = deck[53];

            // assert
            Assert.AreEqual(expectedSH, actualSH);
            Assert.AreEqual(expectedVH, actualVH);

            Assert.AreEqual(expectedSS, actualSS);
            Assert.AreEqual(expectedVS, actualVS);

            Assert.AreEqual(expectedSD, actualSD);
            Assert.AreEqual(expectedVD, actualVD);

            Assert.AreEqual(expectedSC, actualSC);
            Assert.AreEqual(expectedVC, actualVC);

            Assert.AreEqual(expectedJK, actualJK1);
            Assert.AreEqual(expectedJK, actualJK2);
        }
Example #12
0
        public void CreateCardTest()
        {
            // arrange
            int val = 1;
            int suit = 1;

            var expectedS = Suit.Clubs;
            var expectedV = Value.Ace;

            // act
            Card c = new Card((Value)val, (Suit)suit);
            Card d = new Card(val, suit);
            var actualS1 = c.Suit;
            var actualV1 = c.Value;
            var actualS2 = d.Suit;
            var actualV2 = d.Value;

            // assert
            Assert.AreEqual(expectedS, actualS1);
            Assert.AreEqual(expectedV, actualV1);
            Assert.AreEqual(expectedS, actualS2);
            Assert.AreEqual(expectedV, actualV2);
        }