Refactor the game

This commit is contained in:
hal8174 2025-07-10 14:14:12 +02:00
parent b289b18030
commit c2d97173f2
Signed by: hal8174
SSH key fingerprint: SHA256:NN98ZYwnrreQLSOV/g+amY7C3yL/mS1heD7bi5t6PPw
4 changed files with 347 additions and 210 deletions

60
src/player/mod.rs Normal file
View file

@ -0,0 +1,60 @@
pub mod cli {
use crate::game::{playeble, Player};
pub struct Cli {}
impl Player for Cli {
fn select_card(
&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};
pub struct Highest {}
impl Player for Highest {
fn select_card(
&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()
}
}
}