-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.py
More file actions
201 lines (174 loc) · 8.07 KB
/
deck.py
File metadata and controls
201 lines (174 loc) · 8.07 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
@author: Tavish Peckham
"""
from random import shuffle
"""
Deck objects are 52 card standard playing card decks.
"""
allCards = [
"Ace of Diamonds", "Ace of Spades", "Ace of Hearts", "Ace of Clubs",
"2 of Diamonds", "2 of Spades", "2 of Hearts", "2 of Clubs",
"3 of Diamonds", "3 of Spades", "3 of Hearts", "3 of Clubs",
"4 of Diamonds", "4 of Spades", "4 of Hearts", "4 of Clubs",
"5 of Diamonds", "5 of Spades", "5 of Hearts", "5 of Clubs",
"6 of Diamonds", "6 of Spades", "6 of Hearts", "6 of Clubs",
"7 of Diamonds", "7 of Spades", "7 of Hearts", "7 of Clubs",
"8 of Diamonds", "8 of Spades", "8 of Hearts", "8 of Clubs",
"9 of Diamonds", "9 of Spades", "9 of Hearts", "9 of Clubs",
"10 of Diamonds", "10 of Spades", "10 of Hearts", "10 of Clubs",
"Jack of Diamonds", "Jack of Spades", "Jack of Hearts", "Jack of Clubs",
"Queen of Diamonds", "Queen of Spades", "Queen of Hearts",
"Queen of Clubs", "King of Diamonds", "King of Spades", "King of Hearts",
"King of Clubs"
]
shorthand = [
"1", "2", "3", "4", "5", "6", "7", "8", "9", "j", "q", "k", "a", "J", "Q",
"K", "A"
]
S_IDLE = 0
S_FISHLOBBY = 1
S_FISH = 3
S_CHOOSING = 5
class Deck:
def __init__(self): # Creates a new deck object.
self.cards = allCards[:]
shuffle(self.cards)
def getDeck(self): # Returns number of cards left in the deck.
return len(self.cards)
def reset(self, cardList): # Shuffles and returns the deck.
cardlist = self.shuffle(cardList)
return cardList
def draw(self, numToDraw): # Removes + returns cards from the deck.
output = []
if numToDraw < len(self.cards):
for i in range(numToDraw):
output.append(self.cards.pop())
else:
for i in range(len(self.cards)):
output.append(self.cards.pop())
return output
def reveal(self): # Returns the top card of the deck.
topCard = self.cards[0]
return topCard
def burn(self, numToBurn): # Removes the top x cards of the deck.
if numToBurn < len(self.cards) and numToBurn > 0:
self.cards = self.cards[numToBurn:]
return ("%d cards burned." % (numToBurn))
else:
return ("Unable to burn %d cards." % (numToBurn))
class DealerBot:
def __init__(self):
self.state = S_IDLE
self.players = {} # The hands of bother players.
self.deck = Deck() # The 52-card deck.
self.points = {} # How many points each player has.
def consolidate(self):
for player in self.players:
cards = {
"A": 0,
"K": 0,
"Q": 0,
"J": 0,
"1": 0,
"9": 0,
"8": 0,
"7": 0,
"6": 0,
"5": 0,
"4": 0,
"3": 0,
"2": 0
}
for cardType in cards:
for card in self.players[player]:
if card[0] == cardType:
cards[cardType] += 1
if cards[cardType] / 4 >= 1: # If a player has 4 of one number or face card, those cards are removed from the player's hand and the player recieves a point.
for i in range(4):
for card in self.players[player]:
if card[0] == cardType:
self.players[player].remove(card)
self.points[player] += 1
def handsAreEmpty(self): # Return whether the player's hands are empty or not.
for i in self.players:
if len(self.players[i]) > 0:
return False
return True
def interMsg(self, msg, fromPlayer):
recipients, response = "all", ""
if msg == "stop" and self.state != S_IDLE: # Stop the game and set the state to idle.
response = "Thanks for playing!"
self.players = {}
self.state = S_IDLE
if msg == "players":
response = "\n"+" ".join(self.players.keys())
if self.state == S_IDLE: # If the state is idle and a player types "play", start the DealerBot.
if msg == "play":
self.state = S_CHOOSING
response = """Hello, my name is Dealerbot. Which game would you like to play?>
1. Go Fish
Type 'stop' at any time to end the Dealerbot"""
if self.state is S_CHOOSING:
if msg == "1":
response = "Press 1 to enter the Go Fish lobby (Max 2 players)"
self.state = S_FISHLOBBY
elif self.state is S_FISHLOBBY:
if msg == "1" and len(self.players.keys()) < 4:
self.players[fromPlayer] = None
response = "Player %s added. Type go to start." % (fromPlayer)
elif msg == "1" and len(self.players.keys()) == 4:
response = "Sorry, game is full!"
elif msg == "go" and len(self.players.keys()) < 2:
response = "Not enough players to start a game of Go Fish!"
elif msg == "go" and len(self.players.keys()) > 1:
self.state = S_FISH
drawHand = [i for i in self.players]
for i in drawHand:
self.players[i] = self.deck.draw(5)
self.points[i] = 0
print("player: ", i, "hand: ", self.players[i])
recipients = 'all'
response = "Game on! Type 'hand' to look at cards in your hand, and 'take' followed by a number or the first letter of a face card to attempt to take it from your opponent. "
elif self.state is S_FISH:
if msg == "hand": # Return a private message with the contents of the player's hand.
recipients = 'private'
response = self.players
if msg == "points": # Return a private message with how many points the player has.
recipients = 'private'
response = {fromPlayer: [str(self.points)]}
cardsToReturn = []
if len(msg) == 6 or len(msg) == 7:
if msg[0:4] == "take": # If the message begins with "take"...
if msg[5] in shorthand: # And the following number or letter is shorthand for a card in the deck...
cardInQuestion = msg[5]
for i in self.players:
if i != fromPlayer:
for k, j in enumerate(self.players[i]):
if j[0] == cardInQuestion:
cardsToReturn.append(
self.players[i].pop(k))
if len(cardsToReturn) > 0:
recipients = 'all'
response = "%s got %s's" % (
fromPlayer, i
) + str(cardsToReturn) + "!" # A player successfully guessed a card in their opponent's hand.
self.players[fromPlayer].extend(
cardsToReturn)
if cardsToReturn == []: # If we look through a player's hand and *don't* find the card that was asked for...
recipients = 'all'
response = 'go fish, %s!' % (fromPlayer) # Return a "Go Fish" message.
self.players[fromPlayer].extend(self.deck.draw(1))
self.consolidate()
if (self.deck.getDeck() == 0 and self.handsAreEmpty):
maxPoint = 0
maxPlayer = ""
for i in self.points:
if self.points[i] > maxPoint:
maxPoint = self.points[i]
maxPlayer = i
recipients = 'all'
response = maxPlayer + " wins!"
if response != "":
if type(response) == str:
response = " \n[Dealerbot] " + "\n" + response
return recipients, response