use crate::ConnectFour;
const NUM_STONES_PRECALCULATED_UP_TO: u8 = 7;
const PRECALCULATED_INPUT_BYTES: &[u8] = include_bytes!("./scores.dat");
const NUM_SCORES_PRECALCULATED: usize = PRECALCULATED_INPUT_BYTES.len() / (8 + 1);
static PRECALCULATED: [(u64, i8); NUM_SCORES_PRECALCULATED] = load_precalculated();
const fn load_precalculated() -> [(u64, i8); NUM_SCORES_PRECALCULATED] {
let input_bytes = PRECALCULATED_INPUT_BYTES;
let mut result = [(0, 0); NUM_SCORES_PRECALCULATED];
let mut index = 0;
let length = 8 + 1; loop {
if index == NUM_SCORES_PRECALCULATED {
break;
}
let encoded_board = u64::from_le_bytes([
input_bytes[index * length],
input_bytes[index * length + 1],
input_bytes[index * length + 2],
input_bytes[index * length + 3],
input_bytes[index * length + 4],
input_bytes[index * length + 5],
input_bytes[index * length + 6],
input_bytes[index * length + 7],
]);
let score = input_bytes[index * length + 8] as i8;
result[index] = (encoded_board, score);
index += 1;
}
result
}
pub fn precalculated_score(board: &ConnectFour) -> Option<i8> {
if board.stones() >= NUM_STONES_PRECALCULATED_UP_TO {
return None;
}
let index = PRECALCULATED
.binary_search_by_key(&board.encode(), |(k, _)| *k)
.expect("Must be precalculated");
Some(PRECALCULATED[index].1)
}