Set up the game review page along with #229

Merged
savanni merged 24 commits from otg/game-review into main 2024-03-31 23:37:51 +00:00
1 changed files with 15 additions and 11 deletions
Showing only changes of commit b481d5d058 - Show all commits

View File

@ -286,7 +286,7 @@ impl<T> Tree<T> {
next_idx next_idx
} }
pub fn depth(&self) -> usize { pub fn max_depth(&self) -> usize {
self.nodes.iter().fold( self.nodes.iter().fold(
0, 0,
|max, node| if node.depth > max { node.depth } else { max }, |max, node| if node.depth > max { node.depth } else { max },
@ -295,7 +295,8 @@ impl<T> Tree<T> {
// indent represents the indentation that should be applied to all children in this tree. It // indent represents the indentation that should be applied to all children in this tree. It
// amounts to the position of the parent node. // amounts to the position of the parent node.
pub fn position(&self, indent: usize, idx: usize) -> usize { pub fn position(&self, indent: usize, idx: usize) -> (usize, usize) {
println!("[{}]", idx);
let node = &self.nodes[idx]; let node = &self.nodes[idx];
match node.parent { match node.parent {
Some(parent_idx) => { Some(parent_idx) => {
@ -305,11 +306,12 @@ impl<T> Tree<T> {
.iter() .iter()
.take_while(|n| **n != node.id) .take_while(|n| **n != node.id)
.fold(0, |acc, n| acc + self.width(*n)); .fold(0, |acc, n| acc + self.width(*n));
indent + sibling_width + 1 println!("[{}] sibling width {}", idx, sibling_width);
(node.depth, indent + sibling_width)
} }
// Root nodes won't have a parent, so just put them in column 1 // Root nodes won't have a parent, so just put them in the first column
None => 1, None => (0, 0),
} }
} }
@ -452,7 +454,7 @@ mod test {
let tree = Tree::from(&game_tree); let tree = Tree::from(&game_tree);
assert_eq!(tree.depth(), 3); assert_eq!(tree.max_depth(), 3);
} }
// A // A
@ -487,10 +489,12 @@ mod test {
let tree = Tree::from(&game_tree); let tree = Tree::from(&game_tree);
assert_eq!(tree.position(0, 2), 1); assert_eq!(tree.position(0, 2), (2, 0));
assert_eq!(tree.position(0, 1), 1); assert_eq!(tree.position(0, 1), (1, 0));
assert_eq!(tree.position(0, 0), 1); assert_eq!(tree.position(0, 0), (0, 0));
assert_eq!(tree.position(0, 4), 2); assert_eq!(tree.position(0, 4), (3, 1));
assert_eq!(tree.position(0, 5), 3); assert_eq!(tree.position(0, 5), (3, 2));
assert_eq!(tree.position(0, 6), (1, 3));
assert_eq!(tree.position(0, 7), (1, 4));
} }
} }