[][src]Struct connect6::policy::Simulate

pub struct Simulate {
    pub turn: Player,
    pub num_remain: i32,
    pub pos: Option<(usize, usize)>,
    pub node: Rc<RefCell<Node>>,
}

Game simulator with shared memory for efficient tree searching

It provides simulation structure and some utilities to make next decision in policy.

It shares possible selections and board by struct Node to make simulation without copying it. It generate new simulation with method simulate and recover the shared memory Node when it dropped. It can also simulate itself mutablely by simulate_in and recover it by rollback_in.

Examples

let game = Game::new();
let sim = Simulate::from_game(&game);
{
    let sim2 = sim.simulate(0, 0);
    let board = sim2.board();
    assert_eq!(board[0][0], Player::Black);
}
let board = sim.board();
assert_eq!(board[0][0], Player::None);

Fields

turn: Playernum_remain: i32pos: Option<(usize, usize)>node: Rc<RefCell<Node>>

Methods

impl Simulate[src]

pub fn new() -> Simulate[src]

Construct a new Simulate.

pub fn from_game(game: &Game) -> Simulate[src]

Construct a Simulate from the game

Examples

let game = Game::new();
let sim = Simulate::from_game(&game);

pub fn deep_clone(&self) -> Simulate[src]

Deep clone the simulation.

With Rc<RefCell<Node>>, the default Clone implementation make the shallow copy of Node. By this reason, we require the deep_clone implementation which makes the deep copy of Node.

pub fn board(&self) -> Board[src]

Get the board from node.

pub fn possible(&self) -> Vec<(usize, usize)>[src]

Get the possible selections from node.

pub fn search_winner(&self) -> Player[src]

Find the winner of game

Examples

let game = Game::new();
let sim = Simulate::from_game(&game);
assert_eq!(game.is_game_end(), sim.search_winner());

pub fn validate(&self, row: usize, col: usize) -> bool[src]

Validate the position, check invalid position err or already selected position err.

Examples

let game = Game::new();
let sim = Simulate::from_game(&game);
assert!(sim.validate(0, 0));

let sim2 = sim.simulate(0, 0);
assert!(!sim2.validate(0, 0));

pub fn next_turn(&self) -> Player[src]

Return next turn of game.

pub fn simulate(&self, row: usize, col: usize) -> Simulate[src]

Make the new simulation with given position.

By memory sharing of structure Node, once the new simulation is created, node of parents simulations would be also modified. This means, valid simulation is only one in the same time. Simulation will recover the shared memory when it dropped. It can make the tree searching more efficiently and precisely under the borrowing system of Rust.

Examples

let sim = Simulate::new();
assert_eq!(sim.board()[0][0], Player::None);
{
    let sim2 = sim.simulate(0, 0);
    assert_eq!(sim.board()[0][0], Player::Black);
}
assert_eq!(sim.board()[0][0], Player::None);

pub fn simulate_in(&mut self, row: usize, col: usize)[src]

Modify the current state to simulate given position.

It is for making simulation in the loop. It modify the inner state to simulate given position and do not make the new one. If you want to recover the inner state, reference rollback_in

Examples

let mut sim = Simulate::new();
assert_eq!(sim.board()[0][0], Player::None);
sim.simulate_in(0, 0);
assert_eq!(sim.board()[0][0], Player::Black);

pub fn rollback_in(&mut self, row: usize, col: usize)[src]

Modify the inner state to recover the given simulation.

It clear the given position and switching turn properly.

Examples

let mut sim = Simulate::new();
sim.simulate_in(0, 0);
assert_eq!(sim.board()[0][0], Player::Black);
sim.rollback_in(0, 0);
assert_eq!(sim.board()[0][0], Player::None);

Trait Implementations

impl Drop for Simulate[src]

Auto Trait Implementations

impl !Send for Simulate

impl !Sync for Simulate

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Erased for T