Blackjack Game Python

admin
  1. Play Blackjack For Free
  2. Blackjack Game Python Code

Create our own Blackjack Game using Python Blackjack is a card-based game played at casinos. The participants in this game do not compete with each other but the dealer assigned by the casino. In this article, we will be creating the.

Just a simple console blackjack game. The bread and butter code was written with lots of comments, so you can improve on it. Really, the most important part is figuring out when an ace is 11 in value and when it is a 1 in value, so you don't bust.

A card gambling game made by Hardik Deshmukh in Python Over here, I explained how to make you comply with the different rules and conditions required to make the game in python. I’m going to recommend that you build your own code by going through the article first, and in case you feel less confident than you can always refer the code. Documentation of the BlackJack Game¶. Here is a little bit of documentation about the more important functions and classes from the blackjack program. This documentation is auto-generated from the Python source code and doc strings contained in. Learn how to build a command line game in Python for playing Blackjack.

7,327 Views Blackjack python simulator

Can you actually assign values e.g king'queen, I am having trouble displaying the values of the cards.

We're a friendly, industry-focused community of1.20 million developers, IT pros, digital marketers,and technology enthusiasts learning and sharing knowledge.

Here is a little bit of documentation about the more important functions andclasses from the blackjack program. This documentation is auto-generatedfrom the Python source code and doc strings contained in the source code.

blackjack.py
A Blackjack game
class blackjack.Blackjack(win)¶

Controller or coordinator of the game. All buttons belongto this class.

go()¶
This the entry and exit point to/from main. The MouseTrapengine exits to here, then we exit back to main
hit()¶
Button call back – give player another card and let dealerplay then check if game over yet. Otherwise, back to theHit or Stand choice.
play()¶
Button call back to begin a hand. It does some clean upfrom the previous hand and does the initial deal to the dealer andplayer. Unless the player or dealer got a Blackjack (21 from justthe two cards in the initial deal), then after this functionexits, players next pick to Hit or Stand
quit()¶
Button call back to exit the program. It just returnsFalse so that the MouseTrap engine will exit
stand()¶
Button call back – player does not need another card. Let thedealer play now, which finishes the hand.
Blackjack python simulator
startOver()¶
Button click handler for reseting the game
blackjack.main()¶
The main() program that gets everything started. It basicallyjust creates the graphics window, creates the Blackjack(controller) object and gets it started. Another external programthat imports this module could easily do the same and start aBlackjack game in it’s own graphics window.
bkJplayers.py
The participants (Dealer, Player) of a Blackjack game
class bkJplayers.Contestant(win, deck, xmin, ymin, xmax, ymax)¶

Common stuff between player and dealer. Don’t create aninstance of this class - that’s what player and dealer are for.They inherit form this class, which on exists to cut down on codeduplication.

busted()¶
Just return if contestant went bust
clear()¶
Clear the stuff from last hand and get ready for next
finish()¶
Not really a turn, just flip the downcard and report finalscore.
getScore()¶
Pick out the best score and return it
hit()¶
Called from player.hit and from dealer.
standing()¶
Just return if contestant is standing - satisfied with cards inhand
upScore()¶
Pick out the best score of the only the face up cards and return it
wentBust()¶
Set the bust status to true and show a message saying went bust
class bkJplayers.Dealer(win, deck, xmin, ymin, xmax, ymax)¶

Functions specific to the dealer (different than player). Theinit function from Contestant does what’s needed for this class.

deal()¶
The initial deal to dealer - one face up, one face down
play()¶
Dealer finishes the hand after player is finished
setStand()¶
Set the stand status to true and display a message that the dealerstands.
turn()¶
Dealer takes a to turn see what they come up with
class bkJplayers.Player(win, deck, xmin, ymin, xmax, ymax)¶

Functions specific to the player (different than dealer) Theinit function from Contestant does what’s needed for thisclass.

deal()¶
The initial deal to player - one card face up, one down
bkJhand.py
Each player’s hand of cards for a Blackjack game
class bkJhand.Twohands(win, xmin, xmax, ycenter, yscore)¶

One hand for all cards and another hand for just the face-up cards.The later is just for calculating the points of the face up cards, whichyour opponent can see. This class is a wrapper around the Hand class sothat the player classes only have to talk to one Hand class.

In hindsight, this may not have been the best approach. I could havechanged the Hand class to make note of the value of the face down card andkept two scores and two scoreBoxes.

clear()¶
Reset the Hand and remove displayed cards for a new hand
dealdown(card)¶
Deal a card Face down
dealup(card)¶
Deal a card Face up
flip2nd()¶
Flip the 2nd, face down, card over. Remove the face up score andshow the score for all cards.
score()¶
Return the list of scores
scoreBoxAMesg(mesg)¶
Display a message in the all card score box
scoreBoxBMesg(mesg)¶
Display a message in the face up card score box
setDealer()¶
Dealer calls this, just so we know this is the dealer’s hand. Itsets a Boolean variable Only difference is how the face down card isshown.
showScore()¶
Display the score for all cards
upScore()¶
Return the list of scores
upShowScore()¶
Display the score for the face up cards
class bkJhand.Hand(win, xmin, xmax, ycenter, yscore, label)¶

Manage the set of card in the hand. Keeps a list of the cards,shows them on the screen and counts the points. Also has functions todisplay the points for the hand, but the player and dealerdetermine when to do that.

