Implement the functions to add nodes to a game

This commit is contained in:
Savanni D'Gerinel 2023-09-01 10:38:39 -04:00
parent 0700f13429
commit aa053cd10f
2 changed files with 67 additions and 32 deletions

View File

@ -1,17 +1,33 @@
use crate::{Color, Position}; use crate::{Color, Position};
use uuid::Uuid; use uuid::Uuid;
pub trait Node { #[derive(Clone, Debug, PartialEq)]
fn path_to_node<'a>(&'a self, id: Uuid) -> Vec<&'a GameNode>;
fn add_child<'a>(&'a mut self, node: GameNode) -> &'a GameNode;
fn add_annotation(&mut self, label: String, note: String);
}
pub enum GameNode { pub enum GameNode {
MoveNode(MoveNode), MoveNode(MoveNode),
SetupNode(SetupNode), SetupNode(SetupNode),
} }
pub trait Node {
fn children<'a>(&'a self) -> Vec<&'a GameNode>;
/// Provide a pre-order traversal of all of the nodes in the game tree.
fn nodes<'a>(&'a self) -> Vec<&'a GameNode> {
self.children()
.iter()
.map(|node| {
let mut children = node.nodes();
let mut v = vec![*node];
v.append(&mut children);
v
})
.flatten()
.collect::<Vec<&'a GameNode>>()
}
fn add_child<'a>(&'a mut self, node: GameNode) -> &'a mut GameNode;
fn add_annotation(&mut self, label: String, note: String);
}
impl GameNode { impl GameNode {
fn id(&self) -> Uuid { fn id(&self) -> Uuid {
match self { match self {
@ -26,12 +42,25 @@ impl GameNode {
} }
impl Node for GameNode { impl Node for GameNode {
fn path_to_node<'a>(&'a self, id: Uuid) -> Vec<&'a GameNode> { fn children<'a>(&'a self) -> Vec<&'a GameNode> {
unimplemented!() match self {
GameNode::MoveNode(node) => node.children(),
GameNode::SetupNode(node) => node.children(),
}
} }
fn add_child<'a>(&'a mut self, node: GameNode) -> &'a GameNode { fn nodes<'a>(&'a self) -> Vec<&'a GameNode> {
unimplemented!() match self {
GameNode::MoveNode(node) => node.nodes(),
GameNode::SetupNode(node) => node.nodes(),
}
}
fn add_child<'a>(&'a mut self, new_node: GameNode) -> &'a mut GameNode {
match self {
GameNode::MoveNode(node) => node.add_child(new_node),
GameNode::SetupNode(node) => node.add_child(new_node),
}
} }
fn add_annotation(&mut self, label: String, note: String) { fn add_annotation(&mut self, label: String, note: String) {
@ -49,22 +78,18 @@ impl GameTree {
Self { children: vec![] } Self { children: vec![] }
} }
fn nodes<'a>(&'a self) -> impl Iterator<Item = &'a GameNode> { fn add_child<'a>(&'a mut self, node: GameNode) -> &'a mut GameNode {
vec![].into_iter() self.children.push(node);
} self.children.last_mut().unwrap()
fn add_child<'a>(&'a mut self, node: GameNode) -> &'a GameNode {
// self.children.push(node);
unimplemented!()
} }
} }
impl Node for GameTree { impl Node for GameTree {
fn path_to_node<'a>(&'a self, id: Uuid) -> Vec<&'a GameNode> { fn children<'a>(&'a self) -> Vec<&'a GameNode> {
unimplemented!() self.children.iter().collect::<Vec<&'a GameNode>>()
} }
fn add_child<'a>(&'a mut self, node: GameNode) -> &'a GameNode { fn add_child<'a>(&'a mut self, node: GameNode) -> &'a mut GameNode {
unimplemented!() unimplemented!()
} }
@ -73,6 +98,7 @@ impl Node for GameTree {
} }
} }
#[derive(Clone, Debug, PartialEq)]
pub struct MoveNode { pub struct MoveNode {
id: Uuid, id: Uuid,
@ -96,12 +122,13 @@ impl MoveNode {
} }
impl Node for MoveNode { impl Node for MoveNode {
fn path_to_node<'a>(&'a self, id: Uuid) -> Vec<&'a GameNode> { fn children<'a>(&'a self) -> Vec<&'a GameNode> {
unimplemented!() self.children.iter().collect::<Vec<&'a GameNode>>()
} }
fn add_child<'a>(&'a mut self, node: GameNode) -> &'a GameNode { fn add_child<'a>(&'a mut self, node: GameNode) -> &'a mut GameNode {
unimplemented!() self.children.push(node);
self.children.last_mut().unwrap()
} }
fn add_annotation(&mut self, label: String, note: String) { fn add_annotation(&mut self, label: String, note: String) {
@ -109,6 +136,7 @@ impl Node for MoveNode {
} }
} }
#[derive(Clone, Debug, PartialEq)]
pub struct SetupNode { pub struct SetupNode {
id: Uuid, id: Uuid,
@ -129,11 +157,11 @@ impl SetupNode {
} }
impl Node for SetupNode { impl Node for SetupNode {
fn path_to_node<'a>(&'a self, id: Uuid) -> Vec<&'a GameNode> { fn children<'a>(&'a self) -> Vec<&'a GameNode> {
unimplemented!() self.children.iter().collect::<Vec<&'a GameNode>>()
} }
fn add_child<'a>(&'a mut self, node: GameNode) -> &'a GameNode { fn add_child<'a>(&'a mut self, node: GameNode) -> &'a mut GameNode {
unimplemented!() unimplemented!()
} }
@ -165,17 +193,22 @@ mod test {
#[test] #[test]
fn it_can_create_an_empty_game_tree() { fn it_can_create_an_empty_game_tree() {
let tree = GameTree::new(); let tree = GameTree::new();
assert_eq!(tree.nodes().count(), 0); assert_eq!(tree.nodes().len(), 0);
} }
#[test] #[test]
fn it_can_add_moves_to_a_game() { fn it_can_add_moves_to_a_game() {
let mut tree = GameTree::new(); let mut tree = GameTree::new();
let node = MoveNode::new(Color::Black, Position {}); let first_move = MoveNode::new(Color::Black, Position {});
let mut node = tree.add_child(GameNode::MoveNode(node)); let first_ = tree.add_child(GameNode::MoveNode(first_move.clone()));
let new_node = MoveNode::new(Color::White, Position {}); let second_move = MoveNode::new(Color::White, Position {});
node.add_child(new_node); first_.add_child(GameNode::MoveNode(second_move.clone()));
let nodes = tree.nodes();
assert_eq!(nodes.len(), 2);
assert_eq!(nodes[0].id(), first_move.id);
assert_eq!(nodes[1].id(), second_move.id);
} }
#[test] #[test]

View File

@ -18,11 +18,13 @@ pub enum Error {
InvalidSgf(VerboseNomError), InvalidSgf(VerboseNomError),
} }
#[derive(Clone, Debug, PartialEq)]
pub enum Color { pub enum Color {
Black, Black,
White, White,
} }
#[derive(Clone, Debug, PartialEq)]
pub struct Position {} pub struct Position {}
#[derive(Debug)] #[derive(Debug)]