-
Notifications
You must be signed in to change notification settings - Fork 5
/
hand.rs
406 lines (367 loc) · 14.4 KB
/
hand.rs
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use assets::constants::*;
use assets::lookup::{LOOKUP, LOOKUP_FLUSH};
use assets::offsets::OFFSETS;
use std::ops::{Add, AddAssign};
use std::str::FromStr;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum HandCategory {
HighCard = 0,
OnePair = 1,
TwoPair = 2,
ThreeOfAKind = 3,
Straight = 4,
Flush = 5,
FullHouse = 6,
FourOfAKind = 7,
StraightFlush = 8,
}
/// Returns the hand category from hand rank computed by `Hand::evaluate()`.
#[inline]
pub fn get_hand_category(hand_rank: u16) -> HandCategory {
match hand_rank >> 12 {
0 => HandCategory::HighCard,
1 => HandCategory::OnePair,
2 => HandCategory::TwoPair,
3 => HandCategory::ThreeOfAKind,
4 => HandCategory::Straight,
5 => HandCategory::Flush,
6 => HandCategory::FullHouse,
7 => HandCategory::FourOfAKind,
8 => HandCategory::StraightFlush,
_ => unreachable!(),
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Hand {
key: u64,
mask: u64,
}
impl Hand {
/// Creates an empty `Hand` struct.
#[inline]
pub fn new() -> Self {
Self {
key: 0x3333 << SUIT_SHIFT,
mask: 0,
}
}
/// Creates a new hand structure consists of `cards`.
/// Elements in `cards` must be in the range \[0, 51\].
/// (0 corresponds to the deuce of clubs, and 51 corresponds to the ace of spades)
#[inline]
pub fn from_slice(cards: &[usize]) -> Self {
let mut hand = Self::new();
for card in cards {
hand = hand.add_card(*card);
}
hand
}
/// Checks whether the hand is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.mask == 0
}
/// Returns current number of cards in `self`.
#[inline]
pub fn len(&self) -> usize {
self.mask.count_ones() as usize
}
/// Returns the bit mask of `self`.
#[inline]
pub fn get_mask(&self) -> u64 {
self.mask
}
/// Returns whether the `card` is included in `self`.
#[inline]
pub fn contains(&self, card: usize) -> bool {
(self.mask & unsafe { *CARDS.get_unchecked(card) }.1) != 0
}
/// Returns a new hand struct where `card` is added to `self`.
/// `card` must be in the range \[0, 51\] and must not be already included in `self`.
/// (0 corresponds to the deuce of clubs, and 51 corresponds to the ace of spades)
#[inline]
pub fn add_card(&self, card: usize) -> Self {
let (k, m) = unsafe { *CARDS.get_unchecked(card) };
Self {
key: self.key.wrapping_add(k),
mask: self.mask.wrapping_add(m),
}
}
/// Returns a new hand struct where `card` is removed from `self`.
/// `card` must be in the range \[0, 51\] and included in `self`.
#[inline]
pub fn remove_card(&self, card: usize) -> Self {
let (k, m) = unsafe { *CARDS.get_unchecked(card) };
Self {
key: self.key.wrapping_sub(k),
mask: self.mask.wrapping_sub(m),
}
}
/// Returns hand strength in 16-bit integer.
/// This function may crush when `self.len() < 5 || self.len() > 7`.
#[inline]
pub fn evaluate(&self) -> u16 {
let is_flush = self.key & FLUSH_MASK;
if is_flush > 0 {
let flush_key = (self.mask >> (4 * is_flush.leading_zeros())) as u16;
unsafe { *LOOKUP_FLUSH.get_unchecked(flush_key as usize) }
} else {
let rank_key = self.key as u32 as usize;
let offset = unsafe { *OFFSETS.get_unchecked(rank_key >> OFFSET_SHIFT) as usize };
let hash_key = rank_key.wrapping_add(offset);
unsafe { *LOOKUP.get_unchecked(hash_key) }
}
}
}
impl Add for Hand {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
key: self
.key
.wrapping_add(rhs.key)
.wrapping_sub(0x3333 << SUIT_SHIFT),
mask: self.mask.wrapping_add(rhs.mask),
}
}
}
impl AddAssign for Hand {
fn add_assign(&mut self, rhs: Self) {
self.key = self.key.wrapping_add(rhs.key);
self.key = self.key.wrapping_sub(0x3333 << SUIT_SHIFT);
self.mask = self.mask.wrapping_add(rhs.mask);
}
}
impl Default for Hand {
fn default() -> Self {
Self::new()
}
}
impl FromStr for Hand {
type Err = String;
fn from_str(hand_str: &str) -> Result<Self, Self::Err> {
let mut hand = Self::new();
let mut chars = hand_str.chars();
loop {
let rank_opt = chars.next();
if rank_opt.is_none() {
return Ok(hand);
}
let rank_char = rank_opt.unwrap();
let suit_char = chars
.next()
.ok_or("parse failed: expected suit character, but got EOF")?;
let rank_id = match rank_char.to_ascii_uppercase() {
'2' => Ok(0),
'3' => Ok(1),
'4' => Ok(2),
'5' => Ok(3),
'6' => Ok(4),
'7' => Ok(5),
'8' => Ok(6),
'9' => Ok(7),
'T' => Ok(8),
'J' => Ok(9),
'Q' => Ok(10),
'K' => Ok(11),
'A' => Ok(12),
ch => Err(format!(
"parse failed: expected rank character, but got '{}'",
ch
)),
}?;
let suit_id = match suit_char.to_ascii_lowercase() {
'c' => Ok(0),
'd' => Ok(1),
'h' => Ok(2),
's' => Ok(3),
ch => Err(format!(
"parse failed: expected suit character, but got '{}'",
ch
)),
}?;
hand = hand.add_card(rank_id * 4 + suit_id);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
fn evaluate_hand_str(hand_str: &str) -> u16 {
let hand = hand_str.parse::<Hand>().unwrap();
assert_eq!(hand.len(), 7);
hand.evaluate()
}
#[test]
fn test_parser() {
let cards = [2, 3, 5, 7, 11, 13, 17];
let hand_from_vec = Hand::from_slice(&cards);
let hand_from_str = "2h2s3d3s4s5d6d".parse::<Hand>();
assert_eq!(hand_from_str, Ok(hand_from_vec));
assert_eq!("".parse::<Hand>(), Ok(Hand::new()));
assert_eq!(
"A".parse::<Hand>(),
Err("parse failed: expected suit character, but got EOF".into())
);
assert_eq!(
"Ax".parse::<Hand>(),
Err("parse failed: expected suit character, but got 'x'".into())
);
assert_eq!(
"10s".parse::<Hand>(),
Err("parse failed: expected rank character, but got '1'".into())
);
}
#[test]
fn test_all_5card_combinations() {
let mut rankset = HashSet::new();
let mut counter = vec![0; HandCategory::StraightFlush as usize + 1];
for i in 0..(NUMBER_OF_CARDS - 4) {
let hand = Hand::new().add_card(i);
for j in (i + 1)..(NUMBER_OF_CARDS - 3) {
let hand = hand.add_card(j);
for k in (j + 1)..(NUMBER_OF_CARDS - 2) {
let hand = hand.add_card(k);
for m in (k + 1)..(NUMBER_OF_CARDS - 1) {
let hand = hand.add_card(m);
for n in (m + 1)..NUMBER_OF_CARDS {
let hand = hand.add_card(n);
let rank = hand.evaluate();
let category = get_hand_category(rank);
rankset.insert(rank);
counter[category as usize] += 1;
}
}
}
}
}
assert_eq!(rankset.len(), 7462);
assert_eq!(counter[HandCategory::StraightFlush as usize], 40);
assert_eq!(counter[HandCategory::FourOfAKind as usize], 624);
assert_eq!(counter[HandCategory::FullHouse as usize], 3744);
assert_eq!(counter[HandCategory::Flush as usize], 5108);
assert_eq!(counter[HandCategory::Straight as usize], 10200);
assert_eq!(counter[HandCategory::ThreeOfAKind as usize], 54912);
assert_eq!(counter[HandCategory::TwoPair as usize], 123552);
assert_eq!(counter[HandCategory::OnePair as usize], 1098240);
assert_eq!(counter[HandCategory::HighCard as usize], 1302540);
}
#[test]
fn test_all_6card_combinations() {
let mut rankset = HashSet::new();
let mut counter = vec![0; HandCategory::StraightFlush as usize + 1];
for i in 0..(NUMBER_OF_CARDS - 5) {
let hand = Hand::new().add_card(i);
for j in (i + 1)..(NUMBER_OF_CARDS - 4) {
let hand = hand.add_card(j);
for k in (j + 1)..(NUMBER_OF_CARDS - 3) {
let hand = hand.add_card(k);
for m in (k + 1)..(NUMBER_OF_CARDS - 2) {
let hand = hand.add_card(m);
for n in (m + 1)..(NUMBER_OF_CARDS - 1) {
let hand = hand.add_card(n);
for p in (n + 1)..NUMBER_OF_CARDS {
let hand = hand.add_card(p);
let rank = hand.evaluate();
let category = get_hand_category(rank);
rankset.insert(rank);
counter[category as usize] += 1;
}
}
}
}
}
}
assert_eq!(rankset.len(), 6075);
assert_eq!(counter[HandCategory::StraightFlush as usize], 1844);
assert_eq!(counter[HandCategory::FourOfAKind as usize], 14664);
assert_eq!(counter[HandCategory::FullHouse as usize], 165984);
assert_eq!(counter[HandCategory::Flush as usize], 205792);
assert_eq!(counter[HandCategory::Straight as usize], 361620);
assert_eq!(counter[HandCategory::ThreeOfAKind as usize], 732160);
assert_eq!(counter[HandCategory::TwoPair as usize], 2532816);
assert_eq!(counter[HandCategory::OnePair as usize], 9730740);
assert_eq!(counter[HandCategory::HighCard as usize], 6612900);
}
#[test]
fn test_all_7card_combinations() {
let mut rankset = HashSet::new();
let mut counter = vec![0; HandCategory::StraightFlush as usize + 1];
for i in 0..(NUMBER_OF_CARDS - 6) {
let hand = Hand::new().add_card(i);
for j in (i + 1)..(NUMBER_OF_CARDS - 5) {
let hand = hand.add_card(j);
for k in (j + 1)..(NUMBER_OF_CARDS - 4) {
let hand = hand.add_card(k);
for m in (k + 1)..(NUMBER_OF_CARDS - 3) {
let hand = hand.add_card(m);
for n in (m + 1)..(NUMBER_OF_CARDS - 2) {
let hand = hand.add_card(n);
for p in (n + 1)..(NUMBER_OF_CARDS - 1) {
let hand = hand.add_card(p);
for q in (p + 1)..NUMBER_OF_CARDS {
let hand = hand.add_card(q);
let rank = hand.evaluate();
let category = get_hand_category(rank);
rankset.insert(rank);
counter[category as usize] += 1;
}
}
}
}
}
}
}
assert_eq!(rankset.len(), 4824);
assert_eq!(counter[HandCategory::StraightFlush as usize], 41584);
assert_eq!(counter[HandCategory::FourOfAKind as usize], 224848);
assert_eq!(counter[HandCategory::FullHouse as usize], 3473184);
assert_eq!(counter[HandCategory::Flush as usize], 4047644);
assert_eq!(counter[HandCategory::Straight as usize], 6180020);
assert_eq!(counter[HandCategory::ThreeOfAKind as usize], 6461620);
assert_eq!(counter[HandCategory::TwoPair as usize], 31433400);
assert_eq!(counter[HandCategory::OnePair as usize], 58627800);
assert_eq!(counter[HandCategory::HighCard as usize], 23294460);
}
#[test]
fn test_edge_cases() {
// straight flushes
assert_eq!(evaluate_hand_str("AsKsQsJsTs7d5s"), (8 << 12) + 9);
assert_eq!(evaluate_hand_str("Ac7c6c5c4c3c2c"), (8 << 12) + 2);
assert_eq!(evaluate_hand_str("AdQsJc5d4d3d2d"), (8 << 12) + 0);
// four of a kinds
assert_eq!(evaluate_hand_str("AsAcAhAdKsQcTh"), (7 << 12) + 155);
assert_eq!(evaluate_hand_str("3d3h3s2c2d2h2s"), (7 << 12) + 0);
// full houses
assert_eq!(evaluate_hand_str("AsAdAhKcKdKh2d"), (6 << 12) + 155);
assert_eq!(evaluate_hand_str("4h4c3s3c2d2c2h"), (6 << 12) + 1);
assert_eq!(evaluate_hand_str("5h4c3s3c2d2c2h"), (6 << 12) + 0);
// flushes
assert_eq!(evaluate_hand_str("AhKhQhJh9h9c9s"), (5 << 12) + 1276);
assert_eq!(evaluate_hand_str("Js7c6d5c4c3c2c"), (5 << 12) + 0);
// straights
assert_eq!(evaluate_hand_str("AhKcKdKhQcJdTs"), (4 << 12) + 9);
assert_eq!(evaluate_hand_str("Ac8c7c5d4d3d2d"), (4 << 12) + 0);
// three of a kinds
assert_eq!(evaluate_hand_str("AsAcAhKhQd5c3s"), (3 << 12) + 857);
assert_eq!(evaluate_hand_str("7d5c4c3c2d2s2h"), (3 << 12) + 8);
// two pairs
assert_eq!(evaluate_hand_str("AsAhKsKhQsQhJs"), (2 << 12) + 857);
assert_eq!(evaluate_hand_str("7c6d5h3s3c2d2h"), (2 << 12) + 3);
// one pairs
assert_eq!(evaluate_hand_str("AdAsKhQdJs3s2c"), (1 << 12) + 2859);
assert_eq!(evaluate_hand_str("8s7s5h4c3c2d2c"), (1 << 12) + 18);
// high cards
assert_eq!(evaluate_hand_str("AdKdQdJd9s3h2c"), (0 << 12) + 1276);
assert_eq!(evaluate_hand_str("9h8s7d5d4d3c2d"), (0 << 12) + 48);
}
#[test]
fn test_hand_addition() {
let hand1 = "4h4c".parse::<Hand>().unwrap();
let hand2 = "5h4s".parse::<Hand>().unwrap();
let board = "3s3c2d2c2h".parse::<Hand>().unwrap();
assert_eq!((hand1 + board).evaluate(), (6 << 12) + 1);
assert_eq!((hand2 + board).evaluate(), (6 << 12) + 0);
}
}