clear()¶
Clear the screen and the hand of cards in prep for next hand
dealdown(card)¶
Add a dealt card face down
dealup(card)¶
Add a dealt card face up
flip2nd()¶
The second card is first dealt face down. At the last turnof each dealer and player, it is turned up.
hideScoreBox()¶
Clear the score box message area
score()¶
Return the set of current non-bust scores. If best is 21,return it as the only item in the score list.
scoreBoxMesg(mesg)¶
Display some special message in the score box
showScore()¶
Display the current score for the hand
cards.py
Card and Deck classes for games played with playing cards -Blackjack being the first...
class cards.Card((rank, suit))¶

Playing card for games such as Blackjack - this is the non-drawn card,DrawnCard class extends this class to add drawing the card on thescreen.

Note: Deck’s deal method returns a tuple of form (rank, suit),and Card’s __init__ method takes the same tuple to create a deck,so they work nicely together, but may require special care if not usedtogether. This makes it easier to create the type of card needed.

BJValue()¶
Returns how may points the card is worth in Blackjack.Returns Ace as 1 point, so counting it as either1 or 11 point must be done some where else
draw(center)¶
Just a stub - it does nothing
drawDown(center)¶
Just a stub - it does nothing
flip()¶
Just a stub - it does nothing
getImageDown()¶
Just a stub - it does nothing
getImageName()¶
Just a stub - it does nothing
getRank()¶
Returns in range 1 (Ace) to 13 (King) for card
getSuit()¶
Returns suit (one of ‘d’, ‘c’, ‘h’, ‘s’) for diamonds,clubs, hearts or spades
moveTo(new)¶
Just a stub - it does nothing
undraw()¶
Just a stub - it does nothing
class cards.DrawnCard(win, cardTuple)¶

Playing card for games such as Blackjack - this is for a card objectthat we want to display in a graphics window. It extends the genericcard.

draw(center)¶
Show the image of the card face up
drawDown(center)¶
Show the image of the card face down
flip()¶
Flip the card over. Switch the image between face up and facedown

Play Blackjack For Free

getImageDown()¶
Returns file name for face down image of the card
getImageName()¶
Returns file name for face up image of the card
moveTo(new)¶
Move the card image to a new location. new is a Point object
undraw()¶
Remove the image of the card from graphics window
class cards.CoveredCard(win, cardTuple)¶

This is a specialized card for games such as Blackjack.Like the DrawnCard, it draws an image of the card, but in the case whencard is supposed to be face down, instead of displaying the image forthe back of the card, it first shows the card face up, and then coversmost of the card with a face down image. This is to communicate to theuser that to their opponents, this is a face down card, but it lets themsee enough of the card to know what it is. It extends the DrawnCardClass. While self.face = True (it is face up) it behaves the same asDrawnCard class.

drawDown(center)¶
Show the image of the card covered by a face down card
flip()¶
Flip the card over. Switch the image between face up and facedown
moveTo(new)¶
Move the card image to a new location. new is a Point object
undraw()¶
Remove the image of the card from graphics window
class cards.Deck¶

A deck of 52 playing cards

cardsLeft()¶
Returns how many undealt cards are left in the deck
deal()¶
Deal one card from the deck. Returns a card object.
shuffle()¶
Just what the function name says
cards.unitTest()¶
Some unit testing code for deck and card classes: Card, DrawnCard and Covered Card
textboxes.py
Part of the Blackjack game. Displays the scoreboard and other messages.
class textboxes.ScoreBox(win, center, begin=None)¶

Just a Text area to keep the score in

setScore(score=None)¶
Show the score with the begin message, call with no parameters toremove the score from the screen
class textboxes.MessageBox(win, center)¶

Just a Text area to display a message in

setColor(color)¶
Change the text color
setMsg(msg=None)¶
Show a message, call with no parameters toremove the score from the screen
guiengine.py
A simple mouse click event catching engine with button callbacks and a simple Button widget.
class guiengine.MouseTrap(win)¶

A class to catch mouse click events on buttons and run theappropriate call back function. This provides a framework forasychronous programming using the event catching and call backmodel.

Buttons are registered with the trap engine so that they arewatched for a mouse click over the button.

Important notes about call back functions:

1) If the class extends the Button class and has a function namedrun(), then it will be the call back. That implies that the classhas only one button. Probably more useful is to use the baseButton class or an extension of it and use the Button classessetRun() function to specifiy a function of choice for the callback. (See the Button class for more on info.)

2) Rather using a parameter to init or other special function tospecify which Button is the Quit button, the mechanism used isthat a call back function can cause the MousTrap engine to exit(and presumably, the rest of the program) by having the functionreturn a Boolean False value. Thus any function that wants theprogram to continue watching for mouse clicks needs to returnTrue. This can allow more than one exit path for error handlingand avoids needing to register a button as a Quit button. Butdon’t forget in coding the call back function to return thedesired value of True or False.

registerButton(button)¶
Register the button class, each button needs to have a run() methodand a clicked() boolean method (part of the base Button class).
run()¶
Run the event catcher waiting for mouse clicks.
class guiengine.Button(win, center, width, height, label)¶

A button is a labeled rectangle in a window.It is activated or deactivated with the activate()and deactivate() methods. The clicked(p) methodreturns true if the button is active and p is inside it.

activate()¶
Sets this button to ‘active’.
clicked(p)¶
Returns true if button active and p is inside
deactivate()¶
Sets this button to ‘inactive’.
getLabel()¶
Returns the label string of this button.

Blackjack Game Python Code

run()¶
The default event handler. It either runs the handler functionset in setRun() or it raises an exception.
setRun(function)¶
set a function to be the mouse click event handler
setUp(win, center, width, height, label)¶
set most of the Button data - not in init to make easierfor child class methods inheriting from Button.If called from child class with own run(), set self.runDef
graphics.py
Simple object oriented graphics library for Introduction to Programmingusing Python by John Zelle, Ph.D.