-
Notifications
You must be signed in to change notification settings - Fork 11
/
examples.rs
76 lines (71 loc) · 2.23 KB
/
examples.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
use crate::game::*;
use crate::strategy::*;
use rand::{self, Rng};
// dummy, terrible strategy, as an example
#[derive(Clone)]
pub struct RandomStrategyConfig {
pub hint_probability: f64,
pub play_probability: f64,
}
impl GameStrategyConfig for RandomStrategyConfig {
fn initialize(&self, _: &GameOptions) -> Box<dyn GameStrategy> {
Box::new(RandomStrategy {
hint_probability: self.hint_probability,
play_probability: self.play_probability,
})
}
}
pub struct RandomStrategy {
hint_probability: f64,
play_probability: f64,
}
impl GameStrategy for RandomStrategy {
fn initialize(&self, player: Player, _: &BorrowedGameView) -> Box<dyn PlayerStrategy> {
Box::new(RandomStrategyPlayer {
hint_probability: self.hint_probability,
play_probability: self.play_probability,
me: player,
})
}
}
pub struct RandomStrategyPlayer {
hint_probability: f64,
play_probability: f64,
me: Player,
}
impl PlayerStrategy for RandomStrategyPlayer {
fn name(&self) -> String {
format!(
"random(hint={}, play={})",
self.hint_probability, self.play_probability
)
}
fn decide(&mut self, view: &BorrowedGameView) -> TurnChoice {
let p = rand::random::<f64>();
if p < self.play_probability {
TurnChoice::Play(0)
} else if view.board.hints_remaining == view.board.hints_total
|| (view.board.hints_remaining > 0 && p < self.play_probability + self.hint_probability)
{
let hint_player = view.board.player_to_left(&self.me);
let hint_card = rand::thread_rng()
.choose(view.get_hand(&hint_player))
.unwrap();
let hinted = {
if rand::random() {
// hint a color
Hinted::Color(hint_card.color)
} else {
Hinted::Value(hint_card.value)
}
};
TurnChoice::Hint(Hint {
player: hint_player,
hinted,
})
} else {
TurnChoice::Discard(0)
}
}
fn update(&mut self, _: &TurnRecord, _: &BorrowedGameView) {}
}