Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- values = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K']
- suits = ['card:Spades', 'card:Hearts', 'card:Clubs', 'card: Diamonds']
- class Card(object):
- def __init__(self, number, face, value, back='card:BackRed4'):
- self.number = number
- self.face = face
- self.value = value
- self.back = back
- def __repr__(self):
- return f"Card(number = {self.number}, face = {self.face}, value = {self.value}, back = {self.back})"
- def __str__(self):
- return str(self.face)
- def __add__(self, other):
- return self.value + other.value
- def __radd__(self, other):
- return other + self.value
- #card contains 4 attributes: number, string for rendering image,
- #numeric value: 10 if face card, 11 if 'Ace' otherwise value is same
- #as number, string for rendering the back of the card.
- #Create a full deck (52 cards) with list comprehension
- cards = [
- Card(n, ("'" + suit + str(n) + "'"), 10 if n in ['J', 'Q', 'K'] else 11
- if n == 'A' else n) for n in values for suit in suits
- ]
- cards = list(cards)
- random.shuffle(cards)
- def nextCard():
- return cards.pop()
- #print(cards[4])
- '''for card in cards:
- print(card, card.number, card.value)'''
- class Hand(object):
- hit = True
- stand = False
- doubleOn = [9, 10, 11]
- total = []
- def __init__(self, player = True):
- self.player = True
- def handValue(self):
- return sum(self.total)
- def reachedTwentyOne(self):
- if (sum(self.total) > 21):
- self.hit = False
- #lose bet, do next hand
- elif (sum(self.total) == 21) and (any(x.value != 11 for x in self.total)):
- self.hit = False
- #dealer move
- def blackjackCheck(self):
- if (len(self.total) == 2) and (sum(self.total) == 21):
- hit = False
- if self.player == True:
- #check if push, pay player bet times blackjack bonus
- print('Player Blackjack')
- else:
- #do something with insurance or whatever
- print('Dealer Blackjack')
- def add(self, draw):
- if (draw.value == 11) and (any(x.value == 11 for x in self.total)):
- draw.value = 1
- self.total.append(draw)
- elif (draw.value == 11) and (sum(self.total) + draw.value > 21):
- draw.value = 1
- self.total.append(draw)
- elif (draw.value + sum(self.total) > 21) and (any(
- x.value == 11 for x in self.total)):
- for object in self.total:
- if object.value == 11:
- object.value = 1
- self.total.append(draw)
- else:
- self.total.append(draw)
- def playerHit(self):
- self.reachedTwentyOne()
- if self.hit == True:
- self.add(cards.pop())
- self.blackjackCheck()
- print(sum(self.total))
- if (sum(self.total) > 21):
- print('Busted!')
- self.hit = False
- else:
- print('Not allowed to hit')
- def playerStand(self):
- self.hit = False
- self.stand = True
- #dealer move
- def doubleDown(self):
- if self.handValue() in self.doubleOn:
- #double bet
- print(f'doubling down on {sum(self.total)}')
- self.playerHit()
- self.playerStand()
- else:
- print(f'not allowed to double down on {sum(self.total)}')
- def playerSplit(self):
- if self.total[0].value == self.total[1].value:
- playerHand2 = self.__class__()
- playerHand2.add(self.total[1])
- else:
- print(f'Split not allowed on {self.total[0].number} and {self.total[1].number}')
- class Round():
- def __init__(self, player, bet, dealer):
- self.player = player
- self.bet = bet
- self.dealer = dealer
- def newRound(self):
- pass
- playerHand = Hand()
- #dealerHand = Hand()
- #playerHand.add(nextCard())
- #dealerHand.add(nextCard())
- #playerHand.add(nextCard())
- #dealerHand.add(nextCard())
- playerHand.playerHit()
- playerHand.playerHit()
- '''for card in playerHand.total:
- if card.value == 11:
- print('found one')'''
- #print(repr(cards[0]))
- #print(eval('cards[0]'))
Add Comment
Please, Sign In to add comment