Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a deck of cards in c#

Tags:

c#

So I'm trying to create a deck of cards for one of my programming classes. I've never really done anything like this so sorry if I made some stupid mistake. I'm coding this in Visual Studio (per class rules). I am trying to create an array of Card objects for my Deck. The problem I am getting is that when I try and print the array out all I get is 52 lines of Card_Games.Card (the namespace is Card_Games). What did I do wrong in my Card class to not properly assign the value and suit of a card to that Card object?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Deck.FillDeck();
        Deck.PrintDeck();
    }
}

class Card
{
    public int Value;
    public static string[] SuitsArray = new string[] {"Hearts", "Diamonds", "Clubs", "Spades"};
    public string Suit;

    public Card(int value, string suit)
    {
        Value = value;
        Suit = suit;
    }

    public Card(string input)
    {
        string tempValue = "";
        string suitSentence = "";
        switch (Value)
        {
            case 11:
                tempValue = "Jack";
                break;
            case 12:
                tempValue = "Queen";
                break;
            case 13:
                tempValue = "King";
                break;
            case 14:
                tempValue = "Ace";
                break;
            default:
                tempValue = Value.ToString();
                break;
        }
        switch (Suit)
        {
            case "Hearts":
                suitSentence = " of Hearts";
                break;
            case "Diamonds":
                suitSentence = " of Diamonds";
                break;
            case "Clubs":
                suitSentence = " of Clubs";
                break;
            case "Spades":
                suitSentence = " of Spades";
                break;
        }
        input = tempValue + suitSentence;
    }
}

class Deck
{
    public static Object[] deck = new Object[52];


    public static void FillDeck()
    {
        int index = 0;
        foreach (string suit in Card.SuitsArray)
        {
            for (int value = 2; value <= 14; value++)
            {
                Card card = new Card(value, suit);
                deck[index] = card;
                index++;
            }
        }
    }

    public static void PrintDeck()
    {
        for (int i=0; i<52; i++)
        {
            System.Diagnostics.Debug.WriteLine(deck[i]);
        }
    }
}
like image 393
MattCV27 Avatar asked Oct 19 '25 12:10

MattCV27


2 Answers

Here's a slightly optimized version.

I've added getters to pull a Named Value and a full Name, e.g "Ace Of Spades". And used an Enum for suites.

Then I've used a single loop with the modulo operator (%) and Math.Floor to populate the deck. This is where the enum comes into play as it is trivial to work with enums and ints.

public class Card
{
    public enum Suites
    {
        Hearts = 0,
        Diamonds,
        Clubs,
        Spades
    }

    public int Value
    {
        get;
        set;
    }

    public Suites Suite
    {
        get;
        set;
    }

    //Used to get full name, also useful 
    //if you want to just get the named value
    public string NamedValue
    {
        get
        {
            string name = string.Empty;
            switch (Value)
            {
                case (14):
                    name = "Ace";
                    break;
                case (13):
                    name = "King";
                    break;
                case (12):
                    name = "Queen";
                    break;
                case (11):
                    name = "Jack";
                    break;
                default:
                    name = Value.ToString();
                    break;
            }

            return name;
        }
    }

    public string Name
    {
        get
        {
            return NamedValue + " of  " + Suite.ToString();
        }
    }

    public Card(int Value, Suites Suite)
    {
        this.Value = Value;
        this.Suite = Suite;
    }
}

public class Deck
{
    public List<Card> Cards = new List<Card>();
    public void FillDeck()
    {
        public void FillDeck()
        {
          //Can use a single loop utilising the mod operator % and Math.Floor
          //Using divition based on 13 cards in a suited
          for (int i = 0; i < 52; i++)
          {
             Card.Suites suite = (Card.Suites)(Math.Floor((decimal)i/13));
             //Add 2 to value as a cards start a 2
             int val = i%13 + 2;
             Cards.Add(new Card(val, suite));
          }
         }
    }

    public void PrintDeck()
    {
        foreach(Card card in this.Cards)
        {
            Console.WriteLine(card.Name);
        }
    }
}

Demo: https://dotnetfiddle.net/Xuj7b6

like image 77
Jon P Avatar answered Oct 22 '25 00:10

Jon P


Change object[] to Card[]. Then in your print method you can print deck[i].Value and deck[i].Suit. Here it is as a console app:

class Program
{
    static void Main(string[] args)
    {
        Deck.FillDeck();
        Deck.PrintDeck();
        Console.ReadLine(); 
    }
}

class Card
{
    public int Value;
    public static string[] SuitsArray = new string[] { "Hearts", "Diamonds", "Clubs", "Spades" };
    public string Suit;

    public Card(int value, string suit)
    {
        Value = value;
        Suit = suit;
    }

    public Card(string input)
    {
        string tempValue = "";
        string suitSentence = "";
        switch (Value)
        {
            case 11:
                tempValue = "Jack";
                break;
            case 12:
                tempValue = "Queen";
                break;
            case 13:
                tempValue = "King";
                break;
            case 14:
                tempValue = "Ace";
                break;
            default:
                tempValue = Value.ToString();
                break;
        }
        switch (Suit)
        {
            case "Hearts":
                suitSentence = " of Hearts";
                break;
            case "Diamonds":
                suitSentence = " of Diamonds";
                break;
            case "Clubs":
                suitSentence = " of Clubs";
                break;
            case "Spades":
                suitSentence = " of Spades";
                break;
        }
        input = tempValue + suitSentence;
    }
}

class Deck
{
    public static Card[] deck = new Card[52];


    public static void FillDeck()
    {
        int index = 0;
        foreach (string suit in Card.SuitsArray)
        {
            for (int value = 2; value <= 14; value++)
            {
                Card card = new Card(value, suit);
                deck[index] = card;
                index++;
            }
        }
    }

    public static void PrintDeck()
    {
        for (int i = 0; i < 52; i++)
        {
            Console.WriteLine($"{deck[i].Value} {deck[i].Suit}");
        }
    }
}
like image 30
Jon Vote Avatar answered Oct 22 '25 00:10

Jon Vote



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!