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
extern crate rand;
extern crate cards;
extern crate holdem;
extern crate pokereval;
mod utils;
mod parse;
mod internal;
use rand::{Rng};
use std::cmp::{min, max};
use std::collections::BTreeSet;
use std::io::{Error, ErrorKind};
use cards::card::{Card};
use utils::gen_random_suits;
use parse::{parse_5_chars, parse_4_chars, parse_3_chars, parse_2_chars};
use internal::RangeComponent;
pub trait Range {
fn contains(&self, hand: (&Card, &Card)) -> bool;
fn draw(&self) -> (Card, Card);
}
pub struct SimpleRange {
text: String,
components: BTreeSet<RangeComponent>,
combination_lookup: Vec<(usize, RangeComponent)>,
combination_count: usize,
}
impl SimpleRange {
pub fn new_from_string(ranges: &str) -> Result<SimpleRange, Error> {
let raw_components = ranges.split(",");
let mut components : BTreeSet<RangeComponent> = BTreeSet::new();
for component in raw_components {
let chars : Vec<char> = component.chars().collect();
let new_components = match chars.len() {
5 => { try!(parse_5_chars(chars)) },
4 => { try!(parse_4_chars(chars)) },
3 => { try!(parse_3_chars(chars)) },
2 => { try!(parse_2_chars(chars)) },
_ => { return Err(Error::new(ErrorKind::Other,
format!("Too many characters in pattern '{}'", component))); }
};
for new_component in new_components {
components.insert(new_component);
}
}
if components.len() == 0 {
return Err(Error::new(ErrorKind::Other,
"Zero length range string not allowed"));
}
let mut combination_count = 0;
let mut combination_lookup : Vec<(usize, RangeComponent)> = Vec::new();
for component in components.iter() {
match *component {
RangeComponent::Pair(_) => { combination_count+=6; },
RangeComponent::CardsSuited(_, _) => { combination_count+=4; },
RangeComponent::CardsUnsuited(_, _) => { combination_count+=12; },
}
combination_lookup.push( (combination_count, component.clone() ) );
}
Ok(SimpleRange {
text: String::from(ranges),
components: components,
combination_lookup: combination_lookup,
combination_count: combination_count,
})
}
pub fn get_range_text(&self) -> String {
self.text.clone()
}
pub fn get_component_count(&self) -> usize {
self.components.len()
}
}
impl Range for SimpleRange {
fn contains(&self, hand: (&Card, &Card)) -> bool {
let value_greater = max(hand.0.value, hand.1.value);
let value_lesser = min(hand.0.value, hand.1.value);
let mut ita = self.components.iter();
let result = if hand.0.value == hand.1.value {
ita.any(|&x| match x {
RangeComponent::Pair(val) => val == value_greater,
_ => false
})
} else if hand.0.suit == hand.1.suit {
ita.any(|&x| match x {
RangeComponent::CardsSuited(val_g, val_l) => val_g == value_greater && val_l == value_lesser,
_ => false
})
} else {
ita.any(|&x| match x {
RangeComponent::CardsUnsuited(val_g, val_l) => val_g == value_greater && val_l == value_lesser,
_ => false
})
};
result
}
fn draw(&self) -> (Card, Card) {
let mut rng = rand::thread_rng();
let n: usize = rng.gen_range(0, self.combination_count);
let mut speculative_component : Option<RangeComponent> = None;
for item in self.combination_lookup.iter() {
let (cummulative_combination_count, component) = *item;
if n < cummulative_combination_count {
speculative_component = Some(component);
break;
}
}
let range_component = speculative_component.unwrap();
let cards = match range_component {
RangeComponent::Pair(val) => {
let (suit_one, suit_two) = gen_random_suits(&mut rng);
let c1 = Card::new(val, suit_one);
let c2 = Card::new(val, suit_two);
(c1, c2)
},
RangeComponent::CardsSuited(val_g,val_l) => {
let (suit_one, _) = gen_random_suits(&mut rng);
let c1 = Card::new(val_g, suit_one);
let c2 = Card::new(val_l, suit_one);
(c1, c2)
},
RangeComponent::CardsUnsuited(val_g,val_l) => {
let (suit_one, suit_two) = gen_random_suits(&mut rng);
let c1 = Card::new(val_g, suit_one);
let c2 = Card::new(val_l, suit_two);
(c1, c2)
},
};
cards
}
}