Search code examples
c#classpoker

Card hands not displaying in poker game


I'm making a poker game in C# that randomly draws five cards for two different players, and I want the five cards to be displayed in the format "Number of Suit". All the methods for this are in a card dealing class:

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

This is the CardDeck class:

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

And the PlayingCard class:

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

I've tried various ways of getting the text to display, including building a DisplayText class, that haven't worked. The current code outputs five lines of "0 of Hearts" but I want it to output the five different cards for each player.


Solution

  • your display card method is false.

    try :

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

    and the same for player2