Struct open_ttt_lib::ai::Opponent[][src]

pub struct Opponent { /* fields omitted */ }
Expand description

Provides a computer controlled AI opponent.

This can be used to create single player games or implement a hint system for human users.

Implementations

impl Opponent[src]

pub fn new(difficulty: Difficulty) -> Self[src]

Constructs a new AI opponent using the provided difficulty.

Examples

Construct a hard AI opponent:

use open_ttt_lib::ai;

let hard_opponent = ai::Opponent::new(ai::Difficulty::Hard);

Construct an AI opponent that randomly picks positions:

use open_ttt_lib::ai;

let rando = ai::Opponent::new(ai::Difficulty::None);

pub fn get_move(&self, game: &Game) -> Option<Position>[src]

Gets the position the AI opponent wishes to move based on the provided game.

None is returned if the game is over. The AI opponent never tries to select an invalid position, that is a position that is not free.

Examples

use open_ttt_lib::ai;
use open_ttt_lib::game;

let game = game::Game::new();
let ai_opponent = ai::Opponent::new(ai::Difficulty::Medium);

match ai_opponent.get_move(&game) {
    Some(position) => assert!(game.can_move(position)),
    None => panic!("The game is over so the AI opponent cannot do a move."),
};

pub fn evaluate_game(&self, game: &Game) -> HashMap<Position, Outcome>[src]

Evaluates each free position in the provided game.

Each free position in the game is mapped to an outcome for the AI opponent. If the game is over an empty map is returned.

This functionality is useful if you wish to examine how the AI opponent viewed the game. E.g. this can be helpful for creating a hint system to help human players pick a position or when fine-tuning the AI difficulty settings.

Examples

use open_ttt_lib::ai;
use open_ttt_lib::game;

let game = game::Game::new();
let ai_opponent = ai::Opponent::new(ai::Difficulty::Medium);

let outcomes = ai_opponent.evaluate_game(&game);

// Display the outcome for each position.
for (position, outcome) in outcomes {
    assert!(game.can_move(position));
    println!("position: {:?} outcome: {:?}", position, outcome);
}

Trait Implementations

impl Clone for Opponent[src]

fn clone(&self) -> Opponent[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Opponent[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Hash for Opponent[src]

fn hash<__H: Hasher>(&self, state: &mut __H)[src]

Feeds this value into the given Hasher. Read more

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given Hasher. Read more

impl PartialEq<Opponent> for Opponent[src]

fn eq(&self, other: &Opponent) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &Opponent) -> bool[src]

This method tests for !=.

impl StructuralPartialEq for Opponent[src]

Auto Trait Implementations

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

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

pub fn from(t: T) -> T[src]

Performs the conversion.

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

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

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

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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.

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

Performs the conversion.

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.

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

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V