#this program explains how to achieve the blackjack game
#the key points to note here are
#pop funtion,using the same code of function to avoid repetition
#load images from file to frame,shuffling of cards,calculating scores
#then use of the global keyword to avoiding shadowing of the global variable inside the function
import tkinter as tkinter
import random
def load_images(card_images):
suits=['heart','club','diamond','spade']
face_cards=['jack','queen','king']
#for each suit retrieve the image for the cards
for suit in suits:
#first the number cards 1 to 10
for card in range(1,11):
name = 'cards/{}_{}.{}'.format(str(card),suit,'png')
image = tkinter.PhotoImage(file = name)
card_images.append((card,image))
#next the Face Cards
for card in face_cards:
name = 'cards/{}_{}.{}'.format(str(card),suit,'png')
image = tkinter.PhotoImage(file = name)
card_images.append((10,image))
def deal_card(frame):
#pop the next card of the top of the deck
next_card= deck.pop(0)
#add the image to the label and display the label
tkinter.Label(frame,image=next_card[1],relief = 'raised').pack(side = 'left')
#now return the cards face value
return next_card
def deal_dealer():
dealer_score= score_hand(dealer_hand)
while 0 < dealer_score <17:
dealer_hand.append(deal_card(dealer_card_frame))
dealer_score= score_hand(dealer_hand)
dealer_score_label.set(dealer_score)
player_score = score_hand(player_hand)
if player_score>21:
resulttext.set('Dealer Wins!')
elif dealer_score > 21 or dealer_score player_score:
resulttext.set('Dealer Wins!')
else:
resulttext.set('Draw!')
# player_score_label.set(player_score)
# if player_score>21:
# resulttext.set ('Dealer Wins')
# def deal_player():
# deal_card(player_card_frame)
def deal_player():
player_hand.append(deal_card(player_card_frame))
player_score= score_hand(player_hand)
player_score_label.set(player_score)
if player_score>21:
resulttext.set ('Dealer Wins')
#below code is used for testing the global,local shadowing concept
# global player_score
# global player_ace
# card_value = deal_card(player_card_frame)[0]
# if card_value ==1 and not player_ace:
# player_ace = True
# card_value = 11
# player_score = player_score + card_value
# # if we would bust,check if there is an ace and subtract
# if player_score>21 and player_ace:
# player_score = player_score - 10
# player_ace = False
# player_score_label.set(player_score)
# if player_score>21:
# resulttext.set("Dealer Wins")
# print(locals())
def score_hand(hand):
#calculate the total score of all cards in the list
# only one can have the value 11,and this will be reduce to 1 if the hand would bust.
score = 0
ace = False
for next_card in hand:
card_value = next_card[0]
if card_value == 1 and not ace:
ace = True
card_value = 11
score = score + card_value
#if we would bust ,check if there is an ace and subtract 10
if score > 21 and ace:
score =score - 10
ace = False
return score
mainwindow = tkinter.Tk()
#setup the screens and frame for the dealer and the player
mainwindow.title("blackjack")
mainwindow.geometry("640x480")
mainwindow.configure(background = "green")
resulttext = tkinter.StringVar()
result = tkinter.Label(mainwindow,textvariable = resulttext)
result.grid(row=0,column=0,columnspan=3)
card_frame= tkinter.Frame(mainwindow,relief = 'sunken',borderwidth = 1,background = 'green')
card_frame.grid(row=1,column=0,sticky = 'ew',columnspan = 3,rowspan=2)
dealer_score_label = tkinter.IntVar()
tkinter.Label(card_frame,text = 'Dealer',background ='green',fg='white').grid(row=0,column=0)
tkinter.Label(card_frame,textvariable = dealer_score_label,background ='green',fg='white').grid(row=1,column=0)
#embedded Frame hold the card images
dealer_card_frame=tkinter.Frame(card_frame,background = 'green')
dealer_card_frame.grid(row=0,column=1,sticky='ew',rowspan=2)
player_score_label = tkinter.IntVar()
tkinter.Label(card_frame,text = 'Player',background ='green',fg='white').grid(row=2,column=0)
tkinter.Label(card_frame,textvariable = player_score_label,background ='green',fg='white').grid(row=3,column=0)
#embedded Frame hold the card images
player_card_frame=tkinter.Frame(card_frame,background = 'green')
player_card_frame.grid(row=2,column=1,sticky='ew',rowspan=2)
button_frame = tkinter.Frame(mainwindow)
button_frame.grid(row=3,column=0,columnspan = 3,sticky = 'w')
dealer_button=tkinter.Button(button_frame,text = 'Dealer',command = deal_dealer)
dealer_button.grid(row=0,column=0)
player_button=tkinter.Button(button_frame,text = 'Player',command = deal_player)
player_button.grid(row=0,column=1)
#load cards
cards = []
load_images(cards)
# print(cards)
# print(id(cards))
#create a new deck of cards and shuffle them
deck = list(cards)
random.shuffle(deck)
#Create the list to store the dealers and players hands
dealer_hand = []
player_hand = []
deal_player()
dealer_hand.append(deal_card(dealer_card_frame))
deal_player()
mainwindow.mainloop()
Saturday, 7 April 2018
Python - function challenge - blackjack game - Day 22(after 1 day)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment