Trait GameState

Source
pub trait GameState: Clone {
    type Move: Clone;

    // Required methods
    fn next_player(&self) -> Player;
    fn apply_move(&mut self, mov: Self::Move);
    fn for_each_move<F: FnMut(Self::Move)>(&self, f: F);
    fn eval_score(&self, player: Player) -> i32;

    // Provided methods
    fn can_move(&self) -> bool { ... }
    fn solve(&self, depth: usize) -> Option<Self::Move> { ... }
}

Required Associated Types§

Required Methods§

Source

fn next_player(&self) -> Player

Source

fn apply_move(&mut self, mov: Self::Move)

Source

fn for_each_move<F: FnMut(Self::Move)>(&self, f: F)

Source

fn eval_score(&self, player: Player) -> i32

Provided Methods§

Source

fn can_move(&self) -> bool

Examples found in repository?
examples/tictactoe.rs (line 98)
89fn main() {
90    let mut board = TicTacToe {
91        board: [[None; 3]; 3],
92        next_player: Player::One,
93    };
94
95    loop {
96        println!("{}", board);
97
98        if !board.can_move() {
99            let p1 = board.eval_score(Player::One);
100            println!(
101                "You {}!",
102                if p1 > 0 {
103                    "won"
104                } else if p1 < 0 {
105                    "lost"
106                } else {
107                    "drew"
108                }
109            );
110            break;
111        }
112
113        if board.next_player() == Player::One {
114            let (x, y) = loop {
115                let mut buf = String::new();
116                println!("Enter a location(i.e: 0 1)");
117                std::io::stdin().read_line(&mut buf).unwrap();
118                match buf
119                    .split_whitespace()
120                    .map(|s| s.parse())
121                    .collect::<Result<Vec<_>, _>>()
122                {
123                    Ok(v) if v.len() == 2 => break (v[0], v[1]),
124                    _ => {}
125                }
126            };
127            board.apply_move((x, y));
128        } else {
129            println!("AI is thinking...");
130            if let Some(mov) = board.solve(8) {
131                board.apply_move(mov);
132            } else {
133                break;
134            }
135        }
136    }
137}
Source

fn solve(&self, depth: usize) -> Option<Self::Move>

Examples found in repository?
examples/tictactoe.rs (line 130)
89fn main() {
90    let mut board = TicTacToe {
91        board: [[None; 3]; 3],
92        next_player: Player::One,
93    };
94
95    loop {
96        println!("{}", board);
97
98        if !board.can_move() {
99            let p1 = board.eval_score(Player::One);
100            println!(
101                "You {}!",
102                if p1 > 0 {
103                    "won"
104                } else if p1 < 0 {
105                    "lost"
106                } else {
107                    "drew"
108                }
109            );
110            break;
111        }
112
113        if board.next_player() == Player::One {
114            let (x, y) = loop {
115                let mut buf = String::new();
116                println!("Enter a location(i.e: 0 1)");
117                std::io::stdin().read_line(&mut buf).unwrap();
118                match buf
119                    .split_whitespace()
120                    .map(|s| s.parse())
121                    .collect::<Result<Vec<_>, _>>()
122                {
123                    Ok(v) if v.len() == 2 => break (v[0], v[1]),
124                    _ => {}
125                }
126            };
127            board.apply_move((x, y));
128        } else {
129            println!("AI is thinking...");
130            if let Some(mov) = board.solve(8) {
131                board.apply_move(mov);
132            } else {
133                break;
134            }
135        }
136    }
137}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§