DEV Community

Robert Duggan
Robert Duggan

Posted on

Tackling a Problem From a Beginner's Perspective

What do I do?

We often see technical blog posts from experienced developers sharing their insights from years of coding, hardly ever from someone just starting out. The hardest part of beginning a project is actually imagining a project to start, at least for me. So, I sought inspiration from the internet. (codecademy's final project)

Codecademy's Final Project in python was to create some terminal program. This was my first project completely from scratch. Of the options at codecademy I decided on the blackjack game. Making a blackjack game seemed fairly challenging but within my grasp. So I got to work. BTW, you can find the source code here

What do I do, now?

I began by establishing what I would actually need to play blackjack in real life. That being a deck of cards and some people to play with. I didn't have people to play with so I had to settle for some computer opponents of which I have not implemented any kind of AI for yet.

To create the players I took an input from the user about how many people were playing. I then created an empty dictionary of players and filled it with players and an empty hand. ({player 1: [], player 2: []})

I read that blackjack at casinos uses upwards of 6 decks! This is the bulk of the problem that I made for myself. If I had just used 1 deck I could have made it work, but I'm stubborn and wanted to make it as much like the real thing as I could.

This is where the fun begins!

I started this off by creating an empty list and then filling it with numbers from 1 to 52 * 6 + 1. There may be other ways to do this, but I wanted to pop cards out so I didn't use the same card twice. However, I knew that I wouldn't be able to do anything with any numbers above 52, as that's all that's in a deck. So I created a function to reduce the cards into a usable format to use for later.

Now that I have my decks and my fake people, I could start working out how to get fake cards into fake people's hands. I did this by using the function I created that reduces the cards input values to 1 - 52 on a random number generated from the length of the cards list - 1, and then pulling it out and appending it to the players dictionary.

After I got cards into people's hands, I began to work on figuring out just what those cards were (as of right now they are just some number between 1 and 52). My strategy here was to first define the suits, then figure out the faces. It actually took me quite a while to land on a method I liked (the simplest). I just assigned 1 - 13 as diamonds, 14-26 clubs, and so on. As for the face cards, I created a function to reduce the cards given to it to land between 1 and 13, that way I didn't have to have 16 if statements. Although, in retrospect the time I spent making that work was probably much longer than it would have taken to just manually line them out.

In blackjack, the cards numbered 1 - 10 are worth their respective value, however each face card is worth 10 and the ace is worth 11. All I have right now in the players' hands are 2 numbers between 1 and 52. This isn't helpful. Luckily, I made a function before to reduce my if statements on finding the card faces. I utilized it here to loop through each hand and condense it to a value between 1 and 11 to get added up later to decide the winner.

Obviously, we would want the user to know what cards are in their hand, otherwise how can they make smart decisions? I created a little function to list the cards from the dictionary, combined with the other functions I made to reduce the values and find the faces, to output to the user what was in their hand. I then gave him the option to hit or to stand.

If the user selected hit, I pulled another random card from my card list and did my magic to it and put it in the users hand. If the sum of the cards in their hand was greater than 21, I let him know that he lost. Then I rubbed it in his face by telling him who won. By this point, I'd gotten lazy. The user only gets 2 chances to hit, and the computer players aren't making any decisions. I summed everyone up and compared it to the highest known so far and output that to the console.

Conclusion

Thanks for coming to my TED talk about spending 4 days on a blackjack game that takes 4 seconds to play.

Discussion (5)

Collapse
deciduously profile image
Ben Lovy • Edited on

Nice work!

spending 4 days on a blackjack game that takes 4 seconds to play

Too real :)

I took a look at your code, congrats on getting it working. One point I saw is that you've hardcoded your card logic to work with exactly 6 decks. Can you think of a way to generalize this to n decks? That way, the user can choose, or you can use an appropriate amount for the number of players. You can do this just using functions, but this might be an opportunity to get practice defining your own classes, too.

I would also recommend wrapping up your running logic, and using the if __name__ == "__main__" idiom. See this post for an explanation of what that does and why it's a good idea.

Definitely don't take this as bashing your code - you succeeded here! Your program does the thing. First of many.

Collapse
rduggan profile image
Robert Duggan Author

Oh no, I really appreciate the feedback! It's nice to get the perspective of someone who currently does this stuff for a living.

I'm checking that stackoverflow out now.

Collapse
deciduously profile image
Ben Lovy

I've only been a "real" programmer for a few months ;). People who write programs that work are real programmers.

Thread Thread
rduggan profile image
Robert Duggan Author

Hey, I'm not expecting to spend a whole lot more time on this exercise, however I can't help thinking about ways to improve it...

What if I used a class to handle the players? (I'm at my actual day job right now, so I can't look at it) The problem I see is how to handle instantiating some random amount of players? If you look at the code I have now I'm handling it with dictionaries, which I think is a good method, however could I do the same type of thing for classes?

Thread Thread
deciduously profile image
Ben Lovy

Definitely! I think that's a good idea. You can have an overarching Game class that maintains a list of players and the list of cards, and ask for how many players to create in the constructor. Then, each player can be a Player instance, you can have a CardDeck, and a Card...I think classes and objects are really useful for organizing your code as it gets bigger. You can keep most of the same actual logic, it just provides a convenient, readable structure.