movetree

package
v0.0.0-...-043dd78 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package movetree contains logic for managing trees of moves in a game or problem. A movetree can be serialized to / deserialized from an SGF.

Index

Constants

This section is empty.

Variables

View Source
var ErrApplyTreepath = errors.New("error applying treepath")
View Source
var ErrParseTreepath = errors.New("error parsing treepath")

Functions

This section is empty.

Types

type GameInfo

type GameInfo struct {
	// Size of the board, where 19 = 19x19. Between 1 and 25 inclusive. A value of
	// 0 should be taken to mean 'unspecified' and treated as 19x19.
	Size int

	// Komi are points added to the player with the white stones as compensation for playing second.
	// Komi must have a decimal value of .0 or .5 (ex: 6.5)
	Komi *float64

	// Initial player turn. This is traditionally the player with the black stones
	Player color.Color
}

GameInfo contains typed game properties that can exist only on the root.

type MoveTree

type MoveTree struct {
	Root *Node
}

MoveTree contains the game tree information for a go game.

func New

func New() *MoveTree

New creates a MoveTree.

type Node

type Node struct {

	// Children of this position.
	Children []*Node

	// Parent of this node.
	Parent *Node

	// Move indicates a move in the game. A move with a color + no intersection is
	// used to indicate a pass.
	Move *move.Move

	// Placements are stones that are used for setup, but actual moves. For
	// example, handicap stones will be in in placements.
	Placements move.List

	// Comment is the comment for the current node.
	Comment string

	// GameInfo contains properties only found on the root. Should be nil on
	// non-root nodes.
	GameInfo *GameInfo

	// SGFProperties contain all the raw/unprocessed properties
	SGFProperties map[string][]string
	// contains filtered or unexported fields
}

Node contains Properties, Children nodes, and Parent node.

func NewNode

func NewNode() *Node

NewNode creates a Node.

func (*Node) AddChild

func (n *Node) AddChild(nn *Node)

AddChild adds a child node.

func (*Node) AnalysisData

func (n *Node) AnalysisData() interface{}

AnalysisData gets the attached analysis data, returning nil if the analysisData is empty.

func (*Node) MoveNum

func (n *Node) MoveNum() int

MoveNum returns the current move number.

func (*Node) Next

func (n *Node) Next(variation int) *Node

Next gets the next node, given the variation number, returning nil if no node is available.

func (*Node) SetAnalysisData

func (n *Node) SetAnalysisData(an interface{})

SetAnalysisData sets the analysis data.

func (*Node) Traverse

func (n *Node) Traverse(fn func(node *Node))

Traverse Traverses the tree using BFS.

func (*Node) TraverseMainBranch

func (n *Node) TraverseMainBranch(fn func(node *Node))

TraverseMainBranch Traverses the 0th variation nodes (Main Branch). This Traversal uses BFS.

func (*Node) VarNum

func (n *Node) VarNum() int

VarNum returns the variation number.

type Path

type Path []int

A Path is a list of variations that says how to travel through a tree of moves. And has two forms a list version and a string version. First, the list-version:

[0,1,0]

Means we will first take the 0th variation, then we will take the 1ist variation, and lastly we will take the 0th variation again. For convenience, the treepath can also be specified by a string, which is where the fun begins. At its simpliest,

[0,0,0] becomes 0.0.0

func ParsePath

func ParsePath(path string) (Path, error)

ParsePath parses a treepath from a string to an array of ints (Path).

There are a couple different string short-hands that make using treepaths a little easier

0-1x2   Take the 0th variation, then repeat taking the 1st varation twice

Paths say how to get from position n to position m. Thus the numbers are always variations, separated by '.' except in the case of A:B syntax, which means repeat A for B times.

A leading . is an optional. It can be useful to indicate the root node.

Some examples:

  • becomes [] 0 becomes [0] -0 becomes [0] 1 becomes [1] 53 becomes [53] (the 53rd variation) 2-3 becomes [2,3] 0-0-0-0 becomes [0,0,0,0] 0x4 becomes [0,0,0,0] 1x4 becomes [1,1,1,1] 1-2x1-0-2x3 becomes [1,2,0,2,2,2]

func (Path) Apply

func (tp Path) Apply(n *Node) *Node

Apply applies a treepath to a node, moving down a tree of moves. This takes the variation listed in the treepath until either:

1. There are no more variation numbers in the treepath. 2. There is not a child with the given variation number.

func (Path) ApplyToBoard

func (tp Path) ApplyToBoard(n *Node, b *board.Board) (*board.Board, move.List, error)

ApplyToBoard applies a treepath to a Go-Board, returning the captured stones, or an error if the application was unsuccessful.

A board copy, and the relevant captures are returned

func (Path) Clone

func (tp Path) Clone() Path

Clone makes a copy of the treepath.

func (Path) CompactString

func (tp Path) CompactString() string

CompactString returns the treepath as a CompactString (short-hand). examples:

[]                  becomes "-"
[1]                 becomes "-1"
[0,0,0,0]           becomes "-0x4"
[1,1,1,1]           becomes "-1x4"
[1,2,0,0,2,2,2]     becomes "-1-2-0x2-2x3"

func (Path) String

func (tp Path) String() string

String returns the treepath as a string. examples:

[]                  becomes "[]"
[1]                 becomes "[1]"
[1,2,0,2,2,2]       becomes "[1,2,0,2,2,2]"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL