Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7055711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:46:53+00:00 2026-05-28T03:46:53+00:00

This code randomizes 52 cards and puts 13 each in 4 box of text

  • 0

This code randomizes 52 cards and puts 13 each in 4 box of text with every click. I got the general idea of how it worked, but I need help clearing some confusion or understanding how it worked. I put bold comments on the ones I am not too sure about.

namespace Cards
{
    enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace}
}

namespace Cards
{
    enum Suit { Clubs, Diamonds, Hearts, Spades }
}

namespace Cards
{
    class PlayingCard 
    {
        private readonly Suit suit;
        private readonly Value value;

        public PlayingCard(Suit s, Value v)
        {
            this.suit = s;
            this.value = v;
        }

        public override string ToString()
        {
            string result = string.Format("{0} of {1}", this.value, this.suit);
            return result;
        }

        public Suit CardSuit() // **Not sure why this is here.  Maybe for future use?**
        {
            return this.suit;
        }

        public Value CardValue() // **Same as above**
        {
            return this.value;
        }
    }
}

namespace Cards
{
using System;
using System.Collections;

class Pack // **Data Access Layer?**
{
    public const int NumSuits = 4; 
    public const int CardsPerSuit = 13;
    private PlayingCard[,] cardPack;
    private Random randomCardSelector = new Random();

    public Pack() //**Storing all elements**
    {
        this.cardPack = new PlayingCard[NumSuits, CardsPerSuit]; // 

        for (Suit suit = Suit.Clubs; suit <= Suit.Spades; suit++) // 
        {
            for (Value value = Value.Two; value <= Value.Ace; value++)
            {
                this.cardPack[(int)suit, (int)value] = new PlayingCard(suit, value);
            }
        }
    }

    public PlayingCard DealCardFromPack() // **Purpose: To dealing unique cards**
    {
        Suit suit = (Suit)randomCardSelector.Next(NumSuits); // **picks random 0-3 from Suit**
        while (this.IsSuitEmpty(suit)) // **Purpose: Checks if empty but don't know how it works**
        {
            suit = (Suit)randomCardSelector.Next(NumSuits);
        }
        Value value = (Value)randomCardSelector.Next(CardsPerSuit);
        while (this.IsCardAlreadyDealt(suit, value)) // ?
        {
            value = (Value)randomCardSelector.Next(CardsPerSuit);
        }
        PlayingCard card = this.cardPack[(int)suit, (int)value];
        this.cardPack[(int)suit, (int)value] = null; // **sets the current element to null so it isn't reused.**
        return card;
    }

    private bool IsSuitEmpty(Suit suit) // **checks if empty**
    {
        bool result = true;

        for (Value value = Value.Two; value <= Value.Ace; value++) //**Checks Null or not**
        {
            if(!IsCardAlreadyDealt(suit, value))
            {
                result = false;
                break;
            }
        }
        return result;
    }

    private bool IsCardAlreadyDealt(Suit suit, Value value) //**returns current element null?**
    {
        return (this.cardPack[(int)suit, (int)value] == null);
    }
}
}

namespace Cards
{
using System;
using System.Collections;

class Hand // **Business layer?**
{
    public const int HandSize = 13;
    private PlayingCard[] cards = new PlayingCard[HandSize]; // **single array?  Thought it had to be 2 because of 2 parameters?**
    private int playingCardCount = 0;

    public void AddCardToHand(PlayingCard cardDealt)//**Confusion when class used as variable**
    {
        if (this.playingCardCount >= HandSize)
        {
            throw new ArgumentException("Too many cards");
        }
        this.cards[this.playingCardCount] = cardDealt; //**Confused...cardDealt value going in card[]?  How does that work..**
        this.playingCardCount++;//**incrementing, how is this helpful**
    }

    public override string ToString()  //to show all 13 cards in hand
    {
        string result = "";
        foreach (PlayingCard card in this.cards)
        {
            result += card.ToString() + "\n";
        }

        return result;
    }
}
}

namespace Cards
{
    public partial class Game : Window
    {
        public const int NumHands = 4;

        private Pack pack = null;
        private Hand[] hands = {new Hand(), new Hand(), new Hand(), new Hand()}; //**Creates 4 different list all single array?**

        public Game()
        {
            InitializeComponent();
        }

