add benchmark
This commit is contained in:
Generated
+9
@@ -132,6 +132,15 @@ dependencies = [
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "klondike-bench"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"card_game",
|
||||
"klondike",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "klondike-cli"
|
||||
version = "0.1.0"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
members = [
|
||||
"card_game",
|
||||
"klondike",
|
||||
"klondike-bench",
|
||||
"klondike-cli",
|
||||
]
|
||||
resolver = "3"
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "klondike-bench"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
card_game.workspace = true
|
||||
klondike.workspace = true
|
||||
rand = { version = "0.10.1", default-features = false }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,30 @@
|
||||
use card_game::Game;
|
||||
use klondike::{Klondike, KlondikeConfig, KlondikeStats, Rng};
|
||||
|
||||
fn play_to_win(rng: &mut Rng) -> bool {
|
||||
// create game session
|
||||
let mut game = Klondike::with_rng(rng);
|
||||
let mut stats = KlondikeStats::new();
|
||||
const CONFIG: KlondikeConfig = KlondikeConfig {
|
||||
draw_stock: klondike::DrawStockConfig::DrawOne,
|
||||
};
|
||||
// play game a bit
|
||||
while let Some(instruction) = game.get_auto_move()
|
||||
&& !game.is_win()
|
||||
{
|
||||
game.process_instruction(&mut stats, &CONFIG, instruction);
|
||||
|
||||
// quit after 250 moves
|
||||
if 250 < stats.moves() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
game.is_win()
|
||||
}
|
||||
fn main() {
|
||||
use rand::SeedableRng;
|
||||
let mut rng = Rng::seed_from_u64(0);
|
||||
const GAMES: u32 = 10000;
|
||||
let wins: u32 = (0..GAMES).map(|_| play_to_win(&mut rng) as u32).sum();
|
||||
println!("wins = {wins}/{GAMES} win_rate = {}%", wins * 100 / GAMES);
|
||||
}
|
||||
Reference in New Issue
Block a user