from cmu_graphics.cmu_graphics import AppWrapper from cmu_graphics import * import random from types import SimpleNamespace from sys import exit from calendar import c def onAppStart(app_inst: AppWrapper): app_inst.status = "" app_inst.deck = makeRandomDeck() app_inst.playerHand = [app_inst.deck.pop(), app_inst.deck.pop()] app_inst.dealerHand = [app_inst.deck.pop(), app_inst.deck.pop()] app_inst.dealerHand[0].hidden = True app_inst.playerDrawing = True def getScore(hand): score = 0 aces = 0 for card in hand: if card.rank == "Ace": aces += 1 score += 11 elif card.rank in ["Jack", "Queen", "King"]: score += 10 else: score += int(card.rank) while score > 21 and aces > 0: score -= 10 aces -= 1 return score def onKeyPress(app_inst: AppWrapper, key): if key == "h" and app_inst.playerDrawing: app_inst.playerHand.append(app_inst.deck.pop()) elif key == "s": app_inst.playerDrawing = False app_inst.dealerHand[0].hidden = False app_inst.dealerHand[1].hidden = False elif key == "r": onAppStart(app_inst) return elif key == "q": exit() dealer_score = getScore(app_inst.dealerHand) player_score = getScore(app_inst.playerHand) if player_score > 21: app_inst.status = "loss" app_inst.playerDrawing = False return if app_inst.playerDrawing: if dealer_score < 17: app_inst.dealerHand.append(app_inst.deck.pop()) else: while dealer_score < 17: app_inst.dealerHand.append(app_inst.deck.pop()) if dealer_score > 21: app_inst.status = "win" app_inst.playerDrawing = False return if not app_inst.playerDrawing and dealer_score >= 17: if player_score > dealer_score: app_inst.status = "win" elif player_score < dealer_score: app_inst.status = "loss" else: if len(app_inst.dealerHand) < len(app_inst.playerHand): app_inst.status = "win" else: app_inst.status = "loss" def redrawAll(app_inst: AppWrapper): print(app_inst.status, getScore(app_inst.dealerHand), getScore(app_inst.playerHand)) match app_inst.status: case "win": app_inst.dealerHand[0].hidden = False drawLabel( f"Dealer's score: {getScore(app_inst.dealerHand)}", app_inst.width // 2, 30, ) drawRect( app_inst.width // 2 - 100, app_inst.height // 2 - 50, 200, 100, fill="lightGreen", ) drawLabel("You Win!", app_inst.width // 2, app_inst.height // 2) case "loss": app_inst.dealerHand[0].hidden = False drawLabel( f"Dealer's score: {getScore(app_inst.dealerHand)}", app_inst.width // 2, 30, ) drawRect( app_inst.width // 2 - 100, app_inst.height // 2 - 50, 200, 100, fill="pink", ) drawLabel( "You Lose!", app_inst.width // 2, app_inst.height // 2, fill="red" ) case _: pass dealer_card_count = len(app_inst.dealerHand) for i, card in enumerate(app_inst.dealerHand): drawCard( app_inst, card, app_inst.width // 2 + 25 * dealer_card_count // 2 - i * 37.5, 60 + i * 2, ) drawLabel( f"Score: {getScore(app_inst.playerHand)}", app_inst.width // 2, app_inst.height * 3 // 4 - 20, size=18, ) card_count = len(app_inst.playerHand) for i, card in enumerate(app_inst.playerHand): drawCard( app_inst, card, app_inst.width // 2 + 25 * card_count // 2 - i * 37.5, app_inst.height - 60 + i * 2, ) pass def getSuitLabelAndColor(suit): if suit[0] == "C": return "♣", "black" elif suit[0] == "D": return "♦", "red" elif suit[0] == "H": return "♥", "red" else: return "♠", "black" def makeRandomDeck(): # first make a sorted deck ranks = "Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King".split(",") suits = "Clubs,Diamonds,Hearts,Spades".split(",") deck = [makeCard(rank, suit) for rank in ranks for suit in suits] # now shuffle and return the deck random.shuffle(deck) print([card.rank for card in deck]) return deck def makeCard(rank, suit): card = SimpleNamespace() card.rank = rank card.suit = suit card.hidden = False return card def drawCard(app_inst: AppWrapper, card, x, y): label, color = getSuitLabelAndColor(card.suit) drawRect(x - 25, y - 37.5, 50, 75, fill="white", border="black") if card.hidden: drawRect(x - 25, y - 37.5, 50, 75, fill="gray", border="black") else: drawLabel(f"{label}", x - 15, y - 27.5, fill=color, size=20) drawLabel(f"{label}", x + 15, y + 27.5, fill=color, rotateAngle=180, size=20) drawLabel( f"{card.rank[0] if not card.rank.isdigit() else card.rank}", x, y, fill=color, size=20, ) def main(): runApp() main()