Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Encapsulation with Properties Property Practice

General frustration about C# not clicking in my brain

Around the 2:40 mark, Jeremy says: "This is how I learn to code. I often know what I want to do but I don't always know how to do it. That gives me an opportunity to figure it out."

I know some JavaScript already, but I'm having this feeling so much with C#. It reminds me of the first time I was trying to understand how a for loop works. I knew what it was doing, I could describe it, and yet I still somehow couldn't wrap my head all the way around it. I know one day it'll all make sense and I'll wonder what was wrong with me for not understanding it yet.

I'm trying to build a Console app for Black Jack. Here is how I would describe parts of it in plain English:

  1. There is a Card . It has a rank and suit. It can be Deal 't from a Deck into a Hand.
  2. There is a Player which can Hit, Stand, Bet/Ante, and has Cards/Hands. There will be a HumanPlayer and a ComputerPlayer. The HumanPlayer will make an active choice on whether to Hit, Stand, or Bet. The ComputerPlayer will be automated based on the situation (Stand on 17 or higher; Hit on 16 or lower)
  3. There is a Deck made up of Cards. It can be Create 'd, Shuffle 'd, and Deal 't.
  4. There is a Hand, which is made up of Cards and can be Calculate 'd for the Total. The calculations will be by the value of number cards; Jacks, Queens, and Kings will count as 10; Aces will be counted as 11 or 1 based on whether the Total would put it above 21. If a Hand goes over 21, it's a Bust and that Player loses. Whichever player has the higher Total for its Hand would win the game.
  5. There is Money which belongs to each player. The players can Bet that Money, but not more than they currently have.
Steven Parker
Steven Parker
221,293 Points

Your strategy for building your app sounds good. But are you just sharing, or did you have a question about it?

Steven Parker , the hardest part about C# for me in the early stages is how to tie everything together. So more specifically, if there is a Deal method, then it also has to Deal from a Deck and to a certain Player.

1 Answer

Steven Parker
Steven Parker
221,293 Points

Based on your description, I might imagine that a Hand might have an "Add" method that takes a Card argument, and a Deck might have a "Deal" method that returns the top card. So:

Player.Hand.Add(TheDeck.Deal());  // deal the top card into the player's hand
if (Player.Hand.Total > 21) {     // oops!  Bust

But you could also have "Deal" take an argument that is which hand to add to, and have it call "Add" directly instead of returning the dealt card:

TheDeck.Deal(Player.Hand);

So you have implementation options. Do what makes the most sense to you.

Yeah, I actually just woke up this morning with a "Eureka!" moment about List<Type> Thank you for this!