move "is_terminal" and "is_better" logic from node.rs into bot.rs
This commit is contained in:
parent
9067459eb1
commit
4ad8f38341
|
@ -269,7 +269,8 @@ impl SegmentedAStar {
|
|||
let cand = open_set.pop().ok_or(None)?;
|
||||
let cand = unsafe { cand.0.as_node() };
|
||||
|
||||
if cand.is_terminal() {
|
||||
if cand.queue().is_empty() || cand.rating().0 {
|
||||
// terminal node; end search and back up its rating
|
||||
return Err(Some(cand));
|
||||
}
|
||||
|
||||
|
@ -293,7 +294,7 @@ impl SegmentedAStar {
|
|||
|
||||
#[cold]
|
||||
fn backup(&mut self, cand: &Node) -> bool {
|
||||
if cand.is_better(self.best()) {
|
||||
if cand.rating() > self.best().rating() {
|
||||
self.best = cand.into();
|
||||
true
|
||||
} else {
|
||||
|
@ -302,13 +303,13 @@ impl SegmentedAStar {
|
|||
}
|
||||
|
||||
fn select(&mut self) {
|
||||
let mut best = None;
|
||||
let mut best = None::<&Node>;
|
||||
self.depth = 0;
|
||||
|
||||
for (depth, set) in self.open.iter().enumerate() {
|
||||
let Some(cand) = set.peek() else { continue };
|
||||
let cand = unsafe { cand.0.as_node() };
|
||||
if best.map_or(true, |best| cand.is_better(best)) {
|
||||
if best.map_or(true, |best| cand.rating() > best.rating()) {
|
||||
best = Some(cand);
|
||||
self.depth = depth;
|
||||
}
|
||||
|
@ -330,11 +331,8 @@ impl core::cmp::Ord for AStarNode {
|
|||
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
|
||||
let lhs = unsafe { self.0.as_node() };
|
||||
let rhs = unsafe { other.0.as_node() };
|
||||
if lhs.is_better(rhs) {
|
||||
core::cmp::Ordering::Greater
|
||||
} else {
|
||||
core::cmp::Ordering::Less
|
||||
}
|
||||
// TODO: tiebreaker strategy
|
||||
lhs.rating().cmp(&rhs.rating())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -74,16 +74,6 @@ impl Node {
|
|||
self.rating
|
||||
}
|
||||
|
||||
// TODO: move this function to `bot.algorithm`
|
||||
pub fn is_better(&self, other: &Node) -> bool {
|
||||
self.rating > other.rating
|
||||
}
|
||||
|
||||
// TODO: move this function to `bot.algorithm`
|
||||
pub fn is_terminal(&self) -> bool {
|
||||
self.queue().is_empty() || self.rating.0
|
||||
}
|
||||
|
||||
/// Get the initial placement made after the root node which eventually arrives at
|
||||
/// this node.
|
||||
pub fn root_placement(&self) -> Option<Piece> {
|
||||
|
|
Loading…
Reference in New Issue