diff --git a/GoSharpCore/Game.cs b/GoSharpCore/Game.cs index 3886fd1..f965e54 100644 --- a/GoSharpCore/Game.cs +++ b/GoSharpCore/Game.cs @@ -30,7 +30,8 @@ public class Game { private static readonly Dictionary> PropertyHandlers = new Dictionary>(); - private static readonly HashSet PropertiesToExclude = new HashSet { "W", "B", "AE", "AB", "AW" }; + private static readonly HashSet PropertiesToExclude = new HashSet { "W", "B", "AE", "AB", "AW", "C", + "LB", "TR", "MA", "CR", "SQ" }; static Game() { foreach (var kvp in SGFPropToColor) { @@ -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, "□")); } /// @@ -60,6 +67,11 @@ static Game() { private readonly List _moves = new List(); + public void PopMoves() + { + _moves.Clear(); + } + private readonly Dictionary _captures = new Dictionary() { { Content.Black, 0 }, @@ -159,6 +171,9 @@ public Dictionary SetupMoves { [PublicAPI] public int BlackCaptures => _captures[Content.Black]; + public string Comment => _comment; + private string _comment; + /// /// Constructs a root game object based on a GameInfo object. /// @@ -569,6 +584,15 @@ public static List SerializeFromSGF(TextReader sr) { coll.Read(sr); return coll.GameTrees.Select(c => new Game(c)).ToList(); } + + public static List 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) { @@ -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; + } } } diff --git a/GoSharpCore/SGFProperty.cs b/GoSharpCore/SGFProperty.cs index 1d5ffed..360a000 100644 --- a/GoSharpCore/SGFProperty.cs +++ b/GoSharpCore/SGFProperty.cs @@ -31,7 +31,8 @@ public class SGFProperty { private readonly HashSet _moveProperties = new HashSet { - "W","B","AB","AW","AE" + "W","B","AB","AW","AE", "C", + "LB", "TR", "MA", "CR", "SQ" }; /// /// Returns true if this property is a file format property.