-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.rb
316 lines (285 loc) · 8.45 KB
/
cards.rb
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Draw an arbitrary number of cards from a standard deck and
# print out the best 5-card poker hand possible from those cards.
# Optionally, two (wild card) Jokers can be included.
# Modifying the deck away from standard should not produce any issues,
# but more than two jokers may produce some unusual behavior.
# Originally invented for use with the
# Deadlands Classic RPG (http://www.peginc.com/product-category/deadlands/),
# which uses this card draw mechanic in a number of places.
# This code is (c) 2014 Ian McLean, and is
# released under the WTFPL (http://www.wtfpl.net/), so do whatever
# you want with it.
# Usage:
# Open a ruby console, such as irb or pry
# load this file (load './cards.rb')
# d = Deck.new(:jokers=>true)
# d.shuffle
# d.draw(number_of_cards)
# Prevent already-defined constant error on reloading
Object.send(:remove_const, :Card) if defined?(Card)
class Card
SUITS = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
RANK_ORDER = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace']
attr_reader :rank, :suit
attr_writer :rank, :suit
def to_s(format = nil)
if rank == 'Joker'
if format == :short
"Jk(#{suit.first.upcase})"
else
"#{suit} #{rank}"
end
else
if format == :short
"#{rank}#{suit.first}"
else
"#{rank} of #{suit}"
end
end
end
def joker?
rank == 'Joker'
end
def value
RANK_ORDER.index(rank)
end
def suit_value
if rank == 'Joker'
4
else
SUITS.index(suit) + 1
end
end
end
class Deck
attr_reader :cards, :drawn
attr_writer :cards, :drawn
def fill(args = {})
Card::SUITS.each do |s|
Card::RANK_ORDER.each do |r|
c = Card.new
c.rank = r
c.suit = s
cards << c
end
end
if args[:jokers]
['Red', 'Black'].each do |s|
c = Card.new
c.rank = 'Joker'
c.suit = s
cards << c
end
end
end
def initialize(args = {})
self.cards = []
self.drawn = []
fill(args)
true
end
def shuffle(return_discards = true)
self.return_discards
self.cards = self.cards.sort_by{rand}
true
end
def return_discards
self.cards = (self.cards + self.drawn)
self.drawn = []
end
def draw(hand)
if self.cards.size < hand
puts "Deck only contains #{self.cards.size} cards. Reshuffling."
self.shuffle
end
pulled = self.cards[0..hand-1]
pulled.sort_by!{|c| Card::RANK_ORDER.index(c.rank) || 14}
self.cards = self.cards[hand..-1]
self.drawn = self.drawn + pulled
puts Hand.best(pulled)
pulled
end
end
class Hand
class << self
def best(cards)
Hand.straight_flush?(cards) ||
Hand.four?(cards) ||
Hand.full_house?(cards) ||
Hand.flush?(cards) ||
Hand.straight?(cards) ||
Hand.three?(cards) ||
Hand.two_pair?(cards) ||
Hand.pair?(cards) ||
Hand.nothing
end
def nothing
'Nothing!'
end
def straight_flush?(cards)
fsuits = cards.reject{|c| c.joker?}.group_by{|c| c.suit}.select do |s, cs|
(cs.size + cards.select{|c| c.joker?}.size) >= 5
end
fsuits.select! do |s, cs|
# clever!
straight?(cs)
end
best_sf = fsuits.sort_by do |s, cs|
high_card = straight?(cs).match(/\w+/).to_s
Card::RANK_ORDER.index(high_card.to_i == 0 ? high_card : high_card.to_i) +
(Card::SUITS.index(s) * 0.1)
end.last
if best_sf
royal = true
jokers_left = cards.select{|c| c.joker?}.size
[10,'Jack','Queen', 'King', 'Ace'].each do |r|
if best_sf[1].any?{|c| c.rank == r}
elsif jokers_left > 0
jokers_left -= 1
else
royal = false
end
end
"#{royal ? 'Royal' : 'Straight'} Flush of #{best_sf.first}"
else
nil
end
end
def straight?(cards)
cards.reject{|c| c.joker?}.reverse.each do |lowest|
highest_card = lowest.rank
s_ok = true
jokers = cards.select{|c| c.joker?}.size
used_cards = [lowest]
(1..4).each do |step|
needed_rank = Card::RANK_ORDER[lowest.value+step]
if needed_rank
# needed rank exists
matching_card = cards.select{|c| c.rank == needed_rank}[0]
if matching_card
# have one
highest_card = needed_rank
used_cards << matching_card
elsif jokers > 0
# use joker
jokers -= 1
highest_card = needed_rank
used_cards << cards.select{|c| c.joker?}.sort_by{|c| c.suit}[jokers]
else
# don't have one
s_ok = false
end
else
# needed rank ain't a thing
s_ok = false
end
end
if s_ok
return "#{highest_card} high straight"
end
end
if cards.any?{|c| c.rank == 'Ace' || c.joker?}
s_ok = true
jokers = cards.select{|c| c.joker?}.size
['Ace',2,3,4,5].each do |r|
if cards.select{|c| c.rank == r}[0]
elsif jokers > 0
jokers -= 1
else
s_ok = false
end
end
if s_ok
return "5 high straight"
end
end
nil
end
def full_house?(cards)
true_three_ranks = cards.reject{|c| c.joker?}.group_by{|c| c.rank}.select do |r, cs|
cs.size >= 3
end
true_pair_ranks = cards.reject{|c| c.joker?}.group_by{|c| c.rank}.select do |r, cs|
cs.size >= 2
end
case cards.select{|c| c.joker?}.size
when 0
true_three_ranks.keys.sort_by{|k| Card::RANK_ORDER.index(k)}.reverse.each do |tr|
a_pair = true_pair_ranks.keys.sort_by{|k| Card::RANK_ORDER.index(k)}.reject{ |r| r == tr }[-1]
if a_pair
return "Full house, #{tr}s over #{a_pair}s"
end
end
nil
when 1
# Have two pair, use Joker to make three. Other cases (two jokers & pair, joker & 3-of-a-kind)
# would already be triggered by the check for 4-of-a-kind
if true_pair_ranks.size >= 2
used_pairs = true_pair_ranks.sort_by{|r, cs| Card::RANK_ORDER.index(r)}.reverse[0..1]
return "Full house, #{used_pairs[0][0]}s over #{used_pairs[1][0]}s"
end
nil
end
end
def flush?(cards)
fsuits = cards.reject{|c| c.joker?}.group_by{|c| c.suit}.select do |s, cs|
(cs.size + cards.select{|c| c.joker?}.size) >= 5
end
if fsuits.empty?
nil
else
"Flush of #{fsuits.sort_by{|s, cs| Card::SUITS.index(cs.first.suit)}.last[0]}"
end
end
def four?(cards)
four_ranks = cards.reject{|c| c.joker?}.group_by{|c| c.rank}.select do |r, cs|
(cs.size + cards.select{|c| c.joker?}.size) >= 4
end
if four_ranks.empty?
nil
else
best_four = four_ranks.sort_by{|r, cs| Card::RANK_ORDER.index(r)}.reverse.first
"Four #{best_four[0]}s"
end
end
def three?(cards)
three_ranks = cards.reject{|c| c.joker?}.group_by{|c| c.rank}.select do |r, cs|
(cs.size + cards.select{|c| c.joker?}.size) >= 3
end
if three_ranks.empty?
nil
else
best_three = three_ranks.sort_by{|r, cs| Card::RANK_ORDER.index(r)}.reverse.first
"Three #{best_three[0]}s"
end
end
def pair?(cards)
pair_ranks = cards.reject{|c| c.joker?}.group_by{|c| c.rank}.select do |r, cs|
(cs.size + cards.select{|c| c.joker?}.size) >= 2
end
if pair_ranks.empty?
nil
else
best_pair = pair_ranks.sort_by{|r, cs| Card::RANK_ORDER.index(r)}.reverse.first
"Pair of #{best_pair[0]}s"
end
end
def two_pair?(cards)
true_pair_ranks = cards.reject{|c| c.joker?}.group_by{|c| c.rank}.select do |r, cs|
cs.size >= 2
end
high_card = cards.reject{|c| c.joker? || true_pair_ranks.keys.index(c.rank)}.sort_by{|c| Card::RANK_ORDER.index(c.rank)}.last
joker_count = cards.select{|c| c.joker?}.size
pair_ranks = true_pair_ranks.dup
if joker_count > 0
pair_ranks[high_card.rank] = [high_card, cards.select{|c| c.joker?}.first]
end
if pair_ranks.size < 2
nil
else
best_pairs = pair_ranks.sort_by{|r, cs| Card::RANK_ORDER.index(r)}.reverse[0..1]
"Two pair, #{best_pairs.first[0]}s and #{best_pairs[1][0]}s"
end
end
end
end