forked from jgayda/blackjack-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand.py
102 lines (83 loc) · 2.61 KB
/
hand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from card import Card
from typing import List
class HandIterator:
def __init__(self, cards: List[Card]):
self.index = 0
self.cards = cards
def __iter__(self):
return self
def __next__(self):
self.index += 1
try:
return self.cards[self.index]
except IndexError:
self.index = 0
raise StopIteration
class Hand:
def __init__(self, cards: List[Card], betSize: int):
self.cards = cards
self.betSize = betSize
self.insuranceBet = 0
self.isInsured = False
self.finalHandValue = 0
def __iter__(self):
return HandIterator(self.cards)
def addCard(self, card: Card):
self.cards.append(card)
def doubleDown(self):
self.betSize = self.betSize * 2
def insureHand(self):
self.insuranceBet = self.betSize / 2
self.isInsured = True
def isBlackjack(self):
if len(self.cards) != 2:
return False
card1Value = self.cards[0].getValue()
card2Value = self.cards[1].getValue()
if card1Value + card2Value == 21:
return True
return False
def isBust(self):
return self.getHandValue() > 21
def isPair(self):
if len(self.cards) != 2:
return False
card1Rank = self.cards[0].getRank()
card2Rank = self.cards[1].getRank()
return card1Rank == card2Rank
def isSoftTotal(self, softTotalDeductionCount):
if len(self.cards) == 1:
return False
numAces = self.getAcesCount()
if (softTotalDeductionCount == numAces):
return False
return numAces != 0
def getAcesCount(self):
numAces = 0
for card in self.cards:
if card.getValue() == 11:
numAces += 1
return numAces
def getInitialBet(self):
return self.betSize
def getSoftTotalAcelessValue(self, softAcesCount):
total = 0
for card in self.cards:
if card.getValue() != 11:
total += card.getValue()
return total + softAcesCount
def printHand(self, playerName):
print("Player: ", playerName, " has hand:")
for card in self.cards:
card.printCard()
def getCards(self):
return self.cards
def getHandValue(self):
sum = 0
for card in self.cards:
sum += card.getValue()
return sum
def setFinalHandValue(self, value):
self.finalHandValue = value
def splitHand(self):
return self.cards.pop()