From 50880013a84012b6e73745d37371015b55732751 Mon Sep 17 00:00:00 2001 From: Aaditya Murthy Date: Tue, 10 Oct 2023 15:57:32 -0500 Subject: [PATCH] random mover --- Cargo.toml | 1 + src/lib.rs | 14 +++++--------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 12eee3a..c5f146e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2.87" chess = "3.2.0" +rand = "0.8.5" diff --git a/src/lib.rs b/src/lib.rs index 3ce1564..36572a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ +use rand::prelude::*; use std::str::FromStr; -use chess::{Board, ChessMove, Error, MoveGen, Piece, Square}; +use chess::{Board, Error, MoveGen}; use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -11,13 +12,8 @@ pub fn get_next_move(fen: &str) -> JsValue { Err(_) => Board::default(), }; let movegen = MoveGen::new_legal(&board); - + let mut rng = rand::thread_rng(); // return first random move - for chess_move in movegen { - // This move does not capture anything - return JsValue::from_str(&chess_move.to_string()); - } - return JsValue::from_str( - &ChessMove::new(Square::E2, Square::E4, Some(Piece::Queen)).to_string(), - ); + let chess_move = movegen.choose(&mut rng).unwrap(); + return JsValue::from_str(&chess_move.to_string()); }