-
Notifications
You must be signed in to change notification settings - Fork 16
/
Hand.py
143 lines (112 loc) · 2.95 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from random import randint
from Card import Card, Suit
clubs = 0
diamonds = 1
spades = 2
hearts = 3
suits = ["c", "d", "s", "h"]
class Hand:
def __init__(self):
self.clubs = []
self.diamonds = []
self.spades = []
self.hearts = []
# create hand of cards split up by suit
self.hand = [self.clubs, self.diamonds,
self.spades, self.hearts]
self.contains2ofclubs = False
def size(self):
return len(self.clubs) + len(self.diamonds) + len(self.spades) + len(self.hearts)
def addCard(self, card):
if card.suit == Suit(clubs):
if card.rank.rank == 2:
self.contains2ofclubs = True
self.clubs.append(card)
elif card.suit == Suit(diamonds):
self.diamonds.append(card)
elif card.suit == Suit(spades):
self.spades.append(card)
elif card.suit == Suit(hearts):
self.hearts.append(card)
else:
print 'Invalid card'
if self.size() == 13:
for suit in self.hand:
suit.sort()
def updateHand(self):
self.hand = [self.clubs, self.diamonds,
self.spades, self.hearts]
def getRandomCard(self):
suit = randint(0,3)
suit = self.hand[suit]
while len(suit) == 0:
suit = randint(0,3)
suit = self.hand[suit]
index = randint(0, len(suit)-1)
return suit[index]
def strToCard(self, card):
if len(card) == 0: return None
suit = card[len(card)-1].lower() # get the suit from the string
try:
suitIden = suits.index(suit)
except:
print 'Invalid suit'
return None
cardRank = card[0:len(card)-1] # get rank from string
try:
cardRank = cardRank.upper()
except AttributeError:
pass
# convert rank to int
if cardRank == "J":
cardRank = 11
elif cardRank == "Q":
cardRank = 12
elif cardRank == "K":
cardRank = 13
elif cardRank == "A":
cardRank = 14
else:
try:
cardRank = int(cardRank)
except:
print "Invalid card rank."
return None
return cardRank, suitIden
def containsCard(self, cardRank, suitIden):
for card in self.hand[suitIden]:
if card.rank.rank == cardRank:
cardToPlay = card
# remove cardToPlay from hand
# self.hand[suitIden].remove(card)
# update hand representation
# self.updateHand()
return cardToPlay
return None
def playCard(self, card):
cardInfo = self.strToCard(card)
if cardInfo is None:
return None
else:
cardRank, suitIden = cardInfo[0], cardInfo[1]
# see if player has that card in hand
return self.containsCard(cardRank, suitIden)
def removeCard(self, card):
suitId = card.suit.iden
for c in self.hand[suitId]:
if c == card:
if suitId == clubs and card.rank.rank == 2:
self.contains2ofclubs = False
# print "Removing:", c.__str__()
self.hand[card.suit.iden].remove(c)
self.updateHand()
def hasOnlyHearts(self):
print "len(self.hearts):",len(self.hearts)
print "self.size():",self.size()
return len(self.hearts) == self.size()
def __str__(self):
handStr = ''
for suit in self.hand:
for card in suit:
handStr += card.__str__() + ' '
return handStr