From a28a965d12124d95b9ec21f72bc60e6cabcfb276 Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Mon, 18 May 2026 19:17:11 +0000 Subject: [PATCH] seed_from_u64 (#7) Closes #5 Reviewed-on: https://git.aleshym.co/Quaternions/card_game/pulls/7 Co-authored-by: Rhys Lloyd Co-committed-by: Rhys Lloyd --- Cargo.lock | 1 + klondike-cli/Cargo.toml | 1 + klondike-cli/src/main.rs | 11 +++++++++-- klondike/Cargo.toml | 2 +- klondike/src/lib.rs | 12 +++++++----- klondike/src/test.rs | 4 ++-- 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c2b05c6..34560f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,6 +138,7 @@ version = "0.1.0" dependencies = [ "card_game", "klondike", + "rand", ] [[package]] diff --git a/klondike-cli/Cargo.toml b/klondike-cli/Cargo.toml index b6f74d2..5816939 100644 --- a/klondike-cli/Cargo.toml +++ b/klondike-cli/Cargo.toml @@ -6,3 +6,4 @@ edition = "2024" [dependencies] card_game = { version = "0.1.0", path = "../card_game" } klondike = { version = "0.1.0", path = "../klondike" } +rand = { version = "0.10.1", default-features = false, features = ["thread_rng"] } diff --git a/klondike-cli/src/main.rs b/klondike-cli/src/main.rs index d5a58ce..973939d 100644 --- a/klondike-cli/src/main.rs +++ b/klondike-cli/src/main.rs @@ -255,10 +255,14 @@ fn get_good_move(state: &Klondike) -> Option { } fn main() -> Result<(), std::io::Error> { - let mut session = Session::new_default(Klondike::new_random()); + use rand::RngExt; + let mut rng = rand::rng(); + let mut seed = rng.random(); + let mut session = Session::new_default(Klondike::with_seed(seed)); let mut input = String::new(); loop { // display stats + println!("seed: {seed} "); println!("{}", Displayed(session.stats())); // display game println!("{}", Displayed(session.state())); @@ -273,7 +277,10 @@ fn main() -> Result<(), std::io::Error> { // run game match instruction { - SessionInstruction::New => session = Session::new_default(Klondike::new_random()), + SessionInstruction::New => { + seed = rng.random(); + session = Session::new_default(Klondike::with_seed(seed)) + } SessionInstruction::Undo => session.undo(), SessionInstruction::Exit => break Ok(()), SessionInstruction::Hint => { diff --git a/klondike/Cargo.toml b/klondike/Cargo.toml index b71a19b..96dccbf 100644 --- a/klondike/Cargo.toml +++ b/klondike/Cargo.toml @@ -5,4 +5,4 @@ edition = "2024" [dependencies] card_game = { version = "0.1.0", path = "../card_game" } -rand = { version = "0.10.1", default-features = false, features = ["thread_rng"] } +rand = { version = "0.10.1", default-features = false, features = ["std_rng"] } diff --git a/klondike/src/lib.rs b/klondike/src/lib.rs index 978e733..14e51f6 100644 --- a/klondike/src/lib.rs +++ b/klondike/src/lib.rs @@ -1,4 +1,4 @@ -pub type Rng = rand::rngs::ThreadRng; +pub type Rng = rand::rngs::StdRng; use card_game::{Card, Game, Pile, Rank, Stack}; @@ -532,14 +532,16 @@ pub struct Klondike { state: KlondikeState, } impl Klondike { - pub fn new_random() -> Self { - Self::new(Rng::default()) + pub fn with_seed(seed: u64) -> Self { + use rand::SeedableRng; + let rng = Rng::seed_from_u64(seed); + Self::with_rng(rng) } - pub fn new(mut seed: Rng) -> Self { + pub fn with_rng(mut rng: Rng) -> Self { // shuffle a new deck let mut deck = Stack::full_deck(card_game::Deck::Deck1); use rand::seq::SliceRandom; - deck.shuffle(&mut seed); + deck.shuffle(&mut rng); let mut deck = deck.into_iter(); // generate tableaus diff --git a/klondike/src/test.rs b/klondike/src/test.rs index 1eec73b..84bcf90 100644 --- a/klondike/src/test.rs +++ b/klondike/src/test.rs @@ -3,13 +3,13 @@ use card_game::Session; #[test] fn test_is_winnable() { // is winnable - let is_winnable = Session::new_default(Klondike::new_random()).is_winnable(); + let is_winnable = Session::new_default(Klondike::with_seed(123)).is_winnable(); println!("is_winnable = {is_winnable:?}"); } #[test] fn test_klondike() { // create game session - let game = Klondike::new_random(); + let game = Klondike::with_seed(123); let mut session = Session::new_default(game); // is winnable