C# 当试图展示洗牌牌组中的一手牌时,它会用梅花王牌填满每一手牌

C# 当试图展示洗牌牌组中的一手牌时,它会用梅花王牌填满每一手牌,c#,shuffle,C#,Shuffle,这是我第一次使用C。我必须从Java转换一个旧项目。旧项目可以运行,但当我试图转换它时,出现了问题?卡片类将默认卡片设置为黑桃王牌,但它显示的是梅花王牌。Ace和Club都是枚举的第一个列表,所以我猜它没有使用默认值 因为它显示的是同一张牌,我想我的洗牌方法可能有问题。但我现在不确定。 有一个卡片,甲板和手类。然后列举衣服和脸 更新:我相信错误就在下面的方法中?我需要弄清楚如何使用c#中的“n”。这是另一个班级需要的 在Java中,我将其作为: public Card(int n) {

这是我第一次使用C。我必须从Java转换一个旧项目。旧项目可以运行,但当我试图转换它时,出现了问题?卡片类将默认卡片设置为黑桃王牌,但它显示的是梅花王牌。Ace和Club都是枚举的第一个列表,所以我猜它没有使用默认值

因为它显示的是同一张牌,我想我的洗牌方法可能有问题。但我现在不确定。 有一个卡片,甲板和手类。然后列举衣服和脸

更新:我相信错误就在下面的方法中?我需要弄清楚如何使用c#中的“n”。这是另一个班级需要的

在Java中,我将其作为:

public Card(int n) 
{
    face = Face.values()[n % 13];
    suit = Suit.values()[n % 4];
} //end Card (int n) method
c#:

上面的代码在卡类中?我知道这并没有太大帮助,但这是将所有代码发布到这里的唯一其他方法。 //甲板上的一些人

 public void Shuffle() 
    {
        Random ran = new Random();
        for (int nextCard = 0; nextCard < deck.Length; nextCard++)
        {
            Card hold = deck[nextCard];
            int random = ran.Next(deck.Length);
            deck[nextCard] = deck[random];
            deck[random] = hold;
        }
    }

  public Card DealACard()
    {
        if (nextCard > 51)
        {
            return null;
        }
        return deck[nextCard++];
    }

   public Hand DealAHand(int handSize)        
    {
        Hand hand = new Hand(handSize);
        for (int i = 0; i < handSize; i++)
        {
            hand.AddCard(DealACard());
        }
        return hand;
    }
