Here are the class diagrams for the Card and Deck objects:
Here is the C# code for the Card Object. It uses a short cut syntax that lets us not have to explicitly write the get and set methods.
Here is the Deck program. I have added all the methods in the diagram, but I haven't implemented all of them.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DeckOfCards { class Deck { private Listcards; public Deck() { //initialize the list in constructor cards = new List (); //initilize the deck with 52 cards InitializeDeck(); } private void InitializeDeck() { string suit = null; for (int i = 0; i < 4; i++) { //chose the suit switch (i) { case 0: suit = "hearts"; break; case 1: suit="diamonds"; break; case 2: suit="clubs"; break; default: suit="spades"; break; } //get the card numbers for each suit for (int j = 1; j < 14; j++) { Card c = new Card(); c.Suit=suit; c.Number=j; cards.Add(c); } } } public List GetDeck() { return cards; } public void Shuffle() { } public void AddCard(Card c) { cards.Add(c); } public void RemoveCard(Card c) { foreach (Card card in cards) { if (c.Suit.Equals(card.Suit) && c.Number == card.Number) { cards.Remove(c); } } } public Card GetCard(int index) { return cards[index]; } } }
Here is the code to get the deck and display all the cards
And here is a picture of it running
No comments:
Post a Comment