A Python Blackjack Game. 14 Years Ago Ene Uran. 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. Python blackjack. GitHub Gist: instantly share code, notes, and snippets.
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.
Controller or coordinator of the game. All buttons belongto this class.
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.
Functions specific to the dealer (different than player). Theinit function from Contestant does what’s needed for this class.
Functions specific to the player (different than dealer) Theinit function from Contestant does what’s needed for thisclass.
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.
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.
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.
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.
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.
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.
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.
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.
Class based Blackjack game
''
importrandomas r
RULES =''--------------------------------
dealer gives 2 cards to player and himself, only one visible for himself
card values are number value, face cards = 10, ace = 1 or 11
player can hit or stand or split
only split if same value card
split gives player 2 hands, original bet must be matched on 2nd hand
can split 3 times on all cards but aces
can only split once if aces, not blackjack if 10 on both
if pointvalue > 21 = BUST
dealer must hit if <= 16
dealer must stay if >= 17
If player beats dealer, gets double money back, or 1:1
If player get blackjack on first turn, 1.5:1
Player can place double original bet, but must take only one more card
These rules are not very professional will fix later.. were just notes for me to write the game :D
deck =['Ace of Spades','2 of Spades','3 of Spades','4 of Spades','5 of Spades','6 of Spades',
'7 of Spades','8 of Spades','9 of Spades','10 of Spades','Jack of Spades','Queen of Spades',
'King of Spades','Ace of Hearts','2 of Hearts','3 of Hearts','4 of Hearts','5 of Hearts',
'6 of Hearts','7 of Hearts','8 of Hearts','9 of Hearts','10 of Hearts','Jack of Hearts',
'Queen of Hearts','King of Hearts','Ace of Clubs','2 of Clubs','3 of Clubs','4 of Clubs',
'5 of Clubs','6 of Clubs','7 of Clubs','8 of Clubs','9 of Clubs','10 of Clubs',
'Jack of Clubs','Queen of Clubs','King of Clubs','Ace of Diamonds','2 of Diamonds','3 of Diamonds',
'4 of Diamonds','5 of Diamonds','6 of Diamonds','7 of Diamonds','8 of Diamonds','9 of Diamonds',
'10 of Diamonds','Jack of Diamonds','Queen of Diamonds','King of Diamonds'
def checkpoints(hand_list):
pointdict ={
'Ace of Spades':'ace','2 of Spades':2,'3 of Spades':3,'4 of Spades':4,'5 of Spades':5,'6 of Spades':6,'7 of Spades':7,'8 of Spades':8,'9 of Spades':9,'10 of Spades':10,'Jack of Spades':10,'Queen of Spades':10,'King of Spades':10,
'Ace of Hearts':'ace','2 of Hearts':2,'3 of Hearts':3,'4 of Hearts':4,'5 of Hearts':5,'6 of Hearts':6,'7 of Hearts':7,'8 of Hearts':8,'9 of Hearts':9,'10 of Hearts':10,'Jack of Hearts':10,'Queen of Hearts':10,'King of Hearts':10,
'Ace of Clubs':'ace','2 of Clubs':2,'3 of Clubs':3,'4 of Clubs':4,'5 of Clubs':5,'6 of Clubs':6,'7 of Clubs':7,'8 of Clubs':8,'9 of Clubs':9,'10 of Clubs':10,'Jack of Clubs':10,'Queen of Clubs':10,'King of Clubs':10,
'Ace of Diamonds':'ace','2 of Diamonds':2,'3 of Diamonds':3,'4 of Diamonds':4,'5 of Diamonds':5,'6 of Diamonds':6,'7 of Diamonds':7,'8 of Diamonds':8,'9 of Diamonds':9,'10 of Diamonds':10,'Jack of Diamonds':10,'Queen of Diamonds':10,'King of Diamonds':10
##############################################
for card in hand_list:
possible_points =[]
if item 'ace':
possible_points.append(1)
else:
possible_points[i] +=1
possible_points.append(possible_points[-1]+10)
for item in points:
try:
possible_points[i] += item
continue
iflen(possible_points)0:
else:
possible_points.sort()
class BlackjackGame:
print'Starting up Game'
deck =['Ace of Spades','2 of Spades','3 of Spades','4 of Spades','5 of Spades','6 of Spades',
'7 of Spades','8 of Spades','9 of Spades','10 of Spades','Jack of Spades','Queen of Spades',
'King of Spades','Ace of Hearts','2 of Hearts','3 of Hearts','4 of Hearts','5 of Hearts',
'6 of Hearts','7 of Hearts','8 of Hearts','9 of Hearts','10 of Hearts','Jack of Hearts',
'Queen of Hearts','King of Hearts','Ace of Clubs','2 of Clubs','3 of Clubs','4 of Clubs',
'5 of Clubs','6 of Clubs','7 of Clubs','8 of Clubs','9 of Clubs','10 of Clubs',
'Jack of Clubs','Queen of Clubs','King of Clubs','Ace of Diamonds','2 of Diamonds','3 of Diamonds',
'4 of Diamonds','5 of Diamonds','6 of Diamonds','7 of Diamonds','8 of Diamonds','9 of Diamonds',
'10 of Diamonds','Jack of Diamonds','Queen of Diamonds','King of Diamonds'
temp =raw_input('How many decks does this dealer get? ')
temp =raw_input('Invalid input.nHow many decks does this dealer get? ')
print'Too many.. not realistic enough.. i'll just go with 5..'
self.dealerdeck= deck * 5
whileTrue:
temp =raw_input('Would you like to play a round?(y/n) ')
while temp.lower()notin'yn'or temp.lower()notin'ny':
temp =raw_input('Invalid InputnWould you like to play a round?(y/n) ')
returnFalse
returnTrue
iflen(self.dealerdeck)>10:
dealerhand =[]
z = r.randint(0,len(self.dealerdeck)-1)
hand.append(y)
z = r.randint(0,len(self.dealerdeck)-1)
dealerhand.append(y)
else:
self.dealerdeck=self.getdealerdeck()
def play(self):
tempyn =raw_input('Would you like to read the rules?(y/n) ').lower()
tempyn =raw_input('Invalid inputnWould you like to read the rules?(y/n) ').lower()
print RULES
tempyn =raw_input('Would you like to play a hand?(y/n) ').lower()
tempyn =raw_input('Invalid inputnWould you like to play a hand?(y/n) ').lower()
self.dealerdeck,self.playerhand,self.dealerhand=self.gethand()
else: break
def playround(self):
print'Your hand has:',self.playerhand[0],'and',self.playerhand[1]
print'You can see the dealer has:',self.dealerhand[1]
if21in checkpoints(self.playerhand):
#bet = bet + bet*1.5
elif21in checkpoints(self.dealerhand):
print'Dealer gets blackjack'
splitpossible =False
x ='You may; hit(H), stand(S), doubledown(DD), surrender(I'm French)'
x +=', split(split)'
print x
playerchoice =raw_input('Please choose: ').lower()
while playerchoice notin['stand','hit','doubledown','surrender','h','s','dd','i'm french']:
playerchoice =raw_input('InvalidnPlease choose again: ').lower()
whileTrue:
self.playerhand.append(self.dealerdeck.pop(r.randint(0,len(self.dealerdeck)-1)))
if checkpoints(self.playerhand)[0]>21:
elif checkpoints(self.playerhand)[0]21:
print'Blackjack, let's see if the dealer also gets it..'
while temp !='h'and temp !='s':
temp =raw_input('Invalid..nHit or Stay? (h/s)').lower()
break
print'Bust - Dealer wins'
print'Dealer has',self.dealerhand[0]
self.dealerhand.append(self.dealerdeck.pop(r.randint(0,len(self.dealerdeck)-1)))
if checkpoints(self.dealerhand)[0]>21:
else:
playerpoints = checkpoints(self.playerhand)
for x inrange(len(playerpoints)):
playerpoints.pop(x)
if dealerpoints[x]>21:
if playerpoints[-1] dealerpoints[-1]:
elif dealerpoints[-1]> playerpoints[-1]:
else:
if splitpossible True:
while playerchoice notin['stand','hit','doubledown','surrender','h','s','dd','i'm french','split']:
playerchoice =raw_input('InvalidnPlease choose again: ')
------------------------------------
----------------------------------
| ------------- |
| ------------- |
| Dmaster_773 |
| :) |
/
''
lol = BlackjackGame()
lol.getdealerdeck()