[][src]Struct rubot::alpha_beta::Bot

pub struct Bot<T: Game> { /* fields omitted */ }

A game bot which analyses its moves using alpha beta pruning with iterative deepening. In case select terminates before condition returned true, the result is always the best possible move. While this bot caches some data during computation, it does not require a lot of memory and will not store anything between different select calls.

This bot requires Game to be implemented for your game.

Examples

use rubot::{Bot, ToCompletion, tree::Node};
use std::time::Duration;

let tree = Node::root().with_children(&[
    Node::new(false, 7).with_children(&[
        Node::new(true, 4),
        Node::new(true, 2),
    ]),
    Node::new(false, 5).with_children(&[
        Node::new(true, 8),
        Node::new(true, 9)
    ]),
    Node::new(false, 6),
]);

// Create a new bot for the currently active player.
let mut bot = Bot::new(true);

// Find the best possible action.
let best = bot.select(&tree, ToCompletion);
// Search for at most 2 seconds and return the best answer found.
// As 2 seconds are more than enough for this simple tree, this will
// return the best possible action without spending this much time.
let limited = bot.select(&tree, Duration::from_secs(2));

assert_eq!(best, Some(1));
assert_eq!(limited, Some(1));

Please visit select for a simple example.

Implementations

impl<T: Game> Bot<T>[src]

pub fn new(player: T::Player) -> Self[src]

Creates a new Bot for the given player.

pub fn select<U: IntoRunCondition>(
    &mut self,
    state: &T,
    condition: U
) -> Option<T::Action>
[src]

Returns a chosen action based on the given game state.

Returns None if no Action is possible or the bot is currently not the active player.

This method runs until either the best possible action was found or one of RunCondition::depth and RunCondition::step returned false.

pub fn detailed_select<U: IntoRunCondition>(
    &mut self,
    state: &T,
    condition: U
) -> Option<Action<T>>
[src]

Similar to select, except that this function also returns the principal variation and the final evaluation of the given action.

The actions are sorted in order they are executed, so action.path[0] is always equal to the result of select.

use rubot::{Bot, ToCompletion, tree::Node};

let tree = Node::root().with_children(&[
    Node::new(true, 4),
    Node::new(true, 0).with_children(&[
        Node::new(true, 5), // This is the best possible result.
        Node::new(true, 3),
    ])
]);

assert_eq!(&Bot::new(true)
    .detailed_select(&tree, ToCompletion)
    .unwrap()
    .path, &[1, 0]);

Auto Trait Implementations

impl<T> RefUnwindSafe for Bot<T> where
    <T as Game>::Player: RefUnwindSafe

impl<T> Send for Bot<T> where
    <T as Game>::Player: Send

impl<T> Sync for Bot<T> where
    <T as Game>::Player: Sync

impl<T> Unpin for Bot<T> where
    <T as Game>::Player: Unpin

impl<T> UnwindSafe for Bot<T> where
    <T as Game>::Player: UnwindSafe

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

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

impl<T> Tap for T[src]

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.