Skip to content

extend SGF property handling to include comments and labels #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion GoSharpCore/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class Game {
private static readonly Dictionary<string, Func<Game, SGFProperty, Game>> PropertyHandlers =
new Dictionary<string, Func<Game, SGFProperty, Game>>();

private static readonly HashSet<string> PropertiesToExclude = new HashSet<string> { "W", "B", "AE", "AB", "AW" };
private static readonly HashSet<string> PropertiesToExclude = new HashSet<string> { "W", "B", "AE", "AB", "AW", "C",
"LB", "TR", "MA", "CR", "SQ" };

static Game() {
foreach (var kvp in SGFPropToColor) {
Expand All @@ -51,6 +52,12 @@ static Game() {
PropertyHandlers["WR"] = ((x, y) => x.HandleWR(y));
PropertyHandlers["BR"] = ((x, y) => x.HandleBR(y));
PropertyHandlers["TM"] = ((x, y) => x.HandleTM(y));
PropertyHandlers["C"] = ((x, y) => x.HandleC(y));
PropertyHandlers["LB"] = ((x, y) => x.HandleLB(y));
PropertyHandlers["TR"] = ((x, y) => x.HandleMarks(y, "△"));
PropertyHandlers["MA"] = ((x, y) => x.HandleMarks(y, "✕"));
PropertyHandlers["CR"] = ((x, y) => x.HandleMarks(y, "◯"));
PropertyHandlers["SQ"] = ((x, y) => x.HandleMarks(y, "□"));
}

/// <summary>
Expand All @@ -60,6 +67,11 @@ static Game() {

private readonly List<Variation> _moves = new List<Variation>();

public void PopMoves()
{
_moves.Clear();
}

private readonly Dictionary<Content, int> _captures = new Dictionary<Content, int>()
{
{ Content.Black, 0 },
Expand Down Expand Up @@ -159,6 +171,9 @@ public Dictionary<Content, Group> SetupMoves {
[PublicAPI]
public int BlackCaptures => _captures[Content.Black];

public string Comment => _comment;
private string _comment;

/// <summary>
/// Constructs a root game object based on a GameInfo object.
/// </summary>
Expand Down Expand Up @@ -569,6 +584,15 @@ public static List<Game> SerializeFromSGF(TextReader sr) {
coll.Read(sr);
return coll.GameTrees.Select(c => new Game(c)).ToList();
}

public static List<Game> SerializeFromSGFText(string text) {
using(TextReader sr = new StringReader(text))
{
var coll = new SGFCollection();
coll.Read(sr);
return coll.GameTrees.Select(c => new Game(c)).ToList();
}
}

private static void CreateGameTree(SGFGameTree root, Game p) {
if (p.GameInfo != null) {
Expand Down Expand Up @@ -690,5 +714,33 @@ private Game HandleTM(SGFProperty p) {
GameInfo.MainTime = TimeSpan.FromSeconds(p.Values[0].Num);
return this;
}

private Game HandleC(SGFProperty p)
{
_comment = p.Values[0].Value;
return this;
}

public List<(Point, string)> Labels => _labels;
private List<(Point, string)> _labels = new List<(Point, string)>();

private Game HandleLB(SGFProperty p)
{
foreach (var v in p.Values) {
// Value is the content of [].
// MoveA is to the left of ":", MoveB is to the right of ":"
int colonPos = v.Value.IndexOf(":");
_labels.Add((v.MoveA, v.Value.Substring(colonPos + 1))); // Get the characters after ":"
}
return this;
}

private Game HandleMarks(SGFProperty p, string mark)
{
foreach (var v in p.Values) {
_labels.Add( (v.Move, mark) );
}
return this;
}
}
}
3 changes: 2 additions & 1 deletion GoSharpCore/SGFProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class SGFProperty {

private readonly HashSet<string> _moveProperties = new HashSet<string>
{
"W","B","AB","AW","AE"
"W","B","AB","AW","AE", "C",
"LB", "TR", "MA", "CR", "SQ"
};
/// <summary>
/// Returns true if this property is a file format property.
Expand Down