tangled
alpha
login
or
join now
thecoded.prof
/
CMU
0
fork
atom
CMU Coding Bootcamp
0
fork
atom
overview
issues
pulls
pipelines
feat: oct 6 add blackjack
thecoded.prof
5 months ago
56e6efb5
8337c5f1
verified
This commit was signed with the committer's
known signature
.
thecoded.prof
SSH Key Fingerprint:
SHA256:ePn0u8NlJyz3J4Zl9MHOYW3f4XKoi5K1I4j53bwpG0U=
+137
1 changed file
expand all
collapse all
unified
split
python
oct6
blackjack
main.py
+137
python/oct6/blackjack/main.py
···
1
1
+
from cmu_graphics.cmu_graphics import AppWrapper
2
2
+
from cmu_graphics import *
3
3
+
import random
4
4
+
from types import SimpleNamespace
5
5
+
from sys import exit
6
6
+
from calendar import c
7
7
+
8
8
+
9
9
+
def onAppStart(app_inst: AppWrapper):
10
10
+
app_inst.status = ""
11
11
+
app_inst.deck = makeRandomDeck()
12
12
+
app_inst.playerHand = [app_inst.deck.pop(), app_inst.deck.pop()]
13
13
+
app_inst.dealerHand = [app_inst.deck.pop(), app_inst.deck.pop()]
14
14
+
app_inst.dealerHand[0].hidden = True
15
15
+
app_inst.playerDrawing = True
16
16
+
17
17
+
def getScore(hand):
18
18
+
score = 0
19
19
+
aces = 0
20
20
+
for card in hand:
21
21
+
if card.rank == 'Ace':
22
22
+
aces += 1
23
23
+
score += 11
24
24
+
elif card.rank in ['Jack', 'Queen', 'King']:
25
25
+
score += 10
26
26
+
else:
27
27
+
score += int(card.rank)
28
28
+
while score > 21 and aces > 0:
29
29
+
score -= 10
30
30
+
aces -= 1
31
31
+
return score
32
32
+
33
33
+
34
34
+
def onKeyPress(app_inst: AppWrapper, key):
35
35
+
if key == 'h' and app_inst.playerDrawing:
36
36
+
app_inst.playerHand.append(app_inst.deck.pop())
37
37
+
elif key == 's':
38
38
+
app_inst.playerDrawing = False
39
39
+
app_inst.dealerHand[0].hidden = False
40
40
+
app_inst.dealerHand[1].hidden = False
41
41
+
elif key == 'r':
42
42
+
onAppStart(app_inst)
43
43
+
return
44
44
+
elif key == 'q':
45
45
+
exit()
46
46
+
47
47
+
dealer_score = getScore(app_inst.dealerHand)
48
48
+
player_score = getScore(app_inst.playerHand)
49
49
+
if player_score > 21:
50
50
+
app_inst.status = 'loss'
51
51
+
app_inst.playerDrawing = False
52
52
+
return
53
53
+
54
54
+
if app_inst.playerDrawing:
55
55
+
if dealer_score < 17:
56
56
+
app_inst.dealerHand.append(app_inst.deck.pop())
57
57
+
else:
58
58
+
while dealer_score < 17:
59
59
+
app_inst.dealerHand.append(app_inst.deck.pop())
60
60
+
if dealer_score > 21:
61
61
+
app_inst.status = 'win'
62
62
+
app_inst.playerDrawing = False
63
63
+
return
64
64
+
if not app_inst.playerDrawing and dealer_score >= 17:
65
65
+
if player_score > dealer_score:
66
66
+
app_inst.status = 'win'
67
67
+
elif player_score < dealer_score:
68
68
+
app_inst.status = 'loss'
69
69
+
else:
70
70
+
if len(app_inst.dealerHand) < len(app_inst.playerHand):
71
71
+
app_inst.status = 'win'
72
72
+
else:
73
73
+
app_inst.status = 'loss'
74
74
+
75
75
+
def redrawAll(app_inst: AppWrapper):
76
76
+
print(app_inst.status, getScore(app_inst.dealerHand), getScore(app_inst.playerHand))
77
77
+
match app_inst.status:
78
78
+
case "win":
79
79
+
app_inst.dealerHand[0].hidden = False
80
80
+
drawLabel(f"Dealer's score: {getScore(app_inst.dealerHand)}", app_inst.width//2, 30)
81
81
+
drawRect(app_inst.width//2-100, app_inst.height//2-50, 200, 100, fill="lightGreen")
82
82
+
drawLabel("You Win!", app_inst.width//2, app_inst.height//2)
83
83
+
case "loss":
84
84
+
app_inst.dealerHand[0].hidden = False
85
85
+
drawLabel(f"Dealer's score: {getScore(app_inst.dealerHand)}", app_inst.width//2, 30)
86
86
+
drawRect(app_inst.width//2-100, app_inst.height//2-50, 200, 100, fill="pink")
87
87
+
drawLabel("You Lose!", app_inst.width//2, app_inst.height//2, fill="red")
88
88
+
case _:
89
89
+
pass
90
90
+
91
91
+
dealer_card_count = len(app_inst.dealerHand)
92
92
+
for i, card in enumerate(app_inst.dealerHand):
93
93
+
drawCard(app_inst, card, app_inst.width//2+25*dealer_card_count//2-i*37.5, 60+i*2)
94
94
+
95
95
+
drawLabel(f"Score: {getScore(app_inst.playerHand)}", app_inst.width//2, app_inst.height*3//4-20, size=18)
96
96
+
card_count = len(app_inst.playerHand)
97
97
+
for i, card in enumerate(app_inst.playerHand):
98
98
+
drawCard(app_inst, card, app_inst.width//2+25*card_count//2-i*37.5, app_inst.height-60+i*2)
99
99
+
pass
100
100
+
101
101
+
def getSuitLabelAndColor(suit):
102
102
+
if suit[0] == 'C': return '♣', 'black'
103
103
+
elif suit[0] == 'D': return '♦', 'red'
104
104
+
elif suit[0] == 'H': return '♥', 'red'
105
105
+
else: return '♠', 'black'
106
106
+
107
107
+
def makeRandomDeck():
108
108
+
# first make a sorted deck
109
109
+
ranks = 'Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King'.split(',')
110
110
+
suits = 'Clubs,Diamonds,Hearts,Spades'.split(',')
111
111
+
deck = [makeCard(rank, suit) for rank in ranks for suit in suits]
112
112
+
# now shuffle and return the deck
113
113
+
random.shuffle(deck)
114
114
+
print([card.rank for card in deck])
115
115
+
return deck
116
116
+
117
117
+
def makeCard(rank, suit):
118
118
+
card = SimpleNamespace()
119
119
+
card.rank = rank
120
120
+
card.suit = suit
121
121
+
card.hidden = False
122
122
+
return card
123
123
+
124
124
+
def drawCard(app_inst: AppWrapper, card, x, y):
125
125
+
label, color = getSuitLabelAndColor(card.suit)
126
126
+
drawRect(x-25, y-37.5, 50, 75, fill='white', border='black')
127
127
+
if card.hidden:
128
128
+
drawRect(x-25, y-37.5, 50, 75, fill='gray', border='black')
129
129
+
else:
130
130
+
drawLabel(f'{label}', x-15, y-27.5, fill=color, size=20)
131
131
+
drawLabel(f'{label}', x+15, y+27.5, fill=color, rotateAngle=180, size=20)
132
132
+
drawLabel(f'{card.rank[0] if not card.rank.isdigit() else card.rank}', x, y, fill=color, size=20)
133
133
+
134
134
+
def main():
135
135
+
runApp()
136
136
+
137
137
+
main()