CMU Coding Bootcamp
at main 192 lines 5.5 kB view raw
1from cmu_graphics.cmu_graphics import AppWrapper 2from cmu_graphics import * 3import random 4from types import SimpleNamespace 5from sys import exit 6from calendar import c 7 8 9def onAppStart(app_inst: AppWrapper): 10 app_inst.status = "" 11 app_inst.deck = makeRandomDeck() 12 app_inst.playerHand = [app_inst.deck.pop(), app_inst.deck.pop()] 13 app_inst.dealerHand = [app_inst.deck.pop(), app_inst.deck.pop()] 14 app_inst.dealerHand[0].hidden = True 15 app_inst.playerDrawing = True 16 17 18def getScore(hand): 19 score = 0 20 aces = 0 21 for card in hand: 22 if card.rank == "Ace": 23 aces += 1 24 score += 11 25 elif card.rank in ["Jack", "Queen", "King"]: 26 score += 10 27 else: 28 score += int(card.rank) 29 while score > 21 and aces > 0: 30 score -= 10 31 aces -= 1 32 return score 33 34 35def onKeyPress(app_inst: AppWrapper, key): 36 if key == "h" and app_inst.playerDrawing: 37 app_inst.playerHand.append(app_inst.deck.pop()) 38 elif key == "s": 39 app_inst.playerDrawing = False 40 app_inst.dealerHand[0].hidden = False 41 app_inst.dealerHand[1].hidden = False 42 elif key == "r": 43 onAppStart(app_inst) 44 return 45 elif key == "q": 46 exit() 47 48 dealer_score = getScore(app_inst.dealerHand) 49 player_score = getScore(app_inst.playerHand) 50 if player_score > 21: 51 app_inst.status = "loss" 52 app_inst.playerDrawing = False 53 return 54 55 if app_inst.playerDrawing: 56 if dealer_score < 17: 57 app_inst.dealerHand.append(app_inst.deck.pop()) 58 else: 59 while dealer_score < 17: 60 app_inst.dealerHand.append(app_inst.deck.pop()) 61 if dealer_score > 21: 62 app_inst.status = "win" 63 app_inst.playerDrawing = False 64 return 65 if not app_inst.playerDrawing and dealer_score >= 17: 66 if player_score > dealer_score: 67 app_inst.status = "win" 68 elif player_score < dealer_score: 69 app_inst.status = "loss" 70 else: 71 if len(app_inst.dealerHand) < len(app_inst.playerHand): 72 app_inst.status = "win" 73 else: 74 app_inst.status = "loss" 75 76 77def redrawAll(app_inst: AppWrapper): 78 print(app_inst.status, getScore(app_inst.dealerHand), getScore(app_inst.playerHand)) 79 match app_inst.status: 80 case "win": 81 app_inst.dealerHand[0].hidden = False 82 drawLabel( 83 f"Dealer's score: {getScore(app_inst.dealerHand)}", 84 app_inst.width // 2, 85 30, 86 ) 87 drawRect( 88 app_inst.width // 2 - 100, 89 app_inst.height // 2 - 50, 90 200, 91 100, 92 fill="lightGreen", 93 ) 94 drawLabel("You Win!", app_inst.width // 2, app_inst.height // 2) 95 case "loss": 96 app_inst.dealerHand[0].hidden = False 97 drawLabel( 98 f"Dealer's score: {getScore(app_inst.dealerHand)}", 99 app_inst.width // 2, 100 30, 101 ) 102 drawRect( 103 app_inst.width // 2 - 100, 104 app_inst.height // 2 - 50, 105 200, 106 100, 107 fill="pink", 108 ) 109 drawLabel( 110 "You Lose!", app_inst.width // 2, app_inst.height // 2, fill="red" 111 ) 112 case _: 113 pass 114 115 dealer_card_count = len(app_inst.dealerHand) 116 for i, card in enumerate(app_inst.dealerHand): 117 drawCard( 118 app_inst, 119 card, 120 app_inst.width // 2 + 25 * dealer_card_count // 2 - i * 37.5, 121 60 + i * 2, 122 ) 123 124 drawLabel( 125 f"Score: {getScore(app_inst.playerHand)}", 126 app_inst.width // 2, 127 app_inst.height * 3 // 4 - 20, 128 size=18, 129 ) 130 card_count = len(app_inst.playerHand) 131 for i, card in enumerate(app_inst.playerHand): 132 drawCard( 133 app_inst, 134 card, 135 app_inst.width // 2 + 25 * card_count // 2 - i * 37.5, 136 app_inst.height - 60 + i * 2, 137 ) 138 pass 139 140 141def getSuitLabelAndColor(suit): 142 if suit[0] == "C": 143 return "", "black" 144 elif suit[0] == "D": 145 return "", "red" 146 elif suit[0] == "H": 147 return "", "red" 148 else: 149 return "", "black" 150 151 152def makeRandomDeck(): 153 # first make a sorted deck 154 ranks = "Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King".split(",") 155 suits = "Clubs,Diamonds,Hearts,Spades".split(",") 156 deck = [makeCard(rank, suit) for rank in ranks for suit in suits] 157 # now shuffle and return the deck 158 random.shuffle(deck) 159 print([card.rank for card in deck]) 160 return deck 161 162 163def makeCard(rank, suit): 164 card = SimpleNamespace() 165 card.rank = rank 166 card.suit = suit 167 card.hidden = False 168 return card 169 170 171def drawCard(app_inst: AppWrapper, card, x, y): 172 label, color = getSuitLabelAndColor(card.suit) 173 drawRect(x - 25, y - 37.5, 50, 75, fill="white", border="black") 174 if card.hidden: 175 drawRect(x - 25, y - 37.5, 50, 75, fill="gray", border="black") 176 else: 177 drawLabel(f"{label}", x - 15, y - 27.5, fill=color, size=20) 178 drawLabel(f"{label}", x + 15, y + 27.5, fill=color, rotateAngle=180, size=20) 179 drawLabel( 180 f"{card.rank[0] if not card.rank.isdigit() else card.rank}", 181 x, 182 y, 183 fill=color, 184 size=20, 185 ) 186 187 188def main(): 189 runApp() 190 191 192main()