/** 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)); }
/** Adds the newCard to this Hand. */ public virtual int AddCard(Card newCard) { if (newCard == null) { Debug.WriteLine ("Error: Tried to add a null card to a CardPile."); return -1; } cards.Add (new Card(newCard)); //< This uses the Card class's copy constructor. return 0; }
/** Adds a packs's worth of cards to the stock. */ public void AddPack() { if (cards == null) cards = new List<Card> (); /** The order of the pips are A,2,3,4,5,6,7,8,9,10,J,Q,K, and don't want any Jokers. We need to start at 1, and loop through 13 times. */ for (int suit = 1; suit <= 4; suit++) //< { for (int pipValue = 1; pipValue <= Card.King; pipValue++) // Once for each of the 13 pip values { /** This is called a nested for loop. It makes it so that our cards will be sorted by suit and value. Note: If we had switched the order of the nested for loops, it would sort the cards by value then suit. */ var newCard = new Card (pipValue, suit, faceValues[pipValue], suitValues[suit - 1], suitType); AddCard (newCard); } } if (hasJokers) { cards.Add (new Card (Card.Joker, Card.Red)); cards.Add (new Card (Card.Joker, Card.Black)); } }
/** Adds the newCard to this Hand. */ public virtual int InsertCard(Card newCard, int index) { if (newCard == null) return -1; if (index < 0 || index >= NumCards) //< Index out of bounds! return - 2; cards.Insert (index, newCard); return 0; }
/** Checks to see if thisCard is in the pile and returns true if it is. */ public bool Contains(Card thisCard) { if (thisCard == null) return false; foreach (Card card in Cards) if (Equals (card)) return true; return false; }
/** Runs the unit test. */ public override void RunTests() { Debug.Write ("\nTesting Card Class\n" + CardGame.ConsoleLine ('*') + "Creating test Card object...\n"); Card cardA, cardB; cardA = new Card (); Debug.Assert (cardA.ToString () == "Black Joker", "Error: Default constructor should produce a black" + "joker.\n" + cardA.ToString ()); cardA = new Card (Card.Joker, Card.Hearts); Debug.Assert (cardA.ToString () == "Red Joker", "Error: new Card (Card.Joker, Card.Hearts) " + "should produce a black joker.\n" + cardA.ToString ()); cardA = new Card (Card.Joker, Card.Spades); Debug.Assert (cardA.ToString () == "Black Joker", "Error: new Card (Card.Joker, Card.Spades) " + "should produce a black joker.\n" + cardA.ToString ()); cardA = new Card (Card.Ace, Card.Spades); Debug.Assert (cardA.ToString () == "Ace of Spades", "Error: new Card (Card.Ace, Card.Spades) " + "should produce a black joker.:\n" + cardA.ToString ()); cardA = new Card (5, Card.Spades); Debug.Assert (cardA.ToString () == "5 of Spades", "Error: new Card (5, Card.Spades) " + "should produce a black joker.:\n" + cardA.ToString ()); Debug.Write ("Testing Card.Compare (Card) function...\n" + "Creating some test Card object to compare. \n"); Debug.Write ("Creating some Cards with invalid input and testing Equals (int, int) function.\n"); cardA = new Card (-4, 6); Debug.Assert (cardA.ToString () == "Black Joker", "Error: new Card (-4, 6) didn't create a Black " + "Joker, but created a " + cardA.ToString ()); cardA = new Card (15, -1); Debug.Assert (cardA.ToString () == "King of Clubs", "Error: new Card (15, -1) didn't create a King " + "of Clubs, but created a " + cardA.ToString ()); Debug.Write ("Testing bool Equals (Card other) funciton\n"); cardA = new Card (5, Card.Spades); Debug.Assert (cardA.Equals (5, Card.Spades), "Error: !cardA.Equals(5, Card.Spades) :\ncardA: " + cardA.ToString ()); cardB = new Card (5, Card.Spades); Debug.Assert (cardA.Equals(cardB), "Error: !cardA.Equals(cardB) :\ncardA: " + cardA.ToString () + "\ncardB: " + cardB.ToString ()); Debug.Write ("Done testing the Card class.\n\n"); }
/** Operator compares thisCard to the otherCard and returns true if they are the same. */ public bool Equals(Card other) { //< Always check to see if an object is null before trying to access any of it's data members!!! if ((Object)other == null) return false; Debug.WriteLine ("other.ToString (): " + other.ToString () + "\nother.PipValue: " + other.pipValue + " other.Suit: " + other.suit); return Equals (other.PipValue, other.Suit); }
/** Copy Constuctor Quickly copies the state of the other object. */ public Card(Card other) { if (other == null) { suit = Black; suitValue = Black; pipValue = Joker; faceValue = 0; suitType = French; return; } suit = other.suit; suitValue = other.suitValue; pipValue = other.pipValue; faceValue = other.faceValue; suitType = other.suitType; }