0

/嗨我想知道我需要做什么才能让这个for循环循环遍历我的枚举,因为它不需要[]。我正在尝试在一副卡片中创建所有卡片。/

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]);

            }
        }

/这是主要问题区域,我认为我的构造函数也犯了一个错误,但这可能是一个不相关的问题/

        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 ()
        {

        }
    }
}
4

2 回答 2

0

使用交叉连接:

    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();

    }

然后在提供的 foreach 循环中,只需读取 card.Face 或 card.Suit。应该给你所有花色的所有牌。

于 2013-03-17T03:42:49.473 回答
0

您不能使用索引运算符来获取枚举值。看到这个相关的帖子:你能遍历所有的枚举值吗?

再加上你的 cunstroctur 有一些错误,你似乎没有返回任何列表,但你声明你这样做了。“新甲板”方法也是如此。

我建议如下:

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...
于 2013-03-17T03:43:46.450 回答