        private void dealClick(object sender, RoutedEventArgs e)
        {
            try
            {
                pack = new Pack(); 

                for (int handNum = 0; handNum < NumHands; handNum++) //
                {
                    hands[handNum] = new Hand();
                    for (int numCards = 0; numCards < Hand.HandSize; numCards++)
                    {
                        PlayingCard cardDealt = pack.DealCardFromPack(); //**Deals 13 random cards into each array.  Don't understand how it worked though.**
                        hands[handNum].AddCardToHand(cardDealt);
                    }
                }

                north.Text = hands[0].ToString();
                south.Text = hands[1].ToString();
                east.Text = hands[2].ToString();
                west.Text = hands[3].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:46:54+00:00Added an answer on May 28, 2026 at 3:46 am

    PlayingCard:

    class PlayingCard // **Data Access Layer?**
    

    This looks like a business model, nothing to do with data access. In fact, I don’t see any data persistence (like a database) being used in the code at all, so this application doesn’t have a data access layer.

    private readonly Suit suit; // **readonly to protect from changing value?**
    

    Correct. This makes the value “immutable” in that it can’t be changed once the object is constructed. Note that this value is set in (and only in) the class’ constructor. So to have a different suit one needs to construct a different instance of the class (a different object, as it were).

    public PlayingCard(Suit s, Value v) //**Returning value of every card**
    

    Not really. This is the constructor for the class. Any time a new instance of the class is created (for example, var card = new PlayingCard(someSuit, someValue)), this method executes to construct that instance.

    public Suit CardSuit() // **Not sure why this is here.  Maybe for future use?**
    

    This is a method to get the value of the current Suit for that card. So something using the card can see what suit it is. Note that the value it’s returning is private so it can’t otherwise be read from outside the class. (I disagree with this implementation and would prefer a property instead of a method, but that’s another subject entirely.)

    Pack:

    class Pack // **Data Access Layer?**
    

    Nope, same as before. These are models. I’ll get to that in a bit.

    public Pack() //**Storing all elements**
    

    Constructor again, same as before.

    public PlayingCard DealCardFromPack() // **Purpose: To dealing unique cards**
    

    Yes. This is a logical method to have on a Pack object. One can reasonably expect that a Pack can be used to deal PlayingCards. That is, they can be extracted from the Pack. So that’s what this method is for.

    Suit suit = (Suit)randomCardSelector.Next(NumSuits); // **picks random 0-3 from Suit**
    

    Yup, chooses a random Suit from the given enumeration of known Suits.

    while (this.IsSuitEmpty(suit)) // **Purpose: Checks if empty but don't know how it works**
    

    Well, a pack doesn’t have an infinite number of cards. Every time one is removed, it’s deleted from the class that’s holding the cards (the Pack). So it can’t very well deal from a suit for which it no longer has cards. Thus, this checks to see if there are still cards left in the suit before trying to deal one.

    this.cardPack[(int)suit, (int)value] = null; // **sets the current element to null so it isn't reused.**
    

    Yup, this is related to the previous part. When a card is dealt, it’s removed. So this “deletes” it from the Pack.

    private bool IsSuitEmpty(Suit suit) // **checks if empty**
    

    This is a kind of helper method within the class. The method which deals out the cards needs to check if the suit is empty, so this method was written to make that check. (Rather than have all of the code in one big method, which would be poor design.)

    private bool IsCardAlreadyDealt(Suit suit, Value value) //**returns current element null?**
    

    This is another helper method. When dealing cards, one can’t deal a card that’s already been dealt. So this checks for that. So basically, the larger method which uses these methods is saying: “While randomly looking at suits, I need a suit that still has cards. While randomly looking at cards in that suit, I need a card that’s still in the deck. Now remove that card from the deck.”

    Hand:

    class Hand // **Business layer?**
    

    Kind of. Sort of. It’s another model. I’ll get to that in a second.

    private PlayingCard[] cards = new PlayingCard[HandSize]; // **single array?  Thought it had to be 2 because of 2 parameters?**
    

    What 2 parameters? It’s an array of cards. Each Hand is basically a collection of cards. This stores references to those cards.

    public void AddCardToHand(PlayingCard cardDealt)//**Confusion when class used as variable**
    

    I’m not sure what you’re asking in the comment. The purpose of this method is to add a card to the hand. So a reference to a card is passed to the method, and the method adds it to the current hand.

    this.cards[this.playingCardCount] = cardDealt; //**Confused...cardDealt value going in card[]?  How does that work..**
    

    In this case, cardDealt is the card that was given to the Hand to be added. It has nothing to do with the “dealt” concept in the previous class. It’s just the name of the variable. What this line is doing is adding that card to the array of cards in the Hand.

    this.playingCardCount++;//**incrementing, how is this helpful**
    

    The hand is now larger by one card. So if you added, say, the third card to the hand, it would be added as this.cards[2]. Adding another card to the hand after that needs to go in this.cards[3], otherwise it would replace an existing card, which isn’t the desired effect.

    public override string ToString()  //to show all 13 cards in hand
    

    Correct, this method is just to print out what’s currently in the Hand.


    Conclusion:

    Ok, now that we’ve gone through that exercise (and feel free to ask for clarification), let’s talk for a moment about what a “model” is. It’s kind of a business layer in that it’s a “business object.” That is, it’s an object which encapsulates a business concept.

    In the case of this application, there are three concrete concepts which represent the “business” of dealing cards to a hand. These are:

    • A card
    • A deck, which contains a pre-determined finite list of cards
    • A hand, which contains a list of cards given to it

    Each model has the responsibility to internally maintain its business logic. This is done with its internal variables, properties, methods, etc. Things like DealCardFromDeck are real-world concepts of how the business works which are encoded into the models. The other models don’t care how that model implements that business logic, they just know that one can deal a card from a deck. (This is part of the model’s externally-visible interface to other models.)

    The business logic (business layer, if you will, although this code snipped doesn’t really have “layers” per se) is seen in how the models interact with one another. At its simplest, my list of the three models above basically describes how they interact. The code is just an implementation of that logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
This code worked (correctly reflected the user's current timezone) back in the aspx version.
This code works on Linux but fails to match on Windows: if ( preg_match
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
This code does not seem to compile, I just need to write something to
This code is executed by many way. When it's executed by the form button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.