【问题标题】:Card hands not displaying in poker game扑克游戏中不显示牌手
【发布时间】:2016-03-01 10:40:24
【问题描述】:

我正在用 C# 制作一个扑克游戏,它为两个不同的玩家随机抽取五张牌,我希望这五张牌以“花色数”的格式显示。用于此的所有方法都在发牌类中:

class DealOut : CardDeck
{
    private PlayingCard[] Player1;
    private PlayingCard[] Player2;

    private PlayingCard[] Sorted1;
    private PlayingCard[] Sorted2;

    public DealOut()
    {
        Player1 = new PlayingCard[5];
        Player2 = new PlayingCard[5];

        Sorted1 = new PlayingCard[5];
        Sorted2 = new PlayingCard[5];
    }

    public void Deal()
    {
        CreateDeck();
        GetHand();
        SortCards();
        DisplayCards();
        Evaluate();
    }

    // Gives 5 cards to each player
    public void GetHand()
    {
        for (int i = 0; i < 5; i++)
            Player1[i] = GetDeck[i];
        for (int i = 5; i < 10; i++)
            Player2[i - 5] = GetDeck[i];
    }

    public void SortCards()
    {
        var SeePlayer1 = from hand in Player1
                         orderby hand.CardNo
                         select hand;

        var SeePlayer2 = from hand in Player2
                         orderby hand.CardNo
                         select hand;

        var index = 0;
        foreach (var element in SeePlayer1.ToList())
        {
            Sorted1[index] = element;
            index++;
        }

        index = 0;
        foreach (var element in SeePlayer2.ToList())
        {
            Sorted2[index] = element;
            index++;
        }
    }

    public void DisplayCards()
    {
        Console.WriteLine("\nPlayer 1's cards are:\n");
        for (int i = 0; i < 5; i++)
        {
            Console.Write(CardNo.ToString());
            Console.Write(" of ");
            Console.Write(CardSuit.ToString());
            Console.Write("\n");
        }

        Console.WriteLine("\nPlayer 2's cards are:\n");
        for (int i = 5; i < 10; i++)
        {
            Console.Write(CardNo.ToString());
            Console.Write(" of ");
            Console.Write(CardSuit.ToString());
            Console.Write("\n");
        }
    }

这是 CardDeck 类:

class CardDeck : PlayingCard
{
    // All cards in deck
    const int AllCards = 52; 
    // Array of all cards
    private PlayingCard[] deck;

    public CardDeck()
    {
        deck = new PlayingCard[AllCards];
    }

    // Get current deck
    public PlayingCard[] GetDeck { get { return deck; } } 

    // Create deck of cards
    public void CreateDeck()
    {
        int i = 0;
        foreach (Suit s in Enum.GetValues(typeof(Suit)))
        {
            foreach (Number n in Enum.GetValues(typeof(Number)))
            {
                deck[i] = new PlayingCard { CardSuit = s, CardNo = n };
                i++;
            }
        }

        Shuffle();
    }

    // Shuffle the cards
    public void Shuffle()
    {
        Random rand = new Random();
        PlayingCard temp;

        // Shuffles 20 times
        for (int shuffle = 0; shuffle < 20; shuffle++)
        {
            for (int i = 0; i < AllCards; i++)
            {
                // Randomy swap cards, loop 52 times
                int secondCardIndex = rand.Next(13);
                temp = deck[i];
                deck[i] = deck[secondCardIndex];
                deck[secondCardIndex] = temp;
            }
        }
    }
}

还有扑克牌类:

class PlayingCard
{
    public enum Suit
    {
        Hearts, Clubs, Diamonds, Spades
    }

    public enum Number
    {
        Two = 2, Three, Four, Five, Six, Seven, Eight,
        Nine, Ten, Jack, Queen, King, Ace
    }

    public Suit CardSuit { get; set; }
    public Number CardNo { get; set; }
}

我尝试了各种让文本显示的方法,包括构建一个 DisplayText 类,但都没有奏效。当前代码输出五行“红心0”,但我希望它为每个玩家输出五张不同的牌。

【问题讨论】:

  • 旁注:根据您的代码,即class CardDeck : PlayingCard - 所以 deckcard?不是 52 个的集合
  • @DmitryBychenko public void CreateDeck() 方法创建一个包含 52 张卡片的数组
  • @Yellowman94,这不是从PlayingCard 继承CardDeck 的好理由。你可以拨打myCardDeck.CardSuit,这显然是无意义的。关系“一个 CardDeck 包含 52 张扑克牌的集合”(您在代码中创建)是正确的。 “A CardDeck 是 PlayingCard”不是。
  • 另外,为了简化一切:不要洗牌并从顶部挑选牌 - 而是将牌按顺序排列并随机发牌。这在计算上要简单得多,但在概率上是等效的。

标签: c# class poker


【解决方案1】:

你的显卡方法是假的。

试试:

        foreach(var card in Player1)
        {
            Console.Write(card.CardNo.ToString());
            Console.Write(" of ");
            Console.Write(card.CardSuit.ToString());
            Console.Write("\n");
        }

player2 也一样

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2011-05-09
    • 1970-01-01
    • 2012-01-07
    • 2020-01-14
    • 2017-03-26
    • 1970-01-01
    相关资源
    最近更新 更多