Add some changes

This commit is contained in:
Oystein Kristoffer Tveit 2020-10-09 14:11:41 +02:00
parent 0c07bfaf9d
commit a2ea83fcbb
1 changed files with 52 additions and 27 deletions

View File

@ -37,6 +37,24 @@ class Card:
{' ' if self.id != 10 else ''}{self.num} {' ' if self.id != 10 else ''}{self.num}
""" """
def value(self, aceValue):
cardValues = {
'A': aceValue,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'10': 10,
'J': 10,
'Q': 10,
'K': 10
}
return cardValues[self.num]
class CardHandler: class CardHandler:
def __init__(self, cards, printSep=5, aceValue = 1): def __init__(self, cards, printSep=5, aceValue = 1):
self.cards = cards self.cards = cards
@ -98,22 +116,17 @@ class CardHandler:
@property @property
def cardSum(self): def cardSum(self):
cardValues = {
'A': self.aceValue, return sum([card.value(aceValue=self.aceValue) for card in self.cards])
'2': 2,
'3': 3, @property
'4': 4, def containsAce(self):
'5': 5, return any([True for card in self.cards if card.id == 1])
'6': 6,
'7': 7, @property
'8': 8, def containsBlackJack(self):
'9': 9, return any([True for card in self.cards if card.num == 'A']) \
'10': 10, and any([True for card in self.cards if card.value(self.aceValue) == 10])
'J': 10,
'Q': 10,
'K': 10
}
return sum([cardValues[card.num] for card in self.cards])
def emptyCard(): def emptyCard():
@ -137,13 +150,13 @@ class Blackjack:
for _ in range(2): for _ in range(2):
self.dealerHandler.generateNewCard() self.dealerHandler.generateNewCard()
self.handler.generateNewCard() self.handler.generateNewCard()
self.emptyHandler.cards[0] = self.dealerHandler.cards[0]
def determineAceValue(self): def determineAceValue(self):
containsAce = any([True for card in self.dealerHandler.cards if card.id == 1]) if self.handler.cardSum < 11 and self.handler.containsAce: # 11 + 1 = 12, 11 + 11 = 22
if self.dealerHandler.cardSum < 10 and containsAce: # 9 + 1 = 10, 9 + 13 = 22 self.handler.aceValue = self.dealerHandler.aceValue = 11
self.dealerHandler.aceValue = self.handler.aceValue = 13
else: else:
self.dealerHandler.aceValue = self.handler.aceValue = 1 self.handler.aceValue = self.dealerHandler.aceValue = 1
def reset(self): def reset(self):
self.generateNewCards() self.generateNewCards()
@ -173,7 +186,7 @@ class Blackjack:
\/_/\/___/ \/___/ \/____/\/___/ \/___/ \/____/ \/_/\/___/ \/___/ \/____/\/___/ \/___/ \/____/
""") """)
def gameOver(self): def gameOver(self, gameWon):
system('clear') system('clear')
print('\nDEALERS CARDS\n') print('\nDEALERS CARDS\n')
@ -183,9 +196,7 @@ class Blackjack:
print() print()
print('Ace value is', self.handler.aceValue) print('Ace value is', self.handler.aceValue)
cardSumNotExceeds21 = self.handler.cardSum < 22 if gameWon:
playerSumBiggerThanDealer = self.handler.cardSum > self.dealerHandler.cardSum
if cardSumNotExceeds21 and playerSumBiggerThanDealer:
self.youWin() self.youWin()
else: else:
self.youLose() self.youLose()
@ -202,8 +213,21 @@ class Blackjack:
exit(0) exit(0)
def checkIfLost(self): def checkIfLost(self):
if self.handler.cardSum > 21:
self.gameOver() winningConditions = [
self.handler.containsBlackJack and not self.dealerHandler.containsBlackJack,
self.dealerHandler.cardSum > 21
]
losingConditions = [
self.handler.cardSum > 21
]
if any(losingConditions):
self.gameOver(gameWon=False)
return True
elif any(winningConditions):
self.gameOver(gameWon=True)
return True return True
return False return False
@ -221,7 +245,8 @@ class Blackjack:
if not self.checkIfLost(): if not self.checkIfLost():
if not boolInput('Continue? [y/n]: ', yesNoLetters=('y','n')): if not boolInput('Continue? [y/n]: ', yesNoLetters=('y','n')):
self.gameOver() gameWon = self.dealerHandler.cardSum < self.handler.cardSum
self.gameOver(gameWon=gameWon)
return return
self.handler.generateNewCard() self.handler.generateNewCard()