Struct Bot

Source
pub struct Bot<T: Game> { /* private fields */ }
Expand description

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§

Source§

impl<T: Game> Bot<T>

Source

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

Creates a new Bot for the given player.

Source

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

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.

Source

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

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> Freeze for Bot<T>
where <T as Game>::Player: Freeze,

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Tap for T

Source§

fn tap<F>(self, f: F) -> T
where F: FnOnce(&mut T),

Executes a closure on an object, returning it afterwards. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.