Took a break while I worked a bunch of overtime, but had a rare day off yesterday. Decided to continue my learning. Read a bit about python classes. The only use for classes that popped into my head was playing cards, so I threw together a simple game of "blackjack". Hesitate to call it that as its just player vs dealer, no betting, no soft hits for dealer, etc.
I'm sure your going to find LOTS of things I can do better, don't need at all, or just flat out screwed up, but I'm very new at all of this and I'm trying. Any pointers will be greatly appreciate
I'm sure your going to find LOTS of things I can do better, don't need at all, or just flat out screwed up, but I'm very new at all of this and I'm trying. Any pointers will be greatly appreciate
import random import time # Card images cardpic=[''' |---| | A | |---|''',''' |---| | 2 | |---|''',''' |---| | 3 | |---|''',''' |---| | 4 | |---|''',''' |---| | 5 | |---|''',''' |---| | 6 | |---|''',''' |---| | 7 | |---|''',''' |---| | 8 | |---|''',''' |---| | 9 | |---|''',''' |---| |10 | |---|''',''' |---| | J | |---|''',''' |---| | Q | |---|''',''' |---| | K | |---|'''] # Class to give card values class Cards: def __init__(self, card, value, draw): self.card = card self.value = value self.draw = draw # Build the deck deck=[] deck.append(Cards("TWO", 2, 1)) deck.append(Cards("THREE", 3, 2)) deck.append(Cards("FOUR", 4, 3)) deck.append(Cards("FIVE", 5, 4)) deck.append(Cards("SIX", 6, 5)) deck.append(Cards("SEVEN", 7, 6)) deck.append(Cards("EIGHT", 8, 7)) deck.append(Cards("NINE", 9, 8)) deck.append(Cards("TEN", 10, 9)) deck.append(Cards("JACK", 10, 10)) deck.append(Cards("QUEEN", 10, 11)) deck.append(Cards("KING", 10, 12)) deck.append(Cards("ACE", 11, 0)) # Variables player=[] playertotal=0 cpu=[] cputotal=0 # Pick a player card def playerhit(): global playertotal global player deal=random.randint(0,12) car=deck[deal].card num=deck[deal].value image=deck[deal].draw player.append(Cards(car, num, image)) playertotal=playertotal+num # Pick a dealer card def cpuhit(): global cputotal global cpu deal=random.randint(0,12) car=deck[deal].card num=deck[deal].value image=deck[deal].draw cpu.append(Cards(car, num, image)) cputotal=cputotal+num # Draw the cards on screen def table(): for i in range(len(player)): print(cardpic[player[i].draw]) print("_____") for i in range(len(cpu)): print(cardpic[cpu[i].draw]) print("\n" * 5) # Reset the game def start(): print("\n" * 5) global player global cpu global playertotal global cputotal playertotal=0 cputotal=0 player=[] cpu=[] playerhit() playerhit() cpuhit() cpuhit() # Adjust player ACES when busted def checkplayeraces(): global playertotal for i in player: if i.value == 11: i.value = 1 playertotal=playertotal-10 # Adjust dealer ACES when busted def checkcpuaces(): global cputotal for i in cpu: if i.value == 11: i.value = 1 cputotal=cputotal-10 # Decide winner def cpustay(): if cputotal > playertotal: print() print("You Lose") print() play() elif cputotal == playertotal: print() print("Draw") print() play() elif cputotal < playertotal: print() print("You Win") print() play() # Dealers turn def cputurn(): ai=True while ai==True: time.sleep(2) if cputotal > 21: checkcpuaces() if cputotal > 21: print() print("Dealer Busts, You Win!") print() play() ai=False if cputotal < 17: cpuhit() table() else: cpustay() table() ai=False # Deal & players turn def play(): start() run=True while run==True: table() if input()=="h": playerhit() if playertotal > 21: checkplayeraces() if playertotal > 21: table() print() print("Bust! You Lose") print() play() else: cputurn() # Begin play()