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
use super::{lookups};
use holdem::{HandRank};
use holdem::{CactusKevCard};
fn findit(key: usize) -> usize {
let mut low = 0;
let mut high = 4887;
let mut mid;
while low <= high {
mid = (high+low) >> 1;
if key < lookups::PRODUCTS[mid] as usize {
high = mid - 1;
} else if key > lookups::PRODUCTS[mid] as usize {
low = mid + 1;
} else {
return mid;
}
}
panic!("No match found")
}
fn eval_5cards_kev_original(c1: &CactusKevCard, c2: &CactusKevCard, c3: &CactusKevCard, c4: &CactusKevCard, c5: &CactusKevCard) -> HandRank {
let q : usize = ((c1 | c2 | c3 | c4 | c5) as usize) >> 16;
if (c1 & c2 & c3 & c4 & c5 & 0xf000) != 0 {
return lookups::FLUSHES[q] as HandRank;
}
let s = lookups::UNIQUE_5[q] as HandRank;
if s != 0 {
return s;
}
let q =
((c1 & 0xff) * (c2 & 0xff) * (c3 & 0xff) * (c4 & 0xff) * (c5 & 0xff))
as usize;
let lookup = findit(q);
lookups::VALUES[lookup] as HandRank
}
pub fn eval_5cards_kev(c1: &CactusKevCard, c2: &CactusKevCard, c3: &CactusKevCard, c4: &CactusKevCard, c5: &CactusKevCard) -> HandRank {
let kev_rank = eval_5cards_kev_original(c1, c2, c3, c4, c5);
7461 - (kev_rank-1) as HandRank
}
pub fn eval_5cards_kev_array(cards: &[&CactusKevCard; 5]) -> HandRank {
let kev_rank = eval_5cards_kev_original(cards[0], cards[1], cards[2], cards[3], cards[4]);
7461 - (kev_rank-1) as HandRank
}
pub fn eval_6cards_kev_array(cards: &[&CactusKevCard; 6]) -> HandRank {
let mut tmp;
let mut best = 0;
let dummy_kev_value = 0;
let mut subhand : [&CactusKevCard; 5] = [&dummy_kev_value; 5];
for ids in lookups::PERM_6.iter() {
for i in 0..5 {
subhand[i] = cards[ids[i] as usize];
}
tmp = eval_5cards_kev_array(&subhand);
if tmp > best {
best = tmp;
}
}
best
}
pub fn eval_7cards_kev_array(cards: &[&CactusKevCard; 7]) -> HandRank {
let mut tmp;
let mut best = 0;
let dummy_kev_value = 0;
let mut subhand : [&CactusKevCard; 5] = [&dummy_kev_value; 5];
for ids in lookups::PERM_7.iter() {
for i in 0..5 {
subhand[i] = cards[ids[i] as usize];
}
tmp = eval_5cards_kev_array(&subhand);
if tmp > best {
best = tmp;
}
}
best
}
#[cfg(test)]
mod tests {
use std::collections::{HashMap};
use cards::deck::{Deck};
use holdem::{HandRank, HandRankClass, hand_rank_to_class};
use holdem::{CactusKevCard};
use super::{eval_5cards_kev_array};
use super::super::utils::{card_to_deck_number};
#[test]
fn evaluate_all_possible_5_card_combinations() {
let mut deck = Deck::new_unshuffled();
let mut cards : [CactusKevCard; 52] = [0; 52];
for i in 0..52 {
let card = deck.draw().ok().unwrap();
cards[i] = card_to_deck_number(&card);
}
let mut rank_class_count : HashMap<HandRankClass, usize> = HashMap::new();
let mut rank_count : HashMap<HandRank, bool> = HashMap::new();
let dummy_kev_value = 0;
let mut current_hand : [&CactusKevCard; 5] = [&dummy_kev_value; 5];
for i1 in 0..52 {
for i2 in (i1+1)..52 {
for i3 in (i2+1)..52 {
for i4 in (i3+1)..52 {
for i5 in (i4+1)..52 {
current_hand[0] = &cards[i1];
current_hand[1] = &cards[i2];
current_hand[2] = &cards[i3];
current_hand[3] = &cards[i4];
current_hand[4] = &cards[i5];
let rank = eval_5cards_kev_array(¤t_hand);
rank_count.entry(rank).or_insert(true);
}
}
}
}
}
for key in rank_count.keys() {
let rank_class = hand_rank_to_class(key);
let count = rank_class_count.entry(rank_class).or_insert(0);
*count += 1;
}
assert_eq!(rank_count.len(), 7462);
assert_eq!(*rank_class_count.get(&HandRankClass::HighCard).unwrap(), 1277);
assert_eq!(*rank_class_count.get(&HandRankClass::OnePair).unwrap(), 2860);
assert_eq!(*rank_class_count.get(&HandRankClass::TwoPair).unwrap(), 858);
assert_eq!(*rank_class_count.get(&HandRankClass::ThreeOfAKind).unwrap(), 858);
assert_eq!(*rank_class_count.get(&HandRankClass::Straight).unwrap(), 10);
assert_eq!(*rank_class_count.get(&HandRankClass::Flush).unwrap(), 1277);
assert_eq!(*rank_class_count.get(&HandRankClass::FullHouse).unwrap(), 156);
assert_eq!(*rank_class_count.get(&HandRankClass::FourOfAKind).unwrap(), 156);
assert_eq!(*rank_class_count.get(&HandRankClass::StraightFlush).unwrap(), 10);
}
}