-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCards.py
More file actions
292 lines (249 loc) · 8.99 KB
/
Cards.py
File metadata and controls
292 lines (249 loc) · 8.99 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from functools import total_ordering
import discord
import random
from discord.ext import commands
SUITS = ("♠", "♦", "♥", "♣")
@total_ordering
class Card:
def __init__(self, suit, value, Joker=None):
self.suit = suit
self.value = value
self.Joker = Joker
def __str__(self):
if self.Joker:
return self.Joker + " Joker"
return str(self.getValue()) + self.suit
def getValue(self):
if self.value == 14:
return "A"
elif self.value == 11:
return "J"
elif self.value == 12:
return "Q"
elif self.value == 13:
return "K"
else:
return self.value
def __lt__(self, other):
if self.Joker:
if other.Joker:
return self.Joker < other.Joker
return False
if other.Joker:
return True
if self.value != other.value:
return self.value < other.value
else:
# spade diamons Hearts clubs:
return SUITS.index(self.suit) > SUITS.index(other.suit)
def __eq__(self, other) -> bool:
if self.Joker:
return self.Joker == other.Joker
return self.value == other.value and self.suit == other.suit
class Deck:
def __init__(self):
self.inDeck = []
self.playersHands = {}
self.cheated = {}
self.discard = []
self.buildDeck()
def buildDeck(self):
for suit in SUITS:
for value in range(2, 15):
self.inDeck.append(Card(suit, value))
self.inDeck.append(Card(None, 0, "Red"))
self.inDeck.append(Card(None, 1, "Black"))
random.shuffle(self.inDeck)
def shuffle(self):
random.shuffle(self.inDeck)
def shuffleIn(self):
self.inDeck.extend(self.discard)
self.discard = []
self.shuffle()
def printDeck(self):
for card in self.inDeck:
print(str(card))
def getCard(self, player):
card = self.inDeck.pop()
if player not in self.playersHands:
self.playersHands[player] = []
self.playersHands[player].append(card)
return card
def getCards(self, player, num):
cards = []
for i in range(num):
cards.append(self.getCard(player))
return cards
def stringPlayerHand(self, player):
self.playersHands[player].sort()
self.playersHands[player].reverse()
msg = player + "'s hand:"
for card in self.playersHands[player]:
msg += str(card) + "\n"
return msg
def useCard(self, player, value):
for i, card in enumerate(self.playersHands[player]):
if card.value == value:
self.discard.append(self.playersHands[player].pop(i))
return True
return False
def cheatCard(self, player, value):
for i, card in enumerate(self.playersHands[player]):
if card.value == value:
self.cheated[player] = self.playersHands[player].pop(i)
return True
return False
def hasCheated(self, player):
return player in self.cheated
def useCheatedCard(self, player):
if player in self.cheated:
self.discard.append(self.cheated.pop(player))
return True
return False
def getOrderedPlayerHands(self):
order = []
for player in self.playersHands:
for card in self.playersHands[player]:
order.append((player, card))
order.sort(key=lambda x: x[1], reverse=True)
return order
def stringOrderPlayers(self):
order = self.getOrderedPlayerHands()
msg = ""
prevPlayer = None
for player, card in order:
if prevPlayer != player:
msg += "\n" + player + ": "
prevPlayer = player
msg += str(card) + " "
return msg
def flushPlayerHand(self, player):
self.discard.extend(self.playersHands[player])
self.playersHands[player] = []
def flushAllHands(self):
for player in self.playersHands.keys():
self.flushPlayerHand(player)
def checkBlackJoker(self, cards):
for card in cards:
if card.Joker == "Black":
return True
return False
def blackJokerHandler(self, player):
if self.checkBlackJoker(self.playersHands[player]):
if self.hasCheated:
self.useCheatedCard(player)
self.useCard(player, 1)
return True
return False
def useCardsDownTo(self, value):
order = self.getOrderedPlayerHands()
for player, card in order:
if card.value >= value:
self.useCard(player, card.value)
else:
break
class DeckManager(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.deck = Deck()
@commands.command(
name="shuffle", brief="Shuffles deck, opt: shuffle discard into deck (any arg)"
)
async def shuffle(self, ctx, discard=None):
if discard:
self.deck.shuffleIn()
else:
self.deck.shuffle()
msg = "Shuffled"
if discard:
msg += " including discard pile in"
await ctx.send(msg)
@commands.command(
name="getCards",
brief="opt arg: num cards. Get(s) card(s) from the deck",
aliases=["get"],
)
async def getCards(self, ctx, num=1):
msg = ""
if num < 1:
await ctx.send("Invalid number of cards")
return
try:
cards = self.deck.getCards(ctx.author.global_name, num)
except IndexError:
msg = "Warning, there aren't enough cards in the deck, you'll need to shuffle, then get the rest of your cards \n"
bj = self.deck.blackJokerHandler(ctx.author.global_name)
if bj:
msg += "Got a black Joker, lost cheated card, remember to shuffle at end of round\n"
msg += ctx.author.global_name + " got: "
for card in cards:
msg += str(card) + " "
await ctx.send(msg)
@commands.command(name="hand", brief="Prints your hand")
async def hand(self, ctx):
msg = self.deck.stringPlayerHand(ctx.author.global_name)
if self.deck.hasCheated(ctx.author.global_name):
msg += "\nCheated card: " + str(self.deck.cheated[ctx.author.global_name])
await ctx.send(msg)
@commands.command(name="useCard", brief="Use a card from your hand")
async def useCard(self, ctx, value):
if not self.deck.useCard(ctx.author.global_name, int(value)):
await ctx.send("Invalid card")
return
await ctx.send("Used card")
@commands.command(
name="cheatCard", brief="Cheat a card from your hand", aliases=["cheat"]
)
async def cheatCard(self, ctx, value):
if self.deck.hasCheated(ctx.author.global_name):
await ctx.send("You have already cheated a card")
return
if not self.deck.cheatCard(ctx.author.global_name, int(value)):
await ctx.send("Invalid card")
return
await ctx.send("Cheated card")
@commands.command(name="useCheated", brief="Use the card you cheated")
async def useCheated(self, ctx):
if not self.deck.useCheatedCard(ctx.author.global_name):
await ctx.send("You have not cheated a card")
return
await ctx.send("Used cheated card")
@commands.command(name="emptyHand", brief="Discard your hand")
async def emptyHand(self, ctx):
self.deck.flushPlayerHand(ctx.author.global_name)
await ctx.send("Emptied hand")
@commands.command(name="emptyAllHands", brief="Discard all hands")
async def emptyAllHands(self, ctx):
self.deck.flushAllHands()
await ctx.send("Emptied all hands")
@commands.command(name="getOrder", brief="Get the initiative order")
async def getOrder(self, ctx):
await ctx.send(self.deck.stringOrderPlayers())
# debug this
@commands.command(
name="useDownTo",
breif="usage: !useDownTo <val>, Cause all cards down to a value (from any player's hand) to be used",
aliases=["useThrough"],
)
async def useDownTo(self, ctx, value):
self.deck.useCardsDownTo(int(value))
await ctx.send("Used down to " + value)
@commands.command(
name="hasCheated",
breif="show players that have cheated card currently",
help="show players that have cheated card currently",
)
async def hasCheated(self, ctx):
msg = ""
for name in self.deck.cheated.keys():
msg += name + ", "
msg = msg[:-2]
msg += " have cheated cards."
await ctx.send(msg)
@commands.command(name="deckLength")
async def deckLength(self, ctx):
msg = str(len(self.deck.inDeck))
msg = "Deck has " + msg + " cards."
await ctx.send(msg)
async def setup(bot):
await bot.add_cog(DeckManager(bot))