From c648be20aa2e6a7142c7dd67abf80d5dd1384f09 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 3 Mar 2025 11:35:36 -0500 Subject: [PATCH] Board: add `random` --- src/repr/board.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/repr/board.rs b/src/repr/board.rs index 27af858..58a96a3 100644 --- a/src/repr/board.rs +++ b/src/repr/board.rs @@ -5,6 +5,7 @@ use super::{ CoordAxis, CoordPair, }; use const_fn::const_fn; +use rand::{seq::IteratorRandom, Rng}; use std::{cmp::Ordering, fmt, sync::LazyLock}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -97,6 +98,21 @@ impl Board { } } + pub fn random(steps: usize) -> Self { + let mut new = Self::new().starting_pos(); + let mut p = Piece::Black; + let mut rng = rand::rng(); + for _ in 0..steps { + if let Some(m) = new.possible_moves(p).choose(&mut rng) { + new.place_unchecked(m, p); + } + + p = !p; + } + + new + } + /// Starting position pub fn starting_pos(mut self) -> Self { self.place_unchecked(