Python Blackjack Simulator

Python Blackjack Simulator Average ratng: 7,7/10 7418 votes

Hello everyone.

I am learning Python at the moment and I am liking it, so after finishing the collection course and the Dungeon Game I wanted to test my skills with a BlackJack game.

Here is my code so far:

I am new to programming, and I am doing some homework to get more hands on coding experience. I have written a blackjack game in Python 3 and would like a code review of any and all of my code. This is a GUI version of the game blackjack, written in Python and Pygame. Pyweek 71 geometrian 68 library 65 gui 61 engine 58 physics 57 simulation 52. Blackjack Training Simulator Practice Optimal Strategy The fully responsive simulator can be accessed from a desktop or most modern mobile devices, which effectively means – if you have an internet connection – you’ll be able to learn anywhere.

$begingroup$ I think you really need to read the documentation on how classes in python work. For example, you would rarely ever have free standing code as seen in the Deck class, you would either put it in the init function or under a different function $endgroup$ – Navidad20 Dec 14 '16 at 18:26.

I have tested it on my terminal in my virtual environment and I would say it is doing fine.I have added what I will need to do next as comments at the bottom.

Please test it and let me know if you find any bugs that I have missed!! THANKS

Also, as you may remember Kenneth Love , I have asked you about global variables in Python yesterday and the question was because of this project:

I have to use a few: 4 if I have counted correctly.I have thought (actually still think) that I need those because I will have to use them a lot in the code.

Python

But if there is a better way to do this (which I am pretty sure there is :) ), feel free to let me know.

Thanks

Vittorio

Instead of selecting a card and then removing it as its own step, why not just .pop() the first occurrence of the card's value?

Python 3 Blackjack

Awesome work, though! Great to see people come up with brand new creations!

Right, pop is surely needed here.I was wondering about that also because now I don't want to waste the card value as I need it later to check the stats.

ty Kenneth,

Blackjack

good to see you often around the forum

Posting to the forum is only allowed for members with active accounts.
Please sign in or sign up to post.

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

Rules of the game


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


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).

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

  • ♠ 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…


Python blackjack simulator games

Graphical User interface

Python Blackjack Simulator Hack


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.

Coding Blackjack In Python

Blackjack

Other challenges you may enjoy...

Tagged with: iteration, List, Selection