-2

I am creating a blackjack game and so far I have made a card class, I started to make a deck class but I am not sure on how to set up the cards with values taken from the arrays. Also I have to use the arrays in my deck class to do this.

Any help would be appreciated

Here is what I have so far for my deck class

class Deck
{
    private const Int32 MAXCARDS = 52;
    private Card[] _cards = new Card[MAXCARDS];
    Card.SUIT[] suits = { Card.SUIT.SPADES, Card.SUIT.HEARTS, Card.SUIT.DIAMONDS, Card.SUIT.CLUBS };
    String[] values = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

    public Card()
    {

    }
    public Card GetCard(Int32 index)
    {

    }

    public Card SetCard(Int32 index, Card Card)
    {

    }
}

}

darren689
  • 3
  • 2
  • [this answer](http://stackoverflow.com/a/26532939/1070452) has the framework for a Card and Deck classes including how to shuffle. – Ňɏssa Pøngjǣrdenlarp Nov 29 '14 at 19:01
  • Looks like some homework? I can offer a hint based on where I think you might be stuck. While there are indeed 52 cards, each card has to be represented by both a value and a suit at the same time. I would assume such a relationship to reside within the Card class. Why would the deck class care what the suit and/or value of a card is? It just knows there are N number of cards in the deck and doesn't have to know what the cards are. My hint here is that suits and values might not being to the Deck class. – Trevor Ash Nov 29 '14 at 19:34

1 Answers1

0

In order not to spoil the whole action, concentrate on the points where you struggle and try to code further without help :)

 class Card
 {
     public enum SUIT { SPADES, HEARTS, DIAMONDS, CLUBS };

     public string Value {get; set;}
     public SUIT Suit { get; set; }

     public Card(SUIT suit, string value)
     {
         this.Suit = suit;
         this.Value = value;
     }
 }
 class Deck
 {
     private const Int32 MAXCARDS = 52;
     private Card[] _cards = new Card[MAXCARDS];

     public Deck()
     {
         Card.SUIT[] suits = { Card.SUIT.SPADES, Card.SUIT.HEARTS, Card.SUIT.DIAMONDS, Card.SUIT.CLUBS };
         String[] values = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
         Random rnd = new Random();
         Card[] orderedCards = new Card[suits.Length * values.Length];
         int newIndex = 0;

         // Generate an array with all possible combinations of suits and values
         for (int suitsIndex = 0; suitsIndex < suits.Length; suitsIndex++)
         {
             for (int valuesIndex = 0; valuesIndex < values.Length; valuesIndex++)
             {
                 newIndex = values.Length * suitsIndex + valuesIndex; // Think about it :)
                 orderedCards[newIndex] = new Card(suits[suitsIndex], values[valuesIndex]);
             }
         }

         // Generate an array with random numbers from 0 51 (taken from http://stackoverflow.com/questions/5864921/how-can-i-randomize-numbers-in-an-array)
         int[] randomNumbers = Enumerable.Range(0, this._cards.Length).OrderBy(r => rnd.Next()).ToArray();

         // Now go from 0 to 51 and set a card to a random element of the orderedCards array
         for (int i = 0; i < _cards.Length; i++)
         {
             this._cards[i] = orderedCards[randomNumbers[i]];
             Console.WriteLine("Card " + i + " = " + _cards[i].Suit.ToString() + " - " + _cards[i].Value.ToString());
         }
     }
 }
Stefan Woehrer
  • 680
  • 4
  • 12