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 = [
|
dependencies = [
|
||||||
"card_game",
|
"card_game",
|
||||||
"klondike",
|
"klondike",
|
||||||
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
card_game = { version = "0.1.0", path = "../card_game" }
|
card_game = { version = "0.1.0", path = "../card_game" }
|
||||||
klondike = { version = "0.1.0", path = "../klondike" }
|
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> {
|
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();
|
let mut input = String::new();
|
||||||
loop {
|
loop {
|
||||||
// display stats
|
// display stats
|
||||||
|
println!("seed: {seed} ");
|
||||||
println!("{}", Displayed(session.stats()));
|
println!("{}", Displayed(session.stats()));
|
||||||
// display game
|
// display game
|
||||||
println!("{}", Displayed(session.state()));
|
println!("{}", Displayed(session.state()));
|
||||||
@@ -273,7 +277,10 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
|
|
||||||
// run game
|
// run game
|
||||||
match instruction {
|
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::Undo => session.undo(),
|
||||||
SessionInstruction::Exit => break Ok(()),
|
SessionInstruction::Exit => break Ok(()),
|
||||||
SessionInstruction::Hint => {
|
SessionInstruction::Hint => {
|
||||||
|
|||||||
+1
-1
@@ -5,4 +5,4 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
card_game = { version = "0.1.0", path = "../card_game" }
|
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};
|
use card_game::{Card, Game, Pile, Rank, Stack};
|
||||||
|
|
||||||
@@ -532,14 +532,16 @@ pub struct Klondike {
|
|||||||
state: KlondikeState,
|
state: KlondikeState,
|
||||||
}
|
}
|
||||||
impl Klondike {
|
impl Klondike {
|
||||||
pub fn new_random() -> Self {
|
pub fn with_seed(seed: u64) -> Self {
|
||||||
Self::new(Rng::default())
|
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
|
// shuffle a new deck
|
||||||
let mut deck = Stack::full_deck(card_game::Deck::Deck1);
|
let mut deck = Stack::full_deck(card_game::Deck::Deck1);
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
deck.shuffle(&mut seed);
|
deck.shuffle(&mut rng);
|
||||||
let mut deck = deck.into_iter();
|
let mut deck = deck.into_iter();
|
||||||
|
|
||||||
// generate tableaus
|
// generate tableaus
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ use card_game::Session;
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_is_winnable() {
|
fn test_is_winnable() {
|
||||||
// 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:?}");
|
println!("is_winnable = {is_winnable:?}");
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_klondike() {
|
fn test_klondike() {
|
||||||
// create game session
|
// create game session
|
||||||
let game = Klondike::new_random();
|
let game = Klondike::with_seed(123);
|
||||||
let mut session = Session::new_default(game);
|
let mut session = Session::new_default(game);
|
||||||
|
|
||||||
// is winnable
|
// is winnable
|
||||||
|
|||||||
Reference in New Issue
Block a user