Add some changes

This commit is contained in:
Oystein Kristoffer Tveit 2020-10-09 14:11:41 +02:00
parent 0c07bfaf9d
commit a2ea83fcbb

View File

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