Files
basic-computer-games/72_Queen/rust/src/ai.rs
Uğur Küpeli 21c0faeeb0 rust port
final
2022-05-07 01:11:36 +03:00

31 lines
550 B
Rust

use crate::util;
const PREFERRED_MOVES: [u8; 5] = [158, 72, 75, 126, 127];
const SAFE_MOVES: [u8; 2] = [44, 41];
pub fn get_computer_move(loc: u8) -> u8 {
if SAFE_MOVES.contains(&loc) {
return random_move(loc);
}
for m in PREFERRED_MOVES {
if util::is_move_legal(loc, m) && m != loc {
return m;
}
}
random_move(loc)
}
fn random_move(l: u8) -> u8 {
let r: f32 = rand::random();
if r > 0.6 {
l + 11
} else if r > 0.3 {
l + 21
} else {
l + 10
}
}