-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
168 lines (144 loc) · 4.99 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import itertools
import random
import time
from tkinter import *
suits = ["Diamonds", "Hearts", "Spades", "Clubs"]
faces = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
class Card:
def __init__(self, suit, face):
self.suit = suit
self.face = face
def __repr__(self):
return f"{self.face} of {self.suit}"
deck = [Card(suit,face) for suit, face in itertools.product(suits, faces)]
def dealCard(card_deck_list, hand_list):
hand_list.append(card_deck_list.pop(0))
def updateHandValue(hand, hand_value):
num = 0
for x in hand:
card = hand[num]
card_value = getCardValue(card)
if card_value == "A":
if hand_value + 11 > 21:
hand_value = hand_value + 1
else:
hand_value = hand_value + 11
else:
hand_value = hand_value + card_value
num += 1
return hand_value
def getCardValue(card):
val = 0
try:
int(card.face)
except ValueError:
if card.face == "A":
val = "A"
else:
val = 10
else:
val = int(card.face)
return val
root = Tk()
root.title("Blackjack")
root.geometry("650x400")
#root.resizable(False, False)
root.config(bg='#63B76C')
lb1 = Label(root, text="Player Cards:", font=("Source Code Pro", 15), bg='#63B76C')
lb1.place(x=10, y=20)
lb2 = Label(root, text="Player Score:", font=("Source Code Pro", 15), bg='#63B76C')
lb2.place(x=10, y=50)
lb3 = Label(root, text="Dealer Cards:", font=("Source Code Pro", 15), bg='#63B76C')
lb3.place(x=10, y=110)
lb4 = Label(root, text="Dealer Score:", font=("Source Code Pro", 15), bg='#63B76C')
lb4.place(x=10, y=140)
lb5 = Label(root, text="Let's have some fun!", font=("Source Code Pro", 15), bg='#63B76C')
lb5.place(x=10, y=170)
playerHand = []
dealerHand = []
playerHandValue = 0
dealerHandValue = 0
def checkPoints():
global isDealerTurn
global isPlayerTurn
global playerHandValue
if playerHandValue > 21:
lb1.config(text=f"Player Cards: {playerHand}")
lb2.config(text=f"Player Score: {playerHandValue} BUST!")
playerHandValue = -1
dealerTurn()
elif playerHandValue == 21:
lb1.config(text=f"Player Cards: {playerHand}")
lb2.config(text=f"Player Score: {playerHandValue} BLACKJACK!")
dealerTurn()
def hit():
global playerHandValue
dealCard(deck, playerHand)
playerHandValue = 0
playerHandValue = updateHandValue(playerHand, playerHandValue)
lb1.config(text=f"Player Cards: {playerHand}")
lb2.config(text=f"Player Score: {playerHandValue}")
checkPoints()
def stand():
dealerTurn()
def gameLogic():
global playerHandValue
global playerHand
global dealerHand
global dealerHandValue
global isDealerTurn
global isPlayerTurn
global deck
btn2.configure(state=NORMAL)
btn3.configure(state=NORMAL)
deck = [Card(suit,face) for suit, face in itertools.product(suits, faces)]
random.shuffle(deck)
playerHand = []
dealerHand = []
playerHandValue = 0
dealerHandValue = 0
dealCard(deck, playerHand)
dealCard(deck, playerHand)
dealCard(deck, dealerHand)
dealCard(deck, dealerHand)
playerHandValue = updateHandValue(playerHand, playerHandValue)
dealerHandValue = updateHandValue(dealerHand, dealerHandValue)
lb1.config(text=f"Player Cards: {playerHand}")
lb2.config(text=f"Player Score: {playerHandValue}")
lb3.config(text=f"Dealer Cards: {dealerHand[1]}")
lb4.config(text=f"Dealer Score: {getCardValue(dealerHand[1])}")
lb5.config(text=f"Let's have some fun!")
checkPoints()
def endGame():
if dealerHandValue == playerHandValue:
lb5.config(text=f"It's a Tie!")
elif dealerHandValue > playerHandValue:
lb5.config(text=f"You lose! Sorry!")
else:
lb5.config(text=f"You win! Congratulations!")
def dealerTurn ():
global dealerHand
global dealerHandValue
btn2.configure(state=DISABLED)
btn3.configure(state=DISABLED)
lb3.config(text=f"Dealer Cards: {dealerHand}")
lb4.config(text=f"Dealer Score: {dealerHandValue}")
while dealerHandValue < 17:
dealCard(deck, dealerHand)
dealerHandValue = 0
dealerHandValue = updateHandValue(dealerHand, dealerHandValue)
time.sleep(1)
lb3.config(text=f"Dealer Cards: {dealerHand}")
lb4.config(text=f"Dealer Score: {dealerHandValue}")
if dealerHandValue > 21:
lb3.config(text=f"Dealer Cards: {dealerHand}")
lb4.config(text=f"Dealer Score: {dealerHandValue} BUST!")
dealerHandValue = -1
endGame()
btn = Button(root, text="Play Round", font=("Source Code Pro", 15), bd=5, bg='#8B8386', fg='White', command=gameLogic)
btn.place(x=115, y=210)
btn2 = Button(root, text="Hit", font=("Source Code Pro", 15), bd=5, bg='#8B8386', fg='White', command=hit)
btn2.place(x=115, y=260)
btn3 = Button(root, text="Stand", font=("Source Code Pro", 15), bd=5, bg='#8B8386', fg='White', command=stand)
btn3.place(x=115, y=310)
root.mainloop()