-3

I am currently working on creating a card shuffler in C# and working on the Deck class now in which I want to have these 3 public methods, I currently have formed the class program and the public card class, but I'm a bit stuck on how I could start off on these 3 in Deck, just any tips about it would help. These are the 3 public methods I want to put in the Deck class

InitStandardDeck - Which populates a deck with the standard 52 cards. The cards should also be shuffled so they are in random order

DealHand - Which randomly deals 2 cards from the deck. The deck should operate like a standard deck in the sense that once those cards are dealt they are removed from the deck.

DrawACard - randomly selects 1 card from the deck. This also should operate like a standard deck in the sense that once the card is dealt it is removed form the deck.

This is the code I have so far Thank you in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Deck deck = new Deck();
            deck.InitStandardDeck();
          
            bool decided = false;
            do
            {
                for (int i = 0; i < 100; i++)
                {
                    List<Card> hand = deck.DealHand();
                    Console.Write("Hand: ");
                    foreach (Card c in hand)
                    {
                        Console.Write(c.GetDisplayName());
                    }
                    Console.Write("\n");

                    Card card = deck.DrawACard();
                    Console.Write("Card: ");
                    Console.Write(card.GetDisplayName());
                    Console.Write(card.Value);
                    Console.Write("\n");

                }

              
                Console.WriteLine("Play again? (y or n)");
                string playerChoice = Console.ReadLine();
                if (playerChoice.Contains("n"))
                {
                    decided = true;

                }
                
            } while (!decided);

        }
    }

    public class Card
    {
        private string m_displayName;
        private SUIT m_suit;
        public int Value { get; private set; }

        public enum SUIT
        {
            Hearts,
            Clubs,
            Dimonds,
            Spades
        }

        public Card(string name, SUIT suit, int value)
        {
            m_displayName = name;
            m_suit = suit;
            Value = value;
        }

        public string GetDisplayName()
        {
            return "[" + m_displayName + " of " + m_suit.ToString() + "]";
        }
    }

    public class Deck
    {

    }
}
NK2030
  • 15
  • 3
  • 1
    Please see [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/328193) You are encouraged to make an attempt. If during your attempt you encounter a specific problem, such as a specific operation producing an error or an unexpected result, we can help with that. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jul 13 '22 at 17:28

1 Answers1

0

Here's kinda what you're looking for. Please don't ask questions like this in the future, as a comment has pointed out. I'm just passing forward some help I got years ago with a similar kind of question. Happy coding!

InitStandardDeck

List<Card> cards = new List<Card>();

public void InitStandardDeck()
{
    for(int i = 1; i <= 13; i++)
    {
        string numberedName = i.ToString(); //I forget this syntax
        if(i == 1)
            numberedName = "Ace";
        if(i == 11)
            numberedName = "Jack";
        if(i == 12)
            numberedName = "Queen";
        if(i == 13)
            numberedName = "King";
        cards.Add(new Card((numberedName + "Hearts"), 
                           Card.Suit.Hearts,
                           i);
        cards.Add(new Card((numberedName + "Dimonds"), 
                           Card.Suit.Dimonds,
                           i);
        cards.Add(new Card((numberedName + "Spades"), 
                           Card.Suit.Spades,
                           i);
        cards.Add(new Card((numberedName + "Clubs"), 
                           Card.Suit.Clubs,
                           i);
    }

    Random rng = new Random();
    cards = cards.OrderBy(a => rng.Next()).ToList();
}

DealHand

public List<Card> DealHand()
{
    List<Card> hand = new List<Card>();
    hand.AddRange(cards.GetRange(0,2));
    cards.RemoveRange(0,2);
    return hand;
}

DrawACard

public Card DrawACard ()
{
    Card card = cards.ElementAt(0);
    cards.RemoveAt(0);
    return card;
}
Josh Heaps
  • 296
  • 2
  • 13
  • 1
    When providing answers: do not add meta-text like "thanks", "Please don't ask questions like" and other story-telling unrelated to the question (text you put into this answer not suitable for the site at all neither in an answer nor in a comment). Also avoid recommending known bad practices like instantiating local instance of `Random` inside a method or using `OrderBy` for randomizing lists - there is usually good duplicate for common questions (https://stackoverflow.com/questions/273313/randomize-a-listt in this case) – Alexei Levenkov Jul 13 '22 at 18:14
  • @AlexeiLevenkov yeah, I know this isn't the sort of thing this website is meant for. Just... Passing it on. Thank you for trying to make Stack Exchange a better place! – Josh Heaps Jul 13 '22 at 19:25