-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
164 lines (127 loc) · 4.17 KB
/
classes.py
File metadata and controls
164 lines (127 loc) · 4.17 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from random import shuffle
from utils import printMatchedCard
"""
This file contains the main classes for the "Go-Fish" card game.
"""
class Player:
""" Player in the game."""
def __init__(self, number, name):
"""Create a Player with given NAME and NUMBER.
hand -- A list of cards in the player's hand
pairs -- A list of current pairs (tuples of 2 cards) that player has.
"""
self.number = number
self.name = name
self.hand = []
self.pairs = []
def hasCard(self, cardAsked):
if Card(cardAsked, "") in self.hand:
return True
return False
def hasCards(self):
return not len(self.hand) == 0
def numCardsInHand(self):
return len(self.hand)
def goFish(self, deck):
self.drawCard(deck)
def matchGuess(self, card, deck):
"""Helper function for removeFromHand(), called when
If the number of cards in the hand decreases to zero,
"""
matchingCard = self.removeFromHand(card)
self.pairs.append((card, matchingCard))
if len(self.hand) == 0:
self.drawCard(deck)
def addToHand(self, card, deck):
""" If matching card is in hand, remove both, add to pairs list, and
draw a new card. Otherwise, add card to current hand.
"""
if card in self.hand:
toRemove = self.hand[self.hand.index(card)]
self.pairs.append((card, toRemove))
self.hand.remove(toRemove)
self.drawCard(deck)
self.drawCard(deck)
# Commenting out to reduce confusing during game play for new players.
# This code just prints when a player draws a card that matches one in
# their hand.
# if self.number in Card.faceCards:
# printMatchedCard(self.name, Card.faceCards[self.number])
# else:
# printMatchedCard(self.name, card.number)
else:
self.hand.append(card)
def drawCard(self, deck):
""" Draw card from deck and add to hand. """
if not deck.isEmpty():
card = deck.pop()
self.addToHand(card, deck)
elif not deck.printedEmpty():
print("The deck is empty. The last card has been drawn. ")
def removeFromHand(self, card):
""" Remove card from hand. """
if card not in self.hand:
raise RuntimeError("Card does not exist in hand; cannot remove.")
else:
toRemove = self.hand[self.hand.index(card)]
self.hand.remove(card)
return toRemove
class Card:
""" Represents a playing card with a given number and suit. In the context
of this game (Go-Fish), suits serve no purpose other than to distinguish cards
in the deck.
"""
faceCards = {11: "jack", 12: "queen", 13: "king"}
def __init__(self, number, suit):
"""Create a playing card with given NUMBER and SUIT.
number -- 1 through 13, where 11=Jack, 12=Queen, 13=King.
suit -- A string; either clubs, spades, hearts, or diamonds.
"""
self.number = number
self.suit = suit
def __eq__(self, otherCard):
"""Set card equivalence to be based on number, since suits are irrelevant in Go-Fish. """
return self.number == otherCard.number
def __str__(self):
if self.number in Card.faceCards:
return "{} of {}".format(Card.faceCards[self.number], self.suit)
else:
return "{} of {}".format(self.number, self.suit)
def __repr__(self):
if self.number in Card.faceCards:
return "{} of {}".format(Card.faceCards[self.number], self.suit)
else:
return "{} of {}".format(self.number, self.suit)
class Deck:
""" A Deck represents a deck of cards. Each deck contains 52 cards. """
def __init__(self):
"""Create the deck for the game.
deck -- array containing all 52 cards
empty -- boolean representing if the deck is empty
"""
self.deck = self.createDeck()
self.empty = False
self.shuffleDeck()
def createDeck(self):
""" Initialize deck with 52 cards, 1 from each number/facecard and suit combination."""
deck = []
for suit in ["clubs", "spades", "hearts", "diamonds"]:
for num in range(1,14):
deck.append(Card(num, suit))
return deck
def shuffleDeck(self):
""" Shuffle deck using Python's random.shuffle. """
shuffle(self.deck)
def pop(self):
""" Return the top card from the deck. """
if not self.isEmpty():
return self.deck.pop()
return
def isEmpty(self):
if self.length() == 0:
self.empty = True
return True
def length(self):
return len(self.deck)
def printedEmpty(self):
return self.empty