ResponseGenerator

Trait ResponseGenerator 

Source
pub trait ResponseGenerator {
    type State: State;

    // Required method
    fn generate(
        &self,
        state: &Rc<Self::State>,
        depth: i32,
    ) -> Vec<Box<Self::State>>;
}
Expand description

Response generator function object trait.

This trait defines the interface for generating all possible responses from a given state. Implementers should provide game-specific logic for move generation.

§Examples

use std::rc::Rc;
use crate::minimax::ResponseGenerator;

struct MyResponseGenerator;

impl ResponseGenerator for MyResponseGenerator {
    type State = MyGameState;

    fn generate(&self, state: &Rc<Self::State>, depth: i32) -> Vec<Box<Self::State>> {
        // Generate all valid moves for the current player
        let mut responses = Vec::new();

        // Game-specific logic to generate moves
        for possible_move in get_all_valid_moves(state) {
            let new_state = state.apply_move(possible_move);
            responses.push(Box::new(new_state));
        }

        responses
    }
}

§Implementation Notes

  • Return an empty vector if no moves are available (player cannot respond)
  • If passing is allowed in the game, include a “pass” move as a valid response
  • The depth parameter can be used for depth-dependent move generation optimizations

Required Associated Types§

Source

type State: State

The type representing game states that this generator works with

Required Methods§

Source

fn generate(&self, state: &Rc<Self::State>, depth: i32) -> Vec<Box<Self::State>>

Generates a list of all possible responses to the given state.

This method should return all legal moves available to the current player in the given state. The implementation should be game-specific and handle all rules and constraints of the particular game being played.

§Arguments
  • state - The current state to generate responses for
  • depth - Current search depth (ply number), useful for optimizations
§Returns

A vector of boxed game states representing all possible moves. Returns an empty vector if no moves are available.

§Examples
let response_gen = MyResponseGenerator::new();
let current_state = Rc::new(MyGameState::new());

let possible_moves = response_gen.generate(&current_state, 0);
println!("Found {} possible moves", possible_moves.len());
§Note

The caller gains ownership of the returned states.

§Note

Returning no responses indicates that the player cannot respond. It does not necessarily indicate that the game is over or that the player has passed. If passing is allowed, then a “pass” state should be a valid response.

Implementors§