Yea, I don't mind posting my version. I played with it some. Didn't make the GUI prettier, because I hate tweaking GUI's, but I did try to streamline the code a little.
I had to make a CardPlayer class to handle a blackjack player's hand contents (Q, K, 8, etc) and hand sum. This allows you to more easily expand this code to have more than 1 player. (Currently the Blackjack class cannot handle more than Dealer vs one Player, however).
I also had to change the algorithm for summing up a player's hand. The way you were doing it wasn't working consistently. My way eliminated the "values" dictionary because it was just too much hassle to check values repeatedly.
import random
from tkinter import *
deck = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]
class CardPlayer:
global deck
def __init__(self):
self.hand = []
self.hand_sum = 0
def draw_card(self):
card = random.choice(deck)
self.hand.append(card)
self.hand.sort()
self._update_hand_value()
def _update_hand_value(self):
value =0
aces = 0
for card in self.hand:
if str.isnumeric(card):
value += int(card)
else:
if card == "A":
aces += 1
continue
value += 10
for ace in range(aces):
if (value + 11)>21:
value += 1
else:
value += 11
self.hand_sum = value
class Blackjack:
def __init__(self):
self.__game = Tk()
self.__game.geometry("700x300")
# background
#self.__table = PhotoImage(file=".....")
self.__table= Label(self.__game)
#self.__table.config(image = self.__table)
self.__table.place(x=0,y=0, relwidth=1, relheight=1)
# How to end the game
self.__close_instructions = Label(self.__game, text=
"Press 'q' to quit or 'a' to start a new game after this one ends")
self.__close_instructions.grid(row=0, column=0, sticky=W)
self.__game.config(cursor="tcross")
self.__game.grid_rowconfigure(0, minsize=100)
self.__game.grid_columnconfigure(0, minsize = 100)
# Pressing q:closes window
self.__game.bind("q", lambda q: self.__game.destroy())
# Print your hand
self.__post_cards = Label(self.__game)
self.__post_cards.grid(row=2, column=1)
# Print dealer’s cards
self.__dealer_cards = Label(self.__game)
self.__dealer_cards.grid(row=2, column=2)
# Print your score
self.__player_score = Label(self.__game)
self.__player_score.grid(row=3, column=1)
# Prints for each game
self.__main_display = Label(self.__game)
self.__main_display.grid()
self.__main_display.config(text="Let's play Blackjack!")
self.__main_display.config(text="Welcome to the table!")
self.__start_button = Button(self.__game)
self.__start_button.config(text = "Deal", command=self.begin_game)
self.__start_button.grid()
self.__game.mainloop()
def begin_game(self):
self.__start_button.config(state=DISABLED)
self.__game.unbind("a") #unbind 'a' press so player must play
# dealer draws
self.Dealer = CardPlayer()
self.Dealer.draw_card()
self.Dealer.draw_card()
# the player draws
self.Player1 = CardPlayer()
self.Player1.draw_card()
self.Player1.draw_card()
self.__post_cards.config(text="Your cards: %s" % " ".join(self.Player1.hand))
self.__player_score.config(text="Your score: %d" % self.Player1.hand_sum)
self.__dealer_cards.config(text="The dealer shows: %s " % (self.Dealer.hand[0]))
self.__main_display.config(text="hit or stay?")
# This button chooses hit
self.__hitbutton = Button(self.__game)
self.__hitbutton.config(text="Hit", command=lambda : self.hit())
self.__hitbutton.grid(row=3, column=2)
# This button chooses stay
self.__staybutton = Button(self.__game)
self.__staybutton.config(text="Stay", command=lambda : self.stay())
self.__staybutton.grid(row=3, column=3)
if self.Player1.hand_sum == 21:
self.__main_display.config(text="BLACKJACK!!")
self.__hitbutton.destroy()
self.__staybutton.destroy()
self.play_again()
def hit(self):
self.Player1.draw_card()
self.__post_cards.config(text="Your cards: %s" % " ".join(self.Player1.hand))
self.__player_score.config(text="Your score: %d" % self.Player1.hand_sum)
if self.Player1.hand_sum==21:
self.__main_display.config(text="Great Job!")
self.__main_display.config(text="Let's see what the Dealer draws...")
self.__hitbutton.destroy()
self.__staybutton.destroy()
self.dealer_hit()
elif self.Player1.hand_sum>21:
self.__main_display.config(text="Bust!! You lost this hand.")
self.__hitbutton.destroy()
self.__staybutton.destroy()
self.play_again()
def stay(self):
self.__hitbutton.destroy()
self.__staybutton.destroy()
self.dealer_hit()
def dealer_hit(self):
self.__dealer_cards.config(
text="The dealer shows: %s which totals %d" % (" ".join(self.Dealer.hand), self.Dealer.hand_sum))
while self.Dealer.hand_sum<16:
if self.Dealer.hand_sum<22 and self.Dealer.hand_sum<16:
self.__main_display.config(text="Dealer must draw...")
self.Dealer.draw_card()
self.__dealer_cards.config(text="The dealer has: %s which totals %d" % (" ".join(self.Dealer.hand), self.Dealer.hand_sum))
if self.Dealer.hand_sum>21:
self.__main_display.config(text="Dealer BUSTS!! You Win!")
elif self.Dealer.hand_sum<self.Player1.hand_sum:
self.__main_display.config(text="You Win!!")
elif self.Dealer.hand_sum==self.Player1.hand_sum:
self.__main_display.config(text="PUSH, you both keep your bets.")
elif self.Dealer.hand_sum>self.Player1.hand_sum:
self.__main_display.config(text="Dealer Wins!!")
self.play_again() #enable 'a' press to start new game
def play_again(self):
self.__game.bind("a", lambda a: self.begin_game())
def main():
blackjack = Blackjack()
main()