Struct GameTree

Source
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

Source

pub fn count_max_nodes(&self) -> usize

Counts number of nodes in the longest variation

Source

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");
    }
});
Source

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");
    }
});
Source

pub fn has_variations(&self) -> bool

Checks if this GameTree has any variations

Source

pub fn count_variations(&self) -> usize

Counts number of variations in the GameTree

Source

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);
Source

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());
Source

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());

Trait Implementations§

Source§

impl Clone for GameTree

Source§

fn clone(&self) -> GameTree

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GameTree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for GameTree

Source§

fn default() -> Self

Creates an empty GameTree

Source§

impl Into<String> for &GameTree

Source§

fn into(self) -> String

Converts this type into the (usually inferred) input type.
Source§

impl Into<String> for GameTree

Source§

fn into(self) -> String

Converts this type into the (usually inferred) input type.
Source§

impl PartialEq for GameTree

Source§

fn eq(&self, other: &GameTree) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for GameTree

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.