CMU Coding Bootcamp

feat: oct 6 add blackjack

thecoded.prof 56e6efb5 8337c5f1

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