0

/Hi I was wondering what I need to do to get this for loop to cycle through my enums, as it doesn't take [].I am attempting to create all the cards in a deck of cards./

using System;
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public class Deck
    {

        Random rNumber = new Random();

        List<Card> Cards;
        List<Card> ShuffledDeck;

        public Deck ()
        {
            Cards = NewDeck();
        }

        public void Shuffle()
        {

            for (int i = 0; i <= 51; ++i) 
            {

                int c = rNumber.Next (1, 53);

                ShuffledDeck.Add(Cards[c]);

            }
        }

/This is the main problem area, I think I have made a mistake in my constructer as well but that is probably an unrelated issue/

        private List<Card> NewDeck(Suit CardSuit, FaceValue CardValue, int iValue)
        {
            for (int i = 0; i <= 3; i++) 
            {
                for (int j = 0; j <= 12; j++) 
                {
                    Card newCard = new Card(CardSuit[i], CardValue[j], iValue[j]);
                }
            }
        }

        public void Print()
        {
            Console.WriteLine(ShuffledDeck[1].CardValue);
            Console.ReadLine();
        }

    }
}

/*This is the other class containing the enums*/

using System;
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{

    public enum Suit {Spades, Hearts, Clubs, Diamonds}
    public enum FaceValue {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}

    public class Card
    {
        public Suit CardSuit;
        public FaceValue CardValue;
        public int iValue;


        public Card (Suit cardSuit, FaceValue cardValue, int ivalue)
        {
            CardSuit = cardSuit;
            CardValue = cardValue;
            iValue =ivalue;
        }

        public Card ()
        {

        }
    }
}
Jonathan Dunn
  • 289
  • 2
  • 15
  • It would be easier for you to store each card in a readonly array and predefined them yourself as these cards are constant and will not change. and thus avoiding the loop at all .... – Mortalus Mar 17 '13 at 03:32

2 Answers2

0

Use a cross-join:

    public enum Suit {Spades, Hearts, Clubs, Diamonds}
    public enum FaceValue { Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King }

    public static void Main()
    {

        var cards =
            (from suit in Enum.GetValues(typeof(Suit)).Cast<Suit>()
             from face in Enum.GetValues(typeof(FaceValue)).Cast<FaceValue>()
             select new { Suit = suit, Face = face })

             .ToList();

        foreach (var card in cards)
            Console.WriteLine("{0} {1}", card.Face, card.Suit);

        Console.ReadLine();

    }

then in the foreach-loop provided, just read card.Face or card.Suit. Should give you all the cards with all of the suits.

sircodesalot
  • 11,231
  • 8
  • 50
  • 83
0

You cannot use an index operator to get an enum value. see this related post: Can you loop through all enum values?

plus you have some errors in your cunstroctur,you do not seem to return any lists but you declare that you do. Same goes for the "New Deck" method.

I would suggest the following:

private List<Card> NewDeck(Suit CardSuit, FaceValue CardValue, int iValue)
{
   for (int i = 0; i <= 3; i++) 
   {
       for (int j = 0; j <= 12; j++) 
       {
           Card newCard = new Card(AllSuit [i], AllFaces[j], iValue[j]);
       }
   }
}

 var AllSuit = new Suit[]
    {
      Suit.Spades,
      Suit.Hearts,
      Suit.Clubs,
      Suit.Diamonds
    };

    //Do the same for AllFaces...
Community
  • 1
  • 1
Mortalus
  • 10,574
  • 11
  • 67
  • 117
  • Thanks for the help, but now when I compile I get this error.. "The contextual keyword 'var' may only appear within a local variable declaration". – Jonathan Dunn Mar 17 '13 at 12:29
  • What was the exact code that you have used ? .. try to explicitly declare the array of suits instead of using the var keyword... – Mortalus Mar 17 '13 at 13:10
  • Ok I moved the var into my NewDeck method, as it was inside of the class so that is working, but the problems with my constructor persist, I can't seem to get it to return a List, could you point me in the right direction? – Jonathan Dunn Mar 17 '13 at 13:19
  • Thanks for your help, I will start a new post seeing as this is a different problem. – Jonathan Dunn Mar 17 '13 at 14:02