Board: add random

This commit is contained in:
Simon Gardling 2025-03-03 11:35:36 -05:00
parent cb463ed492
commit c648be20aa
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -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(