Skip to content

Commit

Permalink
random mover
Browse files Browse the repository at this point in the history
  • Loading branch information
aadi123 committed Oct 10, 2023
1 parent b1c8758 commit 5088001
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.87"
chess = "3.2.0"
rand = "0.8.5"
14 changes: 5 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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());
}

0 comments on commit 5088001

Please sign in to comment.