seed_from_u64 (#7)

Closes #5

Reviewed-on: #7
Co-authored-by: Rhys Lloyd <krakow20@gmail.com>
Co-committed-by: Rhys Lloyd <krakow20@gmail.com>
This commit was merged in pull request #7.
This commit is contained in:
2026-05-18 19:17:11 +00:00
committed by Quaternions
parent 25760d19a1
commit a28a965d12
6 changed files with 21 additions and 10 deletions
+1 -1
View File
@@ -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"] }
+7 -5
View File
@@ -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
+2 -2
View File
@@ -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