-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.rs
67 lines (53 loc) · 1.28 KB
/
count.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
use crate::game::*;
use serde::Serialize;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Count(pub i32);
impl std::fmt::Display for Count {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self, f)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub enum Move {
Add,
Sub,
}
pub struct Unit;
impl PlayerIndex for Unit {
fn to_index(&self) -> usize {
0
}
}
#[derive(Clone)]
pub struct CountingGame;
impl Game for CountingGame {
type S = Count;
type A = Move;
type P = Unit;
fn apply(state: Self::S, m: &Self::A) -> Self::S {
Count(match m {
Move::Add => state.0 + 1,
Move::Sub => state.0 - 1,
})
}
fn generate_actions(state: &Self::S, actions: &mut Vec<Self::A>) {
if !Self::is_terminal(state) {
actions.extend(vec![Move::Add, Move::Sub]);
}
}
fn is_terminal(state: &Self::S) -> bool {
state.0 == 10
}
fn notation(_: &Self::S, m: &Self::A) -> String {
format!("{:?}", m).to_string()
}
fn winner(_: &Self::S) -> Option<Unit> {
Some(Unit)
}
fn player_to_move(_: &Self::S) -> Unit {
Unit
}
fn num_players() -> usize {
1
}
}