-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
165 lines (120 loc) · 4.84 KB
/
main.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
import tkinter as tk
from tkinter import ttk
from Cards.Deck import Deck
from Cards.Hand import Hand
from PIL import Image, ImageTk
from time import sleep
class Blackjack:
def __init__(self, root, otherPlayers= 0, numDecks= 1):
self.otherPlayers= otherPlayers
self.numDecks= numDecks
self.otherPlayerHands= [ Hand(root) for i in range(otherPlayers)]
self.playerHand= Hand(root)
self.dealerHand= Hand(root)
self.deck= Deck(numDecks)
self.root= root
self.player_frame= self.playerHand.mainFrame
self.player_frame.place(relx=0, rely=0.6, relwidth=0.8, relheight=0.3)
self.dealer_frame= self.dealerHand.mainFrame
self.dealer_frame.place(relx=0, rely=0, relwidth=0.8, relheight=0.3)
self.countsVisible= False
self.countButton= tk.Button(root, text= "Show Counts", command= self.show_counts)
self.countButton.place(relx=0.8, rely=0, relwidth=0.2, relheight=0.2)
self.hitButton= ttk.Button(root, text="Hit", command=self.hit)
self.hitButton["state"] = "disabled"
self.hitButton.place(relx=0, rely=0.9, relwidth=0.1, relheight=0.1)
self.standButton= ttk.Button(root, text="Stand", command=self.stand)
self.standButton.place(relx=0.1, rely=0.9, relwidth=0.1, relheight=0.1)
self.dealButton= ttk.Button(root, text= "Deal", command=self.deal)
self.dealButton.place(relx=0.2, rely=0.9, relwidth=0.1, relheight=0.1)
self.shuffleButton= tk.Button(root, text="Shuffle", command=self.shuffle)
self.shuffleButton.place(relx=0.3, rely=0.9, relwidth=0.1, relheight=0.1)
self.photo_images= []
def shuffle(self):
self.playerHand.clear()
self.dealerHand.clear()
self.deck.initialize_new_cards()
self.updateCountsLabel()
def updateCountsLabel(self):
if self.countsVisible:
runningCount= self.deck.running_count
for card in self.dealerHand.cards:
if card.show_back:
runningCount -= card.getHighLow()
trueCount= runningCount / self.deck.getRemainingDecks()
self.countButton.config(text=f"Running Count: {runningCount}\nTrue Count: {trueCount}")
else:
self.countButton.config(text="Show Counts")
def show_counts(self):
self.countsVisible = not self.countsVisible
self.updateCountsLabel()
def deal(self):
self.photo_images.clear()
self.dealerHand.clear()
self.playerHand.clear()
#deal two to main player first
for i in range(2):
newCard= self.deck.draw()
self.playerHand.append(newCard)
#deal two each extra player
#deal one face down to dealer
newCard= self.deck.draw()
newCard.show_back= True
self.dealerHand.append(newCard)
#deal one face up to dealer
newCard= self.deck.draw()
self.dealerHand.append(newCard)
if self.playerHand.calculateTotal() == 21:
if self.dealerHand.calculateTotal() == 21:
self.push()
else:
self.playerBlackjack()
self.hitButton['state']= "enabled"
self.standButton['state'] = 'enabled'
self.updateCountsLabel()
def hit(self):
newCard= self.deck.draw()
self.playerHand.append(newCard)
if self.playerHand.calculateTotal() > 21:
print("you bust lmao")
self.houseWins()
self.updateCountsLabel()
def dealerHit(self):
newCard= self.deck.draw()
self.dealerHand.append(newCard)
def playerWin(self):
print("Player wins!")
def playerBlackjack(self):
print("Blackjack!")
def houseWins(self):
print("House wins")
def push(self):
print("push")
def stand(self):
self.dealerHand.makeFirstCardFaceUp()
self.hitButton['state']= "disabled"
self.standButton['state']= 'disabled'
while self.dealerHand.calculateTotal() < 17:
self.dealerHit()
playerTotal= self.playerHand.calculateTotal()
dealerTotal= self.dealerHand.calculateTotal()
if (dealerTotal >21):
print("dealer Bust!")
self.playerWin()
elif playerTotal > dealerTotal:
print("higher total!")
self.playerWin()
elif playerTotal < dealerTotal:
self.houseWins()
elif playerTotal == dealerTotal:
self.push()
self.updateCountsLabel()
def main():
root = tk.Tk()
root.title("BlackJack!")
main_frame = tk.Frame(root, width=500, height=500, bg='green')
main_frame.pack(fill=tk.BOTH, expand=True)
bj= Blackjack(main_frame)
root.mainloop()
if __name__ == "__main__":
main()