wip stats

This commit is contained in:
2026-05-17 09:20:14 -07:00
parent 5553a7e1a1
commit 03f5e70498
4 changed files with 170 additions and 47 deletions
+60 -13
View File
@@ -10,11 +10,47 @@ mod test;
#[cfg(doctest)]
struct ReadmeDoctests;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct KlondikeConfig {}
impl Default for KlondikeConfig {
fn default() -> Self {
KlondikeConfig {}
#[derive(Clone, Copy, Debug, Default)]
enum DrawStockConfig {
#[default]
DrawOne = 1,
DrawThree = 3,
}
#[derive(Clone, Debug, Default)]
pub struct KlondikeConfig {
draw_stock: DrawStockConfig,
}
impl KlondikeConfig {
pub const fn draw_one_stock() -> Self {
KlondikeConfig {
draw_stock: DrawStockConfig::DrawOne,
}
}
pub const fn draw_three_stock() -> Self {
KlondikeConfig {
draw_stock: DrawStockConfig::DrawThree,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct KlondikeStats {
recycle_count: usize,
moves: usize,
}
impl KlondikeStats {
pub const fn new() -> Self {
KlondikeStats {
recycle_count: 0,
moves: 0,
}
}
const fn increment_recycle_count(&mut self) {
self.recycle_count += 1;
}
const fn increment_moves(&mut self) {
self.moves += 1;
}
}
@@ -486,14 +522,13 @@ impl Iterator for KlondikeIter {
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Klondike {
config: KlondikeConfig,
state: KlondikeState,
}
impl Klondike {
pub fn new_random_default() -> Self {
Self::new(Rng::default(), KlondikeConfig::default())
pub fn new_random() -> Self {
Self::new(Rng::default())
}
pub fn new(mut seed: Rng, config: KlondikeConfig) -> Self {
pub fn new(mut seed: Rng) -> Self {
// shuffle a new deck
let mut deck = Stack::full_deck(0);
use rand::seq::SliceRandom;
@@ -529,7 +564,7 @@ impl Klondike {
tableau6,
tableau7,
};
Self { config, state }
Self { state }
}
pub const fn state(&self) -> &KlondikeState {
&self.state
@@ -537,31 +572,43 @@ impl Klondike {
}
impl Game for Klondike {
type Stats = KlondikeStats;
type Config = KlondikeConfig;
type Instruction = KlondikeInstruction;
fn possible_instructions(&self) -> impl Iterator<Item = Self::Instruction> + use<> {
let state = self.state.clone();
KlondikeIter::new().filter(move |&instruction| state.is_instruction_valid(instruction))
}
fn is_instruction_valid(&self, instruction: Self::Instruction) -> bool {
fn is_instruction_valid(&self, _config: &Self::Config, instruction: Self::Instruction) -> bool {
self.state.is_instruction_valid(instruction)
}
fn process_instruction(&mut self, instruction: Self::Instruction) {
fn process_instruction(
&mut self,
stats: &mut Self::Stats,
config: &Self::Config,
instruction: Self::Instruction,
) {
match instruction {
// Reset the stock if it's empty
KlondikeInstruction::RotateStock => {
if self.state.stock.face_down().is_empty() {
self.state.stock.flip_it_and_reverse_it();
stats.increment_recycle_count();
} else {
self.state.stock.flip_up();
for _ in 0..config.draw_stock as usize {
self.state.stock.flip_up();
}
}
}
// Move a card from anywhere to a foundation
KlondikeInstruction::DstFoundation(DstFoundation { src, foundation }) => {
stats.increment_moves();
let card = self.state.take_top_card(src);
self.state.extend_foundation(foundation, card);
}
// Move a stack of cards from anywhere to a tableau
KlondikeInstruction::DstTableau(DstTableau { src, tableau }) => {
stats.increment_moves();
let cards = self.state.take_stack(src);
self.state.extend_tableau(tableau, cards);
}
+3 -3
View File
@@ -3,14 +3,14 @@ use card_game::{Game, Session};
#[test]
fn test_is_winnable() {
// is winnable
let is_winnable = Session::new(Klondike::new_random_default()).is_winnable();
let is_winnable = Session::new_default(Klondike::new_random()).is_winnable();
println!("is_winnable = {is_winnable:?}");
}
#[test]
fn test_klondike() {
// create game session
let game = Klondike::new_random_default();
let mut session = Session::new(game);
let game = Klondike::new_random();
let mut session = Session::new_default(game);
// is winnable
let is_winnable = session.is_winnable();