Refactor the game
This commit is contained in:
parent
b289b18030
commit
c2d97173f2
4 changed files with 347 additions and 210 deletions
60
src/player/mod.rs
Normal file
60
src/player/mod.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue