卡牌手不显示在扑克游戏中

时间:2015-11-27 09:09:14

标签: c# class poker

我用C#制作扑克游戏,为两个不同的玩家随机抽取五张牌,我希望这五张牌能以#34; Number of Suit"格式显示。所有这些方法都在卡片处理类中:

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;
            }
        }
    }
}

PlayCard课程:

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类。当前代码输出五行&#34; 0 of Hearts&#34;但是我想让它为每个玩家输出五张不同的牌。

1 个答案:

答案 0 :(得分:1)

您的显示卡方法是错误的。

尝试:

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

和播放器2相同