In this tutorial, we'll cover how to build a command line game for playing Blackjack using Python!
You'll get to build the game from start to finish, and when you're done, you'll have a fully functioning game to play from the command line.
While building the game, we'll explore a few handy Python concepts, such as object-oriented programming using classes and how to manage a game loop.
This tutorial is also extracted from an entire course on building a Blackjack game using a graphical user environment GUIwhich you can check out if you're interested.
Blackjack is a gambling game that requires only a deck of cards.
The goal of the game is to get as close as possible to a hand worth 21 points as the dealer flips over your cards — but go over and you're out!
In Blackjack, numbered cards 2 through 10 are worth their face value, picture cards jack, queen, and king are worth 10, and an ace is worth either 1 or 11 depending on your other cards.
To start a hand, players place their bets and are dealt two cards face up.
They can choose to "hit" receive another card or "stick" stay with their current hand as they attempt to get as close as possible to 21.
If they chose to hit and go over 21, they "bust" and lose the hand and the money they bet!
Players face off against the dealer, who starts with one card face down and one face up.
When all players have chosen to stick or have busted, the dealer then flips over their hidden card and either hits or sticks, their goal being to get a higher hand than any of the players.
If the dealer busts, they pay como al y ganar the value of each player's wager to that player, provided that the player hasn't already busted.
They also need to pay out if they don't get a higher hand than a player.
There are a lot of other rules of course!
Okay, let's get started with some coding!
Installing Python If you don't already have Python installed on your computer, you'll need to do so based on the instructions.
If you'd rather avoid that, you can grab an online coding sandbox with Python and other necessary libraries pre-installed sign in required.
Defining Classes Before we begin coding our blackjack game, it's important we cover how we'll use object-oriented programming, since we will need to utilize classes for our game.
The deck shrinks as cards are drawn and contains 52 unique cards.
A hand is what defines each player's score and thus who wins.
Let's begin with the simplest concept: the Card.
The Card class The Card class will be the first class we define, as both of our other classes will need to use it.
Create a Python file called blackjack.
This will allow us to shuffle our virtual deck of cards at the beginning of every game.
Our first class will be one representing the playing cards.
Each card will have a suit hearts, diamonds, spades, and clubs and a value ace through king.
Our function will return the value and the suit, for example, King of Spades.
That's all we need to do for a Card!
Next up, we need to create a Deck of these Card classes.
The Deck class The Deck will need to contain 52 unique cards and must be able to shuffle itself.
It will also need to be able to deal cards and decrease in size as cards are removed.
Create the Deck class in the blackjack.
We achieve this by using a list comprehension containing lists of every suit and value.
We pass each combination over to the initialization for our Card class to create 52 unique Card instances.
Our Deck will need to be able to be shuffled so that every game is different.
We use the shuffle function in the random library to do this for us how fitting.
To avoid any potential errors, we will only shuffle a deck which still has two or more cards in it, since shuffling one or zero cards is pointless.
After shuffling, we will need to deal cards too.
We utilize the pop function of a list which is the data structure holding our cards to return the top card and remove it from the deck so that it cannot be dealt again.
That's it for the Deck class!
The final utility class to be created for our game to work is the Hand.
All players have a hand of cards, and each hand is worth a numerical value based on the cards it contains.
The Hand class A Hand class will need to contain cards just like the Deck class does.
Since the dealer's hand should only display one card, we also keep track of whether the Hand belongs to the dealer to accommodate this rule.
Start with the below to create the Hand class in the blackjack.
When adding a card to the hand, we simply add the Card instance to our cards list.
This is intentional and done below too!
This way, you don't need to perform the indents yourself and can focus on reading the instructions and code instead of chasing down whitespace errors.
In this code, we first initialize the value of the hand to 0 and assume the player does not have an ace since this is a special case.
Once this is done, we check to see if there was an ace and the increase of 11 points brought the hand's value over 21.
If so, we make the ace worth 1 point instead by subtracting 10 from the hand's value.
The Game Loop We will define the game's main loop within its play method, so that to start a game, you will simply need to create an instance of the Game class and call.
We can use the display method on our Hand instances to print this to the screen.
This marks the end of the code that needs to run at the beginning of every new game.
Now, we enter a loop that will run until a winner is decided.
We need to check whether either hand totals 21, which we will do using two if statements.
If either has a hand value of 21, their Boolean is changed to True.
If either of the Booleans are True, then we have a winner, and will print the winner to the screen and continue, thus breaking us check this out of the game loop.
The player can now make a choice—whether or not to add more cards to their hand hit or submit their current hand stick.
This will always return us a string containing poker online aol text the user typed into the command line.
Since we have a string, we can cast the user's input to lowercase using the lower function to avoid having to check combinations of upper case and lower case when parsing their reply.
Since their total has changed, we will now need to check whether they are over the allowed limit of 21.
Okay, now let's handle when the player decides to stick with their hand.
If they do this, it's time for their score to be compared with the dealer's.
This means we will only get into this block when the user wants to stick.
The value of both the player's and the dealer's hand are printed to the screen to give the final results.
We then compare the values of each hand to see which is higher.
If the player's hand is a higher value than the dealer's, we print You Win!.
If the scores are equal, then we have a tie, so we print Tie!.
That completes https://gothailand.info/blackjack/online-gambling-in-china.html logic required for a user to play a single game.
This will take us right back to the top at self.
Running the Game We've completed the game!
Now, it's time to run this code.
You can start the game by typing python3 blackjack.
Wrapping Up Congrats on working your way through this article source />In it, we covered how to build handy concepts like object-oriented programming, game flow design, and even the basics of Blackjack.
If you got stuck, the complete solution for this project can be found.
You can also launch an online coding sandbox with it preloaded.
Two limitations of this game are that the dealer will never hit and there is no concept of betting.
Feel free to add these more info yourself if you'd like!
Because a dealer is required to hit or stick at certain hand values, you can develop a program that mimic the dealer exactly.
You can also check out the behind this tutorial, if you'd like!
Note from the DEV admins: Now reaching over 3 million visitors per month, is the fastest growing software development community in the world.
We think it's the most awesome and we're working hard to keep it that way.
It's free, open source, devoted to the open web, and will never have popups or a pay wall.
I'm Saul, the founder and CEO of Next Tech.
I've been programming since I was a kid and love to share my findings, thoughts, and the occasional rant about the software industry.
Hey man, it's cool, you know I followed your blog too when constructing my BlackJack project.
I like the user blackjack strategy card surrender a lot.
I hope you don't mind that I've put together a little embedded version of your project below that people can run directly from dev.
We strive for transparency and don't collect excess data.
main.js. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27. var dealer_card2; var dealer_total; var dealer_total2; var dealer_card_dealt; ...
Completely I share your opinion. In it something is also to me it seems it is excellent idea. Completely with you I will agree.
Instead of criticising write the variants is better.
Bravo, this phrase has had just by the way
Quite good question
Also that we would do without your remarkable phrase
I regret, that I can not participate in discussion now. It is not enough information. But this theme me very much interests.
I confirm. And I have faced it. Let's discuss this question. Here or in PM.
And everything, and variants?
I congratulate, your idea is brilliant
I consider, that you are not right. I am assured.
I am sorry, that has interfered... At me a similar situation. Let's discuss. Write here or in PM.
In my opinion you are not right. I suggest it to discuss. Write to me in PM.
I congratulate, this remarkable idea is necessary just by the way
In my opinion you are not right. Write to me in PM, we will communicate.
Completely I share your opinion. I think, what is it good idea.
In my opinion you are not right. I can prove it. Write to me in PM.
Excuse, not in that section.....
Willingly I accept. The theme is interesting, I will take part in discussion. I know, that together we can come to a right answer.
Not spending superfluous words.
Completely I share your opinion. In it something is also idea good, I support.