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 PlayingCard
s. 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 Suit
s.
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.
5
solved Need help understanding this code in a tutorial