card-calculator/src/player/mod.rs
2025-07-13 22:27:26 +02:00

114 lines
2.8 KiB
Rust

pub mod single_look_ahead;
pub mod cli {
use crate::game::{playeble, Player, PlayerBuilder};
pub struct Cli {}
impl PlayerBuilder for Cli {
fn build(&self) -> Box<dyn Player> {
Box::new(Cli {})
}
}
impl Player for Cli {
fn select_card(
&mut self,
hand: &[crate::game::Card],
stack: &[crate::game::Card],
additional_information: &crate::game::AdditionalInformation,
) -> crate::game::Card {
let mut h = hand.to_vec();
h.sort();
println!("hand: {:?}", h);
h.retain(|&c| playeble(c, hand, stack.first().map(|d| d.color)));
println!("playeble: {:?}", h);
println!("stack: {:?}", stack);
println!("additional information: {:?}", additional_information);
let mut buffer = String::new();
let _ = std::io::stdin().read_line(&mut buffer);
let index = buffer.trim().parse::<usize>().unwrap();
h[index]
}
}
}
pub mod highest {
use std::cmp::Reverse;
use crate::game::{playeble, Player, PlayerBuilder};
pub struct Highest {}
impl PlayerBuilder for Highest {
fn build(&self) -> Box<dyn Player> {
Box::new(Highest {})
}
}
impl Player for Highest {
fn select_card(
&mut self,
hand: &[crate::game::Card],
stack: &[crate::game::Card],
additional_information: &crate::game::AdditionalInformation,
) -> crate::game::Card {
let _ = additional_information;
let mut hand = hand.to_vec();
hand.sort_by_key(|c| Reverse(*c));
*hand
.iter()
.find(|&&c| playeble(c, &hand, stack.first().map(|d| d.color)))
.unwrap()
}
}
}
pub mod random {
use rand::{rngs::SmallRng, seq::SliceRandom, SeedableRng};
use crate::game::{playeble, Player, PlayerBuilder};
pub struct Random {
pub seed: u64,
}
impl PlayerBuilder for Random {
fn build(&self) -> Box<dyn Player> {
Box::new(RandomPlayer {
rng: SmallRng::seed_from_u64(self.seed),
})
}
}
struct RandomPlayer {
rng: SmallRng,
}
impl Player for RandomPlayer {
fn select_card(
&mut self,
hand: &[crate::game::Card],
stack: &[crate::game::Card],
additional_information: &crate::game::AdditionalInformation,
) -> crate::game::Card {
let _ = additional_information;
let mut h = hand.to_vec();
h.sort();
h.retain(|&c| playeble(c, hand, stack.first().map(|d| d.color)));
*h.choose(&mut self.rng).unwrap()
}
}
}