pub struct Opponent { /* private fields */ }
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§
Source§impl Opponent
impl Opponent
Sourcepub fn new(difficulty: Difficulty) -> Self
pub fn new(difficulty: Difficulty) -> Self
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);
Sourcepub fn get_move(&self, game: &Game) -> Option<Position>
pub fn get_move(&self, game: &Game) -> Option<Position>
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."),
};
Sourcepub fn evaluate_game(&self, game: &Game) -> HashMap<Position, Outcome>
pub fn evaluate_game(&self, game: &Game) -> HashMap<Position, Outcome>
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);
}