StaticEvaluator

Trait StaticEvaluator 

Source
pub trait StaticEvaluator<G> {
    // Required methods
    fn evaluate(&self, state: &G) -> f32;
    fn alice_wins_value(&self) -> f32;
    fn bob_wins_value(&self) -> f32;
}
Expand description

An interface for static evaluation functions.

A static evaluation function assigns a value to a game state without any lookahead. The value represents the “goodness” of the state from Alice’s perspective. Alice seeks to maximize the value and Bob seeks to minimize it.

The values returned by the static evaluation function should be in the range [bobWinsValue(), aliceWinsValue()]. If the game is over and Alice has won, then the function should return aliceWinsValue(). If the game is over and Bob has won, then the function should return bobWinsValue().

§Examples

use game_player::StaticEvaluator;

// Simple game state for demonstration
#[derive(Debug)]
struct MockGameState {
    player_score: i32,
    opponent_score: i32,
    game_over: bool,
    winner: Option<bool>, // true = Alice wins, false = Bob wins
}

// Simple static evaluator implementation
struct SimpleEvaluator;

impl StaticEvaluator<MockGameState> for SimpleEvaluator {
    fn evaluate(&self, state: &MockGameState) -> f32 {
        if state.game_over {
            match state.winner {
                Some(true) => self.alice_wins_value(),
                Some(false) => self.bob_wins_value(),
                None => 0.0, // Draw
            }
        } else {
            // Simple score difference evaluation
            let diff = state.player_score - state.opponent_score;
            (diff as f32) * 0.1 // Scale to reasonable range
        }
    }

    fn alice_wins_value(&self) -> f32 {
        1000.0
    }

    fn bob_wins_value(&self) -> f32 {
        -1000.0
    }
}

let evaluator = SimpleEvaluator;

// Test ongoing game evaluation
let ongoing_state = MockGameState {
    player_score: 15,
    opponent_score: 10,
    game_over: false,
    winner: None,
};
let score = evaluator.evaluate(&ongoing_state);
assert_eq!(score, 0.5); // (15 - 10) * 0.1

// Test Alice wins
let alice_wins_state = MockGameState {
    player_score: 21,
    opponent_score: 15,
    game_over: true,
    winner: Some(true),
};
assert_eq!(evaluator.evaluate(&alice_wins_state), 1000.0);

// Test Bob wins
let bob_wins_state = MockGameState {
    player_score: 10,
    opponent_score: 21,
    game_over: true,
    winner: Some(false),
};
assert_eq!(evaluator.evaluate(&bob_wins_state), -1000.0);

// Verify win values
assert_eq!(evaluator.alice_wins_value(), 1000.0);
assert_eq!(evaluator.bob_wins_value(), -1000.0);
assert!(evaluator.alice_wins_value() > evaluator.bob_wins_value());

Required Methods§

Source

fn evaluate(&self, state: &G) -> f32

Evaluates the given state and returns its value from Alice’s perspective.

§Arguments
  • state - The state to be evaluated
§Returns

The value of the state from Alice’s perspective

§Note

This function must be implemented.

Source

fn alice_wins_value(&self) -> f32

Returns the value that indicates that Alice has won.

§Returns

The value that indicates that Alice has won

§Note

This function must be implemented.

Source

fn bob_wins_value(&self) -> f32

Returns the value that indicates that Bob has won.

§Returns

The value that indicates that Bob has won

§Note

This function must be implemented.

Implementors§