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:
Generated
+1
@@ -138,6 +138,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"card_game",
|
||||
"klondike",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -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"] }
|
||||
|
||||
@@ -255,10 +255,14 @@ fn get_good_move(state: &Klondike) -> Option<KlondikeInstruction> {
|
||||
}
|
||||
|
||||
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 => {
|
||||
|
||||
+1
-1
@@ -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
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user