-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand.py
More file actions
executable file
·88 lines (70 loc) · 2.4 KB
/
hand.py
File metadata and controls
executable file
·88 lines (70 loc) · 2.4 KB
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
# -*- coding: utf-8 -*-
import card
import random
class Hand:
'''A class to contain the set of cards a player has available'''
def __init__(self):
self.cards = []
def __len__(self):
'''Returns the number of cards in the hand'''
return len(self.cards)
def add_cards(self, cards):
'''Puts the given card in the hand'''
self.cards.extend(cards)
def add_card(self, card):
'''Puts the given card in the hand'''
self.cards.append(card)
def empty(self):
'''Empties the hand'''
self.cards = []
def has_queen(self):
queen_spades = card.Card('♠', 'Q')
return queen_spades in self.cards
def play_card(self, choice):
''' Removes the card from the hand and puts it in play.'''
try:
self.cards.remove(choice)
return choice
except TypeError as err:
print "Invalid type provided"
def getRandomCard(self):
return random.choice(self)
def __getitem__(self, index):
return self.cards[index]
def sortCards(self):
'''Sorts all of the cards in the hand'''
self.cards.sort()
for i in range(len(self.cards)):
(self.cards[i]).loc = i + 1
def hasSuit(self, suit):
for card in self.cards:
if card.suit == suit:
return True
return False
def suitCount(self, suit):
count = 0
for card in self.cards:
if card.suit == suit:
count += 1
return count
def onlyHeartsLeft(self):
for card in self.cards:
if card.suit != '♥':
return False
return True
def __str__(self):
# Please find it within yourselves to forgive me for the following
self.sortCards()
# Split the cards into a list of 5 elements (one for each line of the card).
# Each element will contain a tuple consisting of
# X elements, where X is the # of cards in the hand
zipped = zip(*[card.templatedParts() for card in self.cards])
# Now, we convert each element of 'zipped' into a string, and join them
# by newlines
result = "\n".join("".join(map(str, l)) for l in zipped)
return result
def __contains__(self, other):
for card in self.cards:
if card == other:
return True
return False