use crate::{Playable, Strategy};
use std::sync::Arc;
#[derive(Clone)]
pub struct Player<G: Playable<P>, const P: usize> {
name: String,
new_strategy: Arc<dyn Fn() -> Strategy<G, P> + Send + Sync>,
}
impl<G: Playable<P>, const P: usize> Player<G, P> {
pub fn new(
name: String,
new_strategy: impl Fn() -> Strategy<G, P> + Send + Sync + 'static,
) -> Self {
Player {
name,
new_strategy: Arc::new(new_strategy),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn new_strategy(&self) -> Strategy<G, P> {
(self.new_strategy)()
}
}
impl<G: Playable<P>, const P: usize> std::fmt::Debug for Player<G, P> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(fmt, "Player({})", self.name)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Normal;
use impls::impls;
use test_log::test;
#[test]
fn player_is_send_sync() {
assert!(impls!(Player<Normal<(), u8, 2>, 2>: Send & Sync));
}
}