pub struct GameTree {
pub nodes: Vec<GameNode>,
pub variations: Vec<GameTree>,
}
Expand description
A game tree, containing it’s nodes and possible variations following the last node
Fields§
§nodes: Vec<GameNode>
§variations: Vec<GameTree>
Implementations§
Source§impl GameTree
impl GameTree
Sourcepub fn count_max_nodes(&self) -> usize
pub fn count_max_nodes(&self) -> usize
Counts number of nodes in the longest variation
Sourcepub fn get_unknown_nodes(&self) -> Vec<&GameNode>
pub fn get_unknown_nodes(&self) -> Vec<&GameNode>
Gets a vector of all nodes that contain a SgfToken::Unknown
token
use sgf_parser::*;
let tree: GameTree = parse("(;B[dc];W[ef]TMP[foobar](;B[aa])(;B[cc];W[ee]))").unwrap();
let unknown_nodes = tree.get_unknown_nodes();
unknown_nodes.iter().for_each(|node| {
let unknown_tokens = node.get_unknown_tokens();
assert_eq!(unknown_tokens.len(), 1);
if let SgfToken::Unknown((identifier, value)) = unknown_tokens[0] {
assert_eq!(identifier, "TMP");
assert_eq!(value, "foobar");
}
});
Sourcepub fn get_invalid_nodes(&self) -> Vec<&GameNode>
pub fn get_invalid_nodes(&self) -> Vec<&GameNode>
Gets a vector of all nodes that contain a SgfToken::Invalid
token
use sgf_parser::*;
let tree: GameTree = parse("(;B[dc];W[foobar];B[aa])(;B[cc];W[ee]))").unwrap();
let invalid_nodes = tree.get_invalid_nodes();
invalid_nodes.iter().for_each(|node| {
let invalid_tokens = node.get_invalid_tokens();
if let SgfToken::Invalid((identifier, value)) = invalid_tokens[0] {
assert_eq!(identifier, "W");
assert_eq!(value, "foobar");
}
});
Sourcepub fn has_variations(&self) -> bool
pub fn has_variations(&self) -> bool
Checks if this GameTree has any variations
Sourcepub fn count_variations(&self) -> usize
pub fn count_variations(&self) -> usize
Counts number of variations in the GameTree
Sourcepub fn get_varation_length(&self, variation: usize) -> Result<usize, SgfError>
pub fn get_varation_length(&self, variation: usize) -> Result<usize, SgfError>
Get max length of a variation
use sgf_parser::*;
let tree: GameTree = parse("(;B[dc];W[ef](;B[aa])(;B[cc];W[ee]))").unwrap();
assert_eq!(tree.get_varation_length(0).unwrap(), 1);
assert_eq!(tree.get_varation_length(1).unwrap(), 2);
Sourcepub fn iter(&self) -> GameTreeIterator<'_>
pub fn iter(&self) -> GameTreeIterator<'_>
Gets an iterator for the GameTree
use sgf_parser::*;
let tree: GameTree = parse("(;B[dc];W[ef](;B[aa])(;B[cc];W[ee]))").unwrap();
let mut iter = tree.iter();
assert_eq!(iter.count_variations(), 2);
assert!(iter.pick_variation(1).is_ok());
let mut count = 0;
iter.for_each(|node| {
assert!(!node.tokens.is_empty());
count += 1;
});
assert_eq!(count, tree.count_max_nodes());
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Checks if the tree is valid. self
is assumed to be a root tree, so it can contain
root tokens in it’s first node.
The only way to invalidate a GameTree is to have a root token in a non-root node.
The provided parse
function will not generate invalid a GameTree
, but manual creation
or modification of a game tree can create an invalid state
SgfToken::Invalid
and SgfToken::Unknown
does not invalidate the tree
use sgf_parser::*;
let valid_tree: GameTree = parse("(;SZ[19]B[dc];W[ef](;B[aa])(;B[cc];W[ee]))").unwrap();
assert!(valid_tree.is_valid());
let mut invalid_tree: GameTree = valid_tree.clone();
invalid_tree.variations[1].nodes[0].tokens.push(SgfToken::from_pair("SZ", "19"));
assert!(!invalid_tree.is_valid());