Internal tweaks

This commit is contained in:
2024-03-05 11:43:16 -08:00
parent 171d8b8d6c
commit 76a1bd423c
8 changed files with 201 additions and 82 deletions

View File

@@ -8,14 +8,21 @@ use crate::{board::Board, util::Symb};
/// - coords are the coordinate of this slot's partial
/// - char_idx is the index of this slot in its partial
/// - f32 is the influence of this slot
pub fn free_slots_by_influence(board: &Board) -> Vec<(usize, f32)> {
pub fn free_slots_by_influence(board: &Board) -> Option<Vec<(usize, f32)>> {
// Fill all empty slots with fives and compute starting value
let filled = Board::from_board(board.get_board().map(|x| match x {
None => Symb::from_char('5'),
_ => x,
}));
let filled = {
// This should always result in an evaluatable expression,
// since parenthesis do not exist.
// (the only way to divide by zero is by doing something like /(5-2+3) )
let f = Board::from_board(board.get_board().map(|x| match x {
None => Symb::from_char('5'),
_ => x,
}));
let base = filled.evaluate().unwrap();
f
};
let base = filled.evaluate()?;
// Test each slot:
// Increase its value by 1, and record its effect on the
@@ -36,13 +43,17 @@ pub fn free_slots_by_influence(board: &Board) -> Vec<(usize, f32)> {
.collect();
// Sort by most to least influence
slots.sort_by(|a, b| b.1.abs().partial_cmp(&a.1.abs()).unwrap());
slots
slots.sort_by(|a, b| {
b.1.abs()
.partial_cmp(&a.1.abs())
.unwrap_or(std::cmp::Ordering::Equal)
});
Some(slots)
}
/// Find the maximum possible value of the given board
#[allow(dead_code)]
pub fn maximize_value(board: &Board) -> Board {
pub fn maximize_value(board: &Board) -> Option<Board> {
let n_free = board.get_board().iter().filter(|x| x.is_none()).count();
// Assume we have 10 or fewer available slots
@@ -58,7 +69,7 @@ pub fn maximize_value(board: &Board) -> Board {
.filter(|x| !board.contains(*x))
.collect::<Vec<_>>();
let slots = free_slots_by_influence(&board);
let slots = free_slots_by_influence(&board)?;
let all_symbols = {
// We need this many from the bottom, and this many from the top.
@@ -125,5 +136,5 @@ pub fn maximize_value(board: &Board) -> Board {
}
}
best_board.unwrap()
best_board
}