Games::Go::Sgf2Dg::Diagram3pm
Encapsulate a go diagram
Usage
use Games::Go::Sgf2Dg::Diagrammy $diagram = Games::Go::Sgf2Dg::Diagram->new (options);$diagram->put($coords, 'white' | 'black', ? number ?);$diagram->mark($coords);$diagram->label($coords, 'a');$diagram->get($coords);my $new_diagram = $diagram->next;
Overview
A Games::Go::Sgf2Dg::Diagram object represents a diagram similar to those seen in go textbooks and magazines. Most of the properties defined in SGF FF[4] are supported.
The caller puts 'white' or 'black' stones (possibly numbered), on the intersection selected by $coords. The caller may mark and label intersections and stones.
put, mark, label and property are 'actions'. Actions are provisional until the node method is called. If any provisioanl actions cause a conflict, none of the actions associated with the node are applied, and the node method either calls a user-defined callback function, or returns an error.
When a conflict occurs, the caller should dispose of the current Diagram by getting the information from each intersection and doing something (like printing it). Then the caller converts the Diagram to the starting point of the next diagram by calling the clear method. Alternatively, the caller may save the current Diagram and create the starting point for the next diagram by calling the next method. clear and next may also be called at arbitrary times (for example, to launch a variation diagram).
'coords' may be any unique identifier for the intersection. For example:
my $coords = 'qd'; # SGF formatmy $coords = 'a4'; # NNGS / IGS style coordinatesmy $coords = "$x,$y"; # real coordinatesmy $coords = 'George'; # as long as there's only one George
New
- my $diagram = Games::Go::Sgf2Dg::Diagram->
new(?options?)
A new Games::Go::Sgf2Dg::Diagram can take the following options:
hoshi=> ['coords', ...]-
A reference to a list of coordinates where the Diagram should place hoshi points.
black=> ['coords', ...]-
A reference to a list of coordinates where the Diagram should start with black stones already in place.
white=> ['coords', ...]-
A reference to a list of coordinates where the Diagram should start with white stones already in place.
coord_style=> 'normal' | 'sgf' | numeric-
Defines the coordinate translation system. Note that while
Games::Go::Sgf2Dg::Diagramdoesn't use this coordinate system directly, sgf2dg converters may call the coordinate translator methodsxcoordandycoord, which rely oncoord_styleandboardSizeX/Y(below).Legal values are:
- 'normal'
-
This is the standard coordinate system used for drawing diagrams: the vertical coordinates start with 1 at the bottom and increase towards the top edge. The horizontal coordinates are letters starting with A on the left, and increasing towards the right, but skipping over 'I'. This is the default coordinate style.
- 'sgf'
-
Coordinates within SGF files are single letters, lower case first, then upper case. The origin (aa) is the top left corner. 'A' follows 'z', so the point at (26, 27) translates to (zA).
- numeric: '++' | '+-' | '-+' | '--'
-
Number coordinates can be either increasing or decreasing. '++' starts with (0, 0) in the upper left corner, '-+' has (0, 0) in the upper right corner, etc.
boardSizeX=> number
boardSizeY=> number-
boardSizeX/Yare used by the coordinate translation methods (xcoordandycoordto calculate the appropriate coordinate string.
callback=> \&user_defined_callback-
A reference to a user defined callback function. The user callback is called (with a reference to the
Diagramas the only argument) whennodeis called after conflict is detected.The user callback should either save the current
Diagramand call <next>, or flush theDiagram(by printing for example) and call <clear>.If the user callback is defined, a call to
nodealways returns non-zero (the current node number).
enable_overstones=> true | false-
If true (default), overstones are enabled and may be created by the
Diagramduring a call to theputmethod. The user must be prepared to deal with overstones information in response to a call to thegetmethod.
Methods
- $diagram->
clear -
Clears the
Diagram. Allmarks,labels, andnumbers are removed from the stones and intersections. Allcaptured stones are removed, and all overstones are deleted (at which point, theDiagramis in the same state as anewDiagram). Pending actions that were not applied due to conflicts are now applied to theclearedDiagram.The following options are preserved:
- •
-
node (gets incremented)
- •
-
callback
- •
-
enable_overstones
- $diagram->force_conflict(? $msg ?)
-
Set the conflict flag to force pending actions to be flushed and a new
$diagramcreated. It's a good idea to pass a short$msgexplaining the conflict is being created.$msgis printed in -verbose mode of sgf2dg. If no$msgis defined, a generic (and probably not very helpful) message is produced.
- my $pending_count = $diagram->
actions_pending -
Returns the number of actions that would be executed if
nodewere called.
- my $done_count = $diagram->
actions_done -
Returns the number of actions that have been done for the current
$diagram.
- my $nextDiagram = $diagram->
next -
Creates a new
Diagramobject starting from the currentDiagram.$nextDiagramis the starting point for the nextDiagramin a series, or for a variation.As with the
clearmethod, allcaptured stones are removed, and all overstones are deleted. Pending actions that were not applied due to conflicts are now applied to thenextDiagram.The following options are preserved:
- •
-
node (gets incremented)
- •
-
callback
- •
-
enable_overstones
- •
- $diagram->
hoshi(@hoshi_coords) -
Adds the coords listed in
@hoshi_coordsto any existing hoshi points. In array context, returns the list of coords that are hoshi points. In scalar context, returns a reference to the list.
- $diagram->
node -
All actions (
put,mark,labelandproperty) are provisional untilnodeis called. This makes a collection of actions atomic. ADiagramnode is analogous to a Smart Go Format (SGF) node. If there are no conflicts with the collected provisional actions,nodeincorporates them into theDiagramand returns non-zero (the current node number).If there is a conflict and a user
callbackis defined,nodecalls the callback with a reference to theDiagram($diagram) as the only argument. The user callback should either flush theDiagramand callclear(to reuse theDiagram) or save the currentDiagram, and callnext(to generate a newDiagram).If there is a conflict and no user
callbackis defined,nodereturns 0. The user should either:- •
-
flush the current
Diagramand call$diagram->clearto continue working with the currentDiagram, or:
- •
-
save the current
Diagram(and call$diagram->nextto create a newDiagramto continue working with)
Calling either
nextorclearcauses the pending collection of conflicting actions to be incorporated into the resultingDiagram.
- $diagram->
put('coords', 'black' | 'white', ? number ? ) -
puta black or white stone on theDiagramatcoords. The stone color is must be 'black' or 'white' (case insensitive, 'b' and 'w' also work). Optionalnumberis the number on the stone. If not defined, the stone is un-numbered (which is probably a mistake except at the very start of aDiagram.putting can cause any of the following conflicts:- •
-
stone is numbered and number is already used
- •
-
stone is numbered and the intersection is already labeled
In certain situations, (notably ko and snapbacks but also some other capturing situations),
putstones may become overstones. overstones are stones played on an intersection that contains a stone that has beencaptured, but not yet removed from theDiagram. There are two kinds of overstones: normal andmarked, depending on the state of the underlying (captured but not yet removed) stone.If the underlying stone is
numbered, B>mark>ed orlabeled, the overstone is normal and there will be no conflicts (unless the number is already used!).If the underlying stone is un-
numbered and un-labeled, theDiagramattempts to convert it into amarked stone. If the conversion succeeds, the overstone becomes a marked overstone, and there is no conflict.The conversion of the underlying stone causes a conflict if:
- •
-
a stone of the same color as the underlying stone has already been converted elsewhere in the
Diagram, or
- •
-
a mark of the same color as the underlying stone exists elsewhere in the
Diagram.
See the
getmethod for details of how overstone information is returned.
- $diagram->
renumber($coords, $color, $old_num, $new_num); -
Changes the number of a stone already on the board.
$color, and$old_nummust match the existing color and number for the stone at$coords($old_num or$new_nummay be undef for an un-numbered stone). Only the displayed stone is compared for the match, overstones (game_stones) are not considered.Fails and returns 0 if:
- •
-
there is no diagram stone on the intersection, or
- •
-
$coloror$old_numdon't match, or
- •
-
$new_numis already used, or
- •
-
a
propertyitem exists for$old_numand$new_numis undef
If none of the above,
renumbersets the new number and returns 1.
- my $offset = $diagram->
offset($new_offset); -
Set a new offset for the diagram if
$new_offsetis defined. Returns the current value of the offset, or 0 if no offset has been set.Note that
Diagramdoesn't use the offset for anything, but external programs (like a converter) can use it to adjust the numbering.
- $diagram->
label('coords', 'text'); -
Place a label on an intersection.
textmay be any text, but notice that long strings may overflow a stone or intersection. If 'text' is empty ('') any existing label is removed.The same label can be applied to several intersections only if they are all labeled within a single
node.If the intersection or stone is already labeled, or occupied by a marked, or numbered stone, or if the label has already been used outside the labeling group,
labelcauses a conflict.
- $diagram->
mark('coords', ? 'mark_type' ?); -
Place a mark on a stone or intersection. The 'mark_type' can be any text, but is usually the SGF property:
- CR circle
- MA an X mark
- SQ square
- TR triangle
If 'mark_type' is not supplied (or undef), MA is assumed.
The
markraises a conflict if:- •
-
the intersection is already
labelled or numbered, or
- •
-
the same color and 'mark_type' already exists in the
Diagramfor a previous node (possibly from creating an understone).
- my $diagram->
territory($propID, $coords); -
$propIDshould be one of 'TB', 'TW', or undef.territorymarks the intersection$coordsas being white or black territory (see 'TB', 'TW' in thegetmethod below). If$numberis undef, any previous territory marking is removed.
- my $diagram->
view($coords); -
If
$coordsis defined, then the game-level VW property is set, and the intersection at$coordsis marked as viewable (hash key is 'VW'). If$coordsis '' or undef, then the game-level VW property is deleted, and the VW mark is removed from all intersections.
- my $nameListRef = $diagram->
name(? name, ... ?) -
Adds
name(s) to the currentDiagram. Names accumulate by getting pushed onto a list.In array context,
namereturns the current name list. In scalar context,namereturns a reference to the list of names.
- $diagram->
property($number, $propName, $propValue, ? $propValue... ?);
- my $prop_ref = $diagram->
property($number);
- my $all_props_ref = $diagram->
property(); -
If
$propNameand$propValare defined, pushes them onto the collection of properties associated with move$number.Note that
renumbering a move alsorenumbers the properties.If
$numberand$propNameare defined and$propValueis not ( or is empty), the$propNameproperty is removed.If
$numberis defined and$propName/$propValue are not,propertyreturns a reference to the (possibly empty) hash of property IDs and property Values associated with the move number:my $prop_ref = $diagram->property($number);my $prop_value = $prop_ref->{$propID}->[$idx];If
$numberis not defined, returns a reference to the (possibly empty) hash of properties stored in theDiagram. Hash keys are the move number, and each hash value is in turn a hash. The keys of the property hashes are (short) property IDs and the hash values are lists of property values for each property ID:my $all_prop_ref = $diagram->property();my $prop_value = $all_props_ref->{$moveNumber}->{$propID}->[$idx]property(when$propNameand$propValueare defined) is an action (it is provisional untilnodeis called) because properties are associated with a node in the SGF. However,propertynever causes a conflict.Note that sgf2dg stores the following properties:
propID number propVal comment------ ------ -------Move propertiesW[] or W[tt] move 'pass' white passB[] or B[tt] move 'pass' black passKO move '' force movePL[W|B] move 'W' | 'B' set playerNode annotation propertiesC[text] move text move commentDM[dbl] move 0 | 1 Even positionGB[dbl] move 0 | 1 Good for blackGW[dbl] move 0 | 1 Good for whiteHO[dbl] move 0 | 1 HotspotUC[dbl] move 0 | 1 UnclearN[stxt] move simple_text Name (node name)V[real] move real Value (estimated game score)Move annotation propertiesBM[dbl] move 0 | 1 Bad moveDO move '' Doubtful moveIT move '' Interesting moveTE[dbl] move 0 | 1 Tesuji (good move)Markup propertiesAR[c_pt] move 'pt:pt' ArrowDD[elst] move 'pt?' Dim points: DD[] clearsLN[c_pt] move 'pt:pt' LineSL[lst] move 'pt' Select points (markup unknown)Root propertiesAP[stxt:stxt] 0 'stxt:stxt' Application_name:versionCA[stxt] 0 'charset' character setFF[1-4] 0 0 - 4 FileFormatGM[1-16] 0 0 - 16 GameST[0-3] 0 0 - 3 How to show variations (style?)Game info propertiesAN[stxt] 0 simple_text Annotater (name)BT[stxt] 0 simple_text Black teamWT[stxt] 0 simple_text White teamCP[stxt] 0 simple_text CopyrightON[stxt] 0 simple_text Opening informationOT[stxt] 0 simple_text Overtime description (byo-yomi)PC[stxt] 0 simple_text Place game was playedRE[stxt] 0 simple_text ResultRO[stxt] 0 simple_text RoundRU[stxt] 0 simple_text RulesSO[stxt] 0 simple_text SourceUS[stxt] 0 simple_text User/program who entered the gameGC[text] 0 text Game commentTM[real] 0 real_number Time limitsTiming propertiesBL[real] move real_number BlackLeft (time)WL[real] move real_number WhiteLeft (time)OB[num] move number Black moves left (after this move)OW[num] move number White moves leftGo-specific propertiesHA[num] 0 number HandicapKM[real] 0 real_number KomiMisc. propertiesPM[num] move number Print mode - see FF4 specBS[stext] move stext BlackSpecies (deprecated)WS[stext] move stext WhiteSpecies (deprecated)FG[pt:stext] move bitmask:stext Figure: see FF4 spec
- @title_lines = $diagram->
gameProps_to_title(\&emph_sub) -
gameProps_to_titleconverts game (node 0) properties extracted from the SGF file. The properties are scanned in the order listed here:- GN GameName
- EV EVent
- RO ROund (joined to EVent)
- DT DaTe
- PC PlaCe
- PW PlayerWhite "White:"
- WR WhiteRank (joined to PW)
- PB PlayerBlack "Black"
- BR BlackRank (joined to PB)
- KM KoMi "Komi:"
- RU RUles "Rules:"
- TM TiMe "Time:"
- OT OverTime (byo-yomi) "Byo-yomi:"
- RE REsult "Result:"
- AN ANnotator "Annotated by:"
- SO Source "Source:"
- US USer "Entered by:"
- CP CoPyright
- GC GameComment
For each property that is found, a line is added to the
@title_linesreturn array. If the property has a string in double-quotes in the list above, that string (plus one space) is prefixed to the property text. In addition, if \&emph_sub is defined, the prefix is passed to &$emph_sub to make those portions appear emphasized in the title lines. Example:my @title = $diagram->gameProps_to_title(sub { "{\\bf $_[0]}" });wraps portions of the title line in TeX's bold-face (bf) style.
- $diagram->
capture('coords') -
Captures the stone at the intersection.
Note that
capturehas no visible affect on the diagram. Rather, it marks the stone so that it is removed when creating thenextDiagram.captureis not considered an action because it cannot cause a conflict or change the visible status of the board.
- $diagram->
remove('coords') -
Removes the stone at the intersection.
Unlike
capture,removechanges the visible status of theDiagram: the stone is deleted, along with all marks and letters (only the 'hoshi', if any, is retained).removeis typically used at the start of a variation to remove any stones that are improperly placed for the variation. It is closely related to the AddEmpty (AE) SGF property.
- my $stone = $diagram->
game_stone(coords | $intersection); -
Returns 'black' or 'white' if there is a stone currently on the coords or intersection (a reference to an intersection, such as is returned by
$diagram->get) , otherwise returns undef.Note that the return value is determined by the game perspective, not the diagram perspective. If a stone is
putand latercaptured,game_stonereturns undef even though the diagram should still show the original stone. If a white stone isputand latercaptured, and then a black stone isput,game_stonereturns 'black', andgetindicates that a white stone should be displayed on the diagram.Note also that since
putis provisional untilnodeis called. If you usegame_stoneto check for liberties and captures, it must be done after the call tonodethat realizes theput.
- $diagram->
get('coords') -
Return the current status of the intersection. Status is returned as a reference to a hash. The keys of the hash indicate the items of interest, and the values of the hash are the indices where the item was applied, except where noted below.
Only keys that have been applied are returned - an empty hash means an empty intersection.
The hash keys can be any of:
- 'hoshi'
-
This intersection is a hoshi point. Note that since hoshi points are determined at
newtime, the value of this hash entry is always 0. This key is returned even if a stone has been placed on the intersection.
- 'white'
-
The color of a stone at this intersection.
- 'black'
-
The color of a stone at this intersection.
- 'number'
-
The hash value is the number on the stone. The node for
numberis found in the 'black' or 'white' hash value.
- 'capture'
-
The stone on this intersection has been
captured, the intersection is currently empty from the perspective of the game.
- 'mark'
-
The intersection or stone is marked. The value indicates the type of mark, usually the SGF property:
- CR circle
- MA an X mark
- SQ square
- TR triangle
- 'label'
-
The intersection has been labeled. The value indicates the text of the label.
- 'overstones'
-
If this hash entry exists it means that one or more stones were overlayed on the stone that is currently displayed on this intersection of the
Diagram.The hash value is a reference to an array of color/number pairs. The colors and numbers were passed to the
putmethod which decided to convert the stone into an overstone.This is typically seen as notes to the side of the diagram saying something like "black 33 was played at the marked white stone". In this example. the information returned by
getdescribes 'the marked white stone', while 'black' will be the first item in the 'overstones' array, and '33' will be the second:$diagram->get($coords) == {white => node_number,overstones => ['black', 33],...}
- 'TB' or 'TW'
-
Intersection has been marked as black or white territory with a TB or TW property.
- 'view'
-
Set when the intersection is marked with a VW view property. Relates to the VW game
property:if ((not $diagram->property(0)->VW) or # no game-level VieW property$intersection->{view}) { # this intersection is viewable# display this intersection}
The hash reference returned by
getpoints to the data in theDiagramobject - don't change it unless you know what you are doing.
- my $coord_string = $diagram->
xcoord($x)
- my $coord_string = $diagram->
ycoord($y) -
Returns a string to display for a given
$xor$ycoordinate. The string returned depends not only on the$xor$yvalue, but also on thecoords_styleandboardSizeX/Yconfiguration options,
- my $first_number = $diagram->
first_number -
Returns the lowest number
puton theDiagram, or 0 if no numbered stones have beenput.
- my $last_number = $diagram->
last_number -
Returns the highest number
puton theDiagram, or 0 if no numbered stones have beenput.
- my $parentDiagram = $diagram->
parent(? $parent ?) -
If
$parentis defined, sets theparentfor this diagram.Always returns the current value of
parent(possibly undef).
- my $move_number = $diagram->
var_on_move(? $new_number ?) -
If
$new_numberis defined, sets thevar_on_movefor this diagram. This is intended to be used in conjunction with the <Bparent> information to title diagrams such asmy $title = "Variation 2 on move " .$diagram->var_on_move ." in " .$diagram->parent->name;Always returns the current value of
var_on_move(possibly undef).
- my $overListRef = $diagram->
getoverlist -
Returns a reference to the list of intersections with overstones. The list members are the same intersection hash references returned by the
getmethod.The list is sorted by the order the intersections first had an overstone
puton. If there are no intersections with overstones, returns a reference to an empty list.
- my $user = $diagram->
user( ? $new_user ? ) -
If
$new_useris defined, sets theuservalue for theDiagram. Note that theuseris not used withinDiagram, but can be used by external code for any purpose. Most useful is probably if$new_useris a reference to a hash of user-defined items of interest.Returns the current
uservalue (default is undef).
Releases
Unchanged in 3 releases since Debian 11 (Bullseye).