public void Shuffle()
{
Random ran=新的Random();
for(int-nextCard=0;nextCard51)
{
返回null;
}
返回甲板[nextCard++];
}
公手交易和(int handSize)
{
Hand Hand=新手(手型);
对于(int i=0;i
//一些手工课的学生

    public void AddCard(Card card)
    {
       hand[cardsInHand] = card;
       cardsInHand++;
    }


    public override string ToString() 
    {
        String handToString = ""; //string to hold display format
        //for loop to display each card in a hand
        for (int n = 0; n < cardsInHand; n++)
        {
            handToString += hand[n].ToString() + "\n";
        }
        return handToString;
    }
public void AddCard(卡片)
{
手牌[卡片手牌]=卡片;
cardsInHand++;
}
公共重写字符串ToString()
{
字符串handToString=”“;//用于保存显示格式的字符串
//for loop可在手牌中显示每张牌
对于(int n=0;n
//驾驶员等级

        Deck deck1 = new Deck();
        int cardsToGet = 53;

        do
        {
            Console.Write("How many cards are in one hand? ");
            int handSize = Convert.ToInt32(Console.ReadLine());
           // int handSize = Console.Read();

            Console.Write("How many players are playing? ");
            int players = Convert.ToInt32(Console.ReadLine());

            cardsToGet = handSize * players;

            if (cardsToGet < 53) // if to shuffle deck and display players' hands
            {
                deck1.Shuffle();

                for (int i = 0; i < players; i++) // displays each players hand
                {
                    Console.WriteLine("\nPlayer " + (i + 1) + ":");
                    Console.WriteLine(deck1.DealAHand(handSize));
                }

            }
            else
            {
                Console.WriteLine("\nThere are not enough cards in the deck to deal " + players + " hands of " + handSize + " cards. Try again.\n");
            } 
        }
        while (cardsToGet > 52);
甲板1=新甲板();
int cardsToGet=53;
做
{
控制台。写下(“一只手有多少张牌?”);
int handSize=Convert.ToInt32(Console.ReadLine());
//int handSize=Console.Read();
控制台。写(“有多少玩家在玩?”);
int players=Convert.ToInt32(Console.ReadLine());
cardsToGet=手持*播放器;
if(cardsToGet<53)//如果要洗牌并显示玩家的手
{
deck1.Shuffle();
for(int i=0;i52);

假设要求每手牌的数量,然后许多玩家为每个玩家显示一手牌,而不复制牌。目前,它用俱乐部的王牌填满了每个球员的手。显示时没有错误。

现在您已经更新了问题,这是可以回答的。错误在于你的
构造器,不管你传入的
n
的值是多少,它都会创建每个卡的
值为13,而
套装
值为4。您有正确的方法将
int
转换为
Face
Suit
enum(只需强制转换它),因此您只需像Java版本那样执行模运算:

public Card(int n)
{
  var face = (Face) n % 13;
  var suit = (Suit) n % 4;
}
嗯,差不多了。C#中的
var
关键字只创建了一个局部变量,该变量在其声明的范围之外不可见:在本例中,构造函数。您要做的是为
卡的实例属性赋值。您尚未向我们展示这些属性的名称,但我将假定它们的名称为
Face
Suit
(首字母大写为C#命名约定);根据需要重命名:

public Card(int n)
{
  this.Face = (Face) n % 13;
  this.Suit = (Suit) n % 4;
}

现在,你的卡片应该都不同了,而不是都是俱乐部的王牌。

你好,TJ,欢迎来到S/O,来自其他语言可能是一个学习曲线。就像我切换到Java一样。我很快就把它放在一起,介绍如何在C#中使用列表和类。不是完美的,但是做你想要得到的。希望能有帮助。我最初是通过WPF应用程序与控制台应用程序用C#编写的,但核心是一样的(唯一不同的是“MessageBox.Show()”,它显示了每只手与控制台输出的对比,我也包括了这一点)

我以注释我的代码而臭名昭著,我希望这一切(或大部分)在您深入C#时是有意义的

我从纸牌和西装的枚举开始

public enum CardFace
{
    Two = 0,
    Three = 1,
    Four = 2,
    Five = 3,
    Six = 4,
    Seven = 5,
    Eight = 6,
    Nine = 7,
    Ten = 8,
    Jack = 9,
    Queen = 10,
    King = 11,
    Ace = 12
}

public enum CardSuit
{
    Hearts = 0,
    Clubs = 1,
    Diamonds = 2,
    Spades = 3
}
接下来,一个类来表示一张卡

public class SingleCard
{
    public CardFace Face { get; set; }
    public CardSuit Suit { get; set; }
    // place-holder for randomizing cards
    public int RndNumber { get; set; }
    // return the name of the card based on it's parts as single string
    public string NameOfCard { get { return $"{Face} of {Suit}"; } }
}
现在,这个类用于构建初始牌组,洗牌,发牌和展示牌(整个牌组,或单手牌)

公共类卡片
{
公共列表单组{get;private set;}=new List();
公共列表ShuffledDeck{get;private set;}
//创建一个随机生成器一次并保持激活状态。这有助于防止
//每次需要洗牌时重新创建,并获得相同的序列。
//使静态如果你想要多个甲板,他们继续使用相同的随机化对象
private static Random rndGen=new Random();
公共甲板
{
//建造一副牌。。。
//开始通过ea
public class SingleCard
{
    public CardFace Face { get; set; }
    public CardSuit Suit { get; set; }
    // place-holder for randomizing cards
    public int RndNumber { get; set; }
    // return the name of the card based on it's parts as single string
    public string NameOfCard { get { return $"{Face} of {Suit}"; } }
}
public class DeckOfCards
{
    public List<SingleCard> SingleDeck { get; private set; } = new List<SingleCard>();

    public List<SingleCard> ShuffledDeck { get; private set; }

    // create a single random generator ONCE and leave active.  This to help prevent
    // recreating every time you need to shuffle and getting the same sequences.
    // make static in case you want multiple decks, they keep using the same randomizing object
    private static Random rndGen = new Random();

    public DeckOfCards()
    {
        // build the deck of cards once...
        // Start going through each suit
        foreach (CardSuit s in typeof(CardSuit).GetEnumValues())
        {
            // now go through each card within each suit
            foreach (CardFace f in typeof(CardFace).GetEnumValues())
                // Now, add a card to the deck of the suite / face card
                SingleDeck.Add(new SingleCard { Face = f, Suit = s });
        }

        // so now you have a master list of all cards in your deck declared once...
    }

    public void ShuffleDeck()
    {
        // to shuffle a deck, assign the next random number sequentially to the deck.
        // don't just do random of 52 cards, but other to prevent duplicate numbers
        // from possibly coming in
        foreach (var oneCard in SingleDeck)
            oneCard.RndNumber = rndGen.Next(3901);  // any number could be used...

        // great, now every card has a randomized number assigned.
        // return the list sorted by that random number...
        ShuffledDeck = SingleDeck.OrderBy( o => o.RndNumber).ToList();
    }

    public void DisplayTheCards( List<SingleCard> theCards )
    {
        // show the deck of cards, or a single person's hand of cards
        var sb = new StringBuilder();
        foreach (var c in theCards)
            sb = sb.AppendLine( c.NameOfCard );

        MessageBox.Show(sb.ToString());
    }

    public void ConsoleDisplayTheCards(List<SingleCard> theCards)
    {
        // show the deck of cards, or a single person's hand of cards
        foreach (var c in theCards)
            Console.WriteLine(c.NameOfCard);
    }


    public List<List<SingleCard>> DealHands( int Players, int CardsPerHand )
    {
        // create a list of how many hands to be dealt...
        // each player hand will consist of a list of cards
        var Hands = new List<List<SingleCard>>(Players);

        // prepare every players hand before dealing cards
        for (var curPlayer = 0; curPlayer < Players; curPlayer++)
            // each player gets their own list of cards
            Hands.Add( new List<SingleCard>());


        // prepare card sequence to deal
        var nextCard = 0;
        // loop for as many cards per hand
        for (var oneCard = 0; oneCard < CardsPerHand; oneCard++)
        {
            // loop every player gets a card at a time vs one player gets all, then next player
            for (var curPlayer = 0; curPlayer < Players; curPlayer++)
                // add whatever the next card is to each individual's hand
                Hands[curPlayer].Add(ShuffledDeck[nextCard++]);
        }

        return Hands;
    }



}