Blackjack In Python

In Python it is easy to create this full deck of cards using 2 lists and a couple of for loops: Here we have created a list of 52 cards, with each card consisting of a tuple of 2 values: (name,suit). I made blackjack to the best of my ability in Python. I'm pretty happy with it seeing that I only started coding a few months ago. Anyways try it out and give me any feedback in the comments ty.

In this challenge you are going to create a blackjack game for one player. The computer will be the dealer.

The probability of winning in blackjack is known now. To find the best betting strategy, let’s simplify the problem. We know that the strategy that works in a coin toss event will also work in black jack. However, coin toss event is significantly less computationally intensive. Blackjack Game in Python Movies like Rain Man and 21 portray the ability to count cards in blackjack as being equivalent to a genius. In a nutshell, counting cards means assigning points to each card that is dealt during a round of blackjack, and using this point system to place higher bets when you are in favor of winning more money.

Rules of the game


Blackjack Python Object Oriented

Source: wikipedia.

“Blackjack, also known as twenty-one, is the most widely played casino banking game in the world. Blackjack is a comparing card game between a player and dealer, meaning that players compete against the dealer but not against any other players. It is played with one or more decks of 52 cards. The object of the game is to beat the dealer, which can be done in a number of ways:

  • Get 21 points on the player’s first two cards (called a blackjack), without a dealer blackjack;
  • Reach a final score higher than the dealer without exceeding 21; or
  • Let the dealer draw additional cards until his or her hand exceeds 21.

The player or players are dealt an initial two-card hand and add together the value of their cards. Face cards (kings, queens, and jacks) are counted as ten points. A player and the dealer can count his or her own ace as 1 point or 11 points. All other cards are counted as the numeric value shown on the card. After receiving their initial two cards, players have the option of getting a “hit”, or taking an additional card. In a given round, the player or the dealer wins by having a score of 21 or by having the highest score that is less than 21. Scoring higher than 21 (called “busting” or “going bust”) results in a loss. A player may win by having any final score equal to or less than 21 if the dealer busts. If a player holds an ace valued as 11, the hand is called “soft”, meaning that the player cannot go bust by taking an additional card; 11 plus the value of any other card will always be less than or equal to 21. Otherwise, the hand is “hard”.

The dealer has to take hits until his or her cards total 17 or more points. (In some casinos the dealer also hits on a “soft” 17, e.g. an initial ace and six.) Players win if they do not bust and have a total that is higher than the dealer’s. The dealer loses if he or she busts or has a lesser hand than the player who has not busted. If the player and dealer have the same total, this is called a “push” and the player typically does not win or lose money on that hand.”

Final Product


You can check this game online.

Your Challenge


How To Make Blackjack In Python

Your challenge is to recreate this game for one player only using your chosen programming language (e.g. Python, JavaScript…). The end-user will play against the computer (the dealer).

Coding Blackjack In Python

To start with you may want to create this game using a text-based interface. Cards could be displayed as follows:

Python
  • ♠ As (Ace of spade), 2s, 3s, … 9s, 10s, Js, Qs, Ks
  • ♥ Ah (Ace of heart), 2h, 3h, … 9h, 10h, Jh, Qh, Kh
  • ♣ Ac (Ace of club), 2c, 3c, … 9c, 10c, Jc, Qc, Kc
  • ♦ Ad (Ace of diamond), 2d, 3d, … 9d, 10d, Jd, Qd, Kd

You may also want to investigate unicode characters to display the selected cards in a more visual way.

Tips / Questions to get you started


Before getting started you may want to ask yourself the following questions?
  • What data structure could I use to create the deck of 52 cards?
  • Once I have stored all 52 cards in my data structure, how can I shuffle the cards?
  • What variables will I need to store the initial amount ($) the player has, the value of the bet ($), the total number of points of the dealer, the total number of points for the player?
  • Input: What information do I need to ask/retrieve from the end-user and when does the program need to ask for that information?
  • Output: What information do I need to display to the end-user during the game? (and when in the game?)
  • When or how does the game end?
  • How can I break up this project into smaller achievable steps? What would be the first few steps that I would then focus on?

Complete the Code…


Graphical User interface


Simple Blackjack In Python

Should you decide to create a full graphical user interface (e.g. as a webpage using HTML & JavaScript or as a Python program using graphical library such as PyGame) then you can download the graphics for each of the playing cards from wikimedia.
Online

Other challenges you may enjoy...

Tagged with: iteration, List, Selection

I'm new to programming and I'm joining here to ask questions, contribute (when I have more knowledge under my belt), and basically just learn and figure out if programming is right for me.

I am currently learning Python through a course on Udemy and I'm working on a milestone project to create a Blackjack game. The only rule is for me to use Object Oriented Programming and to create classes for things like the Card and Deck.

I've watched some tutorials on OOP, but I still do not feel confident in implementing them, so I was hoping I could share what I've written and hopefully have a better understanding through interactions here.

Python

To start, I created a plan to break the project down into smaller tasks as it was overwhelming to me. Here's what I planned:

  1. Create a deck with 52 cards

  2. Assign numerical values to each card (Ace can be 1 or 11)

  3. Shuffle the deck

  4. Dealer and player are handed 2 cards each as a start

  5. Player is asked if he/she wants to hit or stand

  6. Dealer has to hit until he reaches 17

  7. Compare 'points' to see who has more

  8. At any point, if anyone exceeds 21, that person loses

I am currently stuck at trying to figure out the first part. I'm basically trying to create a list with all 52 cards in it using OOP and for loops, but I can't seem to get things right.

I'm sharing my code below which was written in Python 2. Any ideas how to proceed?

Thank you,Paul

Blackjack

Edit #1: Thank you everyone for the comments and feedback. I think I am getting a little closer now. Can someone please let me know if I am doing this right and how I can print the list I used to create my deck such that I am able to see each of the 52 cards in a list? I tried using the str method but it appears I'm doing it wrongly.