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>
impl<T: Game> Bot<T>
Sourcepub fn select<U: IntoRunCondition>(
&mut self,
state: &T,
condition: U,
) -> Option<T::Action>
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
.
Sourcepub fn detailed_select<U: IntoRunCondition>(
&mut self,
state: &T,
condition: U,
) -> Option<Action<T>>
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]);