Example #1
0
 /** Copy constructor. */
 public CardPile(CardPile other)
 {
     int newNumCards = other.NumCards;
     cards = new List<Card> (newNumCards);
     for (int i = 0; i < newNumCards; i++)
         cards[i] = new Card (other.GetCard (i));
 }
Example #2
0
        /** Draws a CardPile with the spcified number of cards.
            @return Return null if there are not enough cards. */
        public CardPile DrawCards(int numCards)
        {
            if (numCards > NumCards)
                return null;

            CardPile newPile = new CardPile ();

            // We have to search the whole cards with a for loop.
            for (int i = 0; i < numCards; i++)
                newPile.AddCard (DrawCard ());

            return newPile;
        }
Example #3
0
        /** Runs the unit tests. */
        public override void RunTests()
        {
            Debug.Write ("\nTesting CardPile Class\n" + CardGame.ConsoleLine ('*') + "\n" +
                "Creating test CardPile...\n\n");

            var stock = new CardPile ();

            for (int i = 0; i < Deck.PackSize; i++)
                stock.AddCard (Card.RandomCard ());

            Debug.Assert (stock.NumCards == 52, "Error: There where not 52 cards!\n" + stock.ToString());

            Debug.Write (stock.ToString () + "\n\nShuffling CardPile...\n");

            stock.Shuffle ();

            Debug.WriteLine ("\n\n" + stock.ToString () +
                "\n\nTesting DrawCard () by drawing 42 card...\n");

            for (int i = 0; i < 42; i++)
                Debug.Write (stock.DrawCard ().ToString () + ", ");

            Debug.WriteLine ("\n\nPrinting the last 10 cards...\n\n" + stock.ToString () +
                "\n\nTesting DrawRandomCard () by drawing 5 stock...\n");

            for (int i = 0; i < 5; i++)
                Debug.Write (stock.DrawRandomCard ().ToString () + ", ");

            int numLoops = 10,
                numExpectedNullstock = numLoops - stock.NumCards,
                counter = 0;

            Debug.WriteLine ("\n\nPrinting last " + numExpectedNullstock + " stock...\n\n" + stock.ToString () +
                "\n\nDrawing 10 more random stock and expecting 5 null stock...\n");

            String drawnstock = "";

            for (int i = 1; i <= numLoops; i++)
            {
                var tempCard = stock.DrawRandomCard ();

                if (tempCard != null)
                {
                    Debug.WriteLine (i + ", ");
                    drawnstock += tempCard.ToString () + ", ";
                }
                else
                    counter++;
            }
            Debug.Assert (counter == numExpectedNullstock, "Error: There were supposed to be " + numExpectedNullstock +
                " null stock and got " + counter + "!\nDraw stock:\n" + drawnstock + "\n" + stock.ToString ());

            Debug.Assert (stock.IsEmpty, "Error: After trying to take more stock than the stock had, the stock was " +
                "not empty!");

            Debug.Write ("\n\nPrinting empty CardPile...\n\n" + stock.ToString () +
                "\n\nShuffling and printing stock...\n");

            stock.Shuffle();

            Debug.WriteLine (stock.ToString () + "\n\n");

            Debug.WriteLine ("Done testing the CardPile class.\n");
        }
Example #4
0
        /** Adds the newCard to this Hand. */
        public virtual int AddCards(CardPile newCards)
        {
            if (newCards == null)    //< We do not allow Jokers.
            {
                Debug.WriteLine ("Error: Attempted to add a null pile of cards to a CardPile!");
                return -1;
            }

            foreach (Card card in newCards.Cards)
                AddCard (newCards.DrawCard ());

            return 0;
        }