Added predict to o-x-rust
This commit is contained in:
@@ -56,3 +56,25 @@ pub fn get_score(me: u8, other: u8, first: bool, board: &[u8]) -> i32 {
|
||||
}
|
||||
score
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn predict(me: u8, other: u8, first: bool, board: &[u8]) -> usize {
|
||||
let (mut max_p, mut max_s) = (0, i32::MIN);
|
||||
let empty = count_empty(board);
|
||||
if empty == 0 {
|
||||
return 0;
|
||||
}
|
||||
for i in 0..9 {
|
||||
if board[i] != 0 {
|
||||
continue;
|
||||
}
|
||||
let copy = &mut [0u8; 9];
|
||||
copy.copy_from_slice(board);
|
||||
copy[i] = get_turn(me, other, first, empty);
|
||||
let score = get_score(me, other, first, board);
|
||||
if score > max_s {
|
||||
(max_p, max_s) = (i, score);
|
||||
}
|
||||
}
|
||||
max_p
|
||||
}
|
||||
|
Reference in New Issue
Block a user