I have a c# project to Create a console project named Cards that includes a struct Card, two classes Deck and Hand, and two enums: Rank and Suit. I attached a require output for this project. I have all my class but when I compile it only show the first few line comparing to the whole output.

    Program.cs
    namespace Cards {

    class Program
    {
        static void Main(string[] args)
        {

            Deck deck = new Deck();
            deck.Shuffle();

            for (int i = 0; i < 5; i++)
            {
                Hand hand1 = new Hand(deck.Deal(5));
                Console.WriteLine(hand1.ToString());
            }

            Console.ReadKey();
        }
    }
}

Hand.cs
namespace Cards
{
    class Hand
    {
        public List<Card> Cards
        {
            get;
            private set;
        }

        public Hand(List<Card> newHand)
        {
            newHand.Sort();
            this.Cards = newHand;
        }

        public bool IsFlush()
        {

            int spadeCount = 0;
            int heartCount = 0;
            int clubCount = 0;
            int diamondCount = 0;

            foreach (Card c in Cards)
            {
                switch (c.CardSuit)
                {
                    case Suit.SPADE:
                        spadeCount++;
                        break;
                    case Suit.HEART:
                        heartCount++;
                        break;
                    case Suit.CLUB:
                        clubCount++;
                        break;
                    case Suit.DIAMOND:
                        diamondCount++;
                        break;
                }
            }

            bool isFlush = spadeCount >= 5 || heartCount >= 5 || clubCount >= 5 || diamondCount >= 5;

            return isFlush;
        }

        public bool IsStraight()
        {
            int straightCount = 0;

            object lastCard = null;

            foreach (Card c in Cards)
            {
                if (lastCard == null)
                {
                    lastCard = c;
                }

                if (c.CardRank - ((Card)lastCard).CardRank == 1)
                {
                    straightCount++;
                }

                lastCard = c;
            }

            bool isStraight = straightCount >= 5;
            return isStraight;
        }


        public override string ToString()
        {
            String output = "";

            foreach (Card c in Cards)
            {
                output += String.Format("{0}", c.ToString());
            }


            if (IsFlush() && IsStraight())
            {
                output += " Straight Flush";
            }
            else if (IsFlush())
            {
                output += " Flush";
            }
            else if (IsStraight())
            {
                output += " Straight";
            }

            return output;
        }
    }
}

Deck.cs
namespace Cards
{
    class Deck
    {
        public List<Card> Cards
        {
            get;
            private set;
        }

        public Deck()
        {
            this.Cards = Deck.NewDeck();
        }

        public Deck(List<Card> cardDeck)
        {
            this.Cards = cardDeck;
        }

        public void Shuffle()
        {
            Random random = new Random();
            for (int i = this.Cards.Count; i > 1; i--)
            {
                int j = random.Next(i);

                Card tmp = this.Cards[j];
                this.Cards[j] = this.Cards[i - 1];
                this.Cards[i - 1] = tmp;
            }
        }

        public List<Card> Deal(int i)
        {
            if (i > this.Cards.Count)
            {
                return new List<Card>();
            }

            List<Card> dealtCards = new List<Card>();
            for (int j = 0; j < i; j++)
            {
                dealtCards.Add(this.Cards[j]);
            }

            this.Cards.RemoveRange(0, i);

            return dealtCards;
        }

        public override string ToString()
        {
            String output = "";
            int suitCount = 4;
            int currCount = 0;

            List<Card> sortedDeck = NewDeck();

            foreach (Card c in sortedDeck)
            {
                output += String.Format("{0,-9}", c.ToString());
                if (++currCount == suitCount)
                {
                    output += "\n";
                    currCount = 0;
                }
            }

            return output;
        }

        public static List<Card> NewDeck()
        {
            List<Card> newDeck = new List<Card>();

            foreach (Cards.Rank rank in Enum.GetValues(typeof(Cards.Rank)))
            {
                foreach (Cards.Suit suit in Enum.GetValues(typeof(Cards.Suit)))
                {
                    newDeck.Add(new Card(suit, rank));
                }
            }

            return newDeck;
        }
    }
}

Card.cs
namespace Cards
{
    public enum Suit { DIAMOND, CLUB, HEART, SPADE };
    public enum Rank { ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING };

    public struct Card : IComparable<Card>
    {

        public Suit CardSuit
        {
            get;
            private set;
        }

        public Rank CardRank
        {
            get;
            private set;
        }

        public Card(Suit _suit, Rank _rank)
            : this()
        {
            this.CardSuit = _suit;
            this.CardRank = _rank;
        }

        public override string ToString()
        {
            String rank = "";
            String suit = "";

            switch (this.CardRank)
            {
                case Rank.ACE:
                    rank = "Ace";
                    break;
                case Rank.DEUCE:
                    rank = "Deuce";
                    break;
                case Rank.THREE:
                    rank = "Three";
                    break;
                case Rank.FOUR:
                    rank = "Four";
                    break;
                case Rank.FIVE:
                    rank = "Five";
                    break;
                case Rank.SIX:
                    rank = "Six";
                    break;
                case Rank.SEVEN:
                    rank = "Seven";
                    break;
                case Rank.EIGHT:
                    rank = "Eight";
                    break;
                case Rank.NINE:
                    rank = "Nine";
                    break;
                case Rank.TEN:
                    rank = "Ten";
                    break;
                case Rank.JACK:
                    rank = "Jack";
                    break;
                case Rank.QUEEN:
                    rank = "Queen";
                    break;
                case Rank.KING:
                    rank = "King";
                    break;
            }

            switch (this.CardSuit)
            {
                case Suit.SPADE:
                    suit = (char)6 + "";
                    break;
                case Suit.HEART:
                    suit = (char)3 + "";
                    break;
                case Suit.DIAMOND:
                    suit = (char)4 + "";
                    break;
                case Suit.CLUB:
                    suit = (char)5 + "";
                    break;
            }

            return String.Format("{0} {1}", suit, rank);
        }

        public int CompareTo(Card other)
        {
            int compare = this.CardRank.CompareTo(other.CardRank);

            if (compare.Equals(0))
            {
                return this.CardSuit.CompareTo(other.CardSuit);
            }

            return compare;
        }
    }
}

cards_opt

1) IsStraight will never return true because in line 85 you compare it with itself and it won't be 1 rank more than itself. You can fix this method by adding one keyword to it. It will also never detect a royal straight.

2) In your override of ToString() you build a string by adding to a string. This is a bad practice. Use StringBuilder to build strings.

3) Your shuffle isn't the Fisher-Yates shuffle (which it looks like you are trying to do).

4) Your output is missing a routine to display the deck, and the shuffled deck. You are also missing a space inbetween each card in the ToString() method of the class Hand. You also can't expect a flush, straight or straight flush in every run, or in many runs. They are rare, which is why they are the top winning hands :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.