tetris/src/actors/mod.rs

45 lines
965 B
Rust

use crate::game::{Action, Game};
use crate::playfield::{Matrix, PlayField};
use crate::tetromino::{Tetromino, TetrominoType};
use rand::RngCore;
pub mod qlearning;
#[derive(Hash, PartialEq, Eq, Clone)]
pub struct State {
matrix: Matrix,
active_piece: Option<Tetromino>,
held_piece: Option<TetrominoType>,
}
impl From<Game> for State {
fn from(game: Game) -> Self {
(&game).into()
}
}
impl From<&Game> for State {
fn from(game: &Game) -> Self {
game.playfield().clone().into()
}
}
impl From<PlayField> for State {
fn from(playfield: PlayField) -> Self {
Self {
matrix: playfield.field().clone(),
active_piece: playfield.active_piece,
held_piece: playfield.hold_piece().map(|t| t.clone()),
}
}
}
pub trait Actor {
fn get_action<T: RngCore>(
&self,
rng: &mut T,
state: &State,
legal_actions: &[Action],
) -> Action;
}