public static void CompareProp(SGFTree excepted, SGFTree actual, string indent) { var actualprop = actual.Properties.GroupBy(x => x.Name).ToList(); var exceptedprop = excepted.Properties.GroupBy(x => x.Name); foreach (var grp in exceptedprop) { var actualgroup = actualprop.FirstOrDefault(x => x.Key == grp.Key); if (actualgroup == null) { string values = SerializeValues(grp); Trace.WriteLine($"{indent}missing property '{grp.Key}' = {values}"); } else { CompareValues(indent, actualprop, actualgroup, grp); } } foreach (var grp in actualprop) { var values = SerializeValues(grp); Trace.WriteLine($"{indent}new property '{grp.Key}' = {values}"); } }
public static SGFTree LoadFromStream(Stream baseStream) { var stream = new StreamReader( baseStream, DefaultEncodingConfiguration); SGFTree tree = null; try { var loader = new SgfReader(stream); if (loader.ReadUntil('(') == '(') { tree = new SGFTree(); if (!loader.ReadTree(tree)) { tree = null; } } } finally { stream.Close(); } if (tree != null) { return(tree); } throw new Exception("Couldn't load sgf from stream"); }
void StartNewGame(int size) { this.size = size; Tree = new SGFTree(new TreeNode(1)); SetInfomation(); SetupGame(Tree.Root); //GenerateBoard(size); }
public void WriteSgfTree(SGFTree tree) { if (tree.HasContent) { _writer.Write("("); WriteSgfSequence(tree); _writer.Write(")"); NewLine(); } }
private bool ReadTree(SGFTree currentNode) { var nextChar = '\0'; var propertyName = string.Empty; do { nextChar = ConsumeWhiteSpaces(); if (nextChar == ';') { currentNode = currentNode.NewNode(); } else if (nextChar == '(') { ReadTree(currentNode); } else if (nextChar == ')') { return(true); } else if (nextChar == '[') { var attribute = ReadAttribute(); if (propertyName != string.Empty) { currentNode.AddAttribute( new SGFProperty(propertyName, attribute)); } } else if (char.IsLetter(nextChar)) { propertyName = ""; do { propertyName = string.Concat(propertyName, nextChar); nextChar = ConsumeWhiteSpaces(); } while (nextChar != '[' && !stream.EndOfStream); if (stream.EndOfStream) { throw new Exception("Unexpected end of stream"); } currentNode.AddAttribute( new SGFProperty(propertyName, ReadAttribute())); } else if (stream.EndOfStream) { throw new Exception("Unexpected end of stream"); } } while (nextChar != ')'); return(true); }
/// <summary> /// Save to debugging with tiers softwares /// /!\ Test Chain SgfReader+SgfCompiler+GoSgfBuilder+SgfWriter! /// </summary> /// <param name="name"></param> /// <returns></returns> private static void SaveActual(string name, SGFTree actual) { var file = Path.Combine(AssemblyLocation, "usecases", string.Concat(name, "ex.sgf")); Trace.WriteLine($"saveAs result to {file}"); using (var stream = File.Open(file, FileMode.Truncate, FileAccess.ReadWrite)) using (var sw = new StreamWriter(stream, Encoding.UTF8)) { var writer = new SgfWriter(sw, true); writer.WriteSgfTree(actual); } }
private static bool IsMoveNode(SGFTree tree) { foreach (var property in tree.Properties) { var name = property.Name.ToUpper(); if (name == "B" || name == "W") { return(true); } } return(false); }
void SetInfomation() { if (Tree == null) { return; } Tree.Root.SetAction("AP", "WeGo", true); Tree.Root.SetAction("SZ", "" + this.size, true); Tree.Root.SetAction("GM", "1", true); Tree.Root.SetAction("FF", "4", true); Debug.Log(SGFTree.GetSgfString(Tree.Root)); }
private static Stone FindStone(SGFTree tree) { foreach (var property in tree.Properties) { var name = property.Name.ToUpper(); if (name == "B" || name == "W") { var result = CompilePointValue(property.Value.ToLower()); result.IsBlack = name == "B"; return(result); } } return(new Stone()); }
private void WriteSgfSequence(SGFTree tree) { WriteProperties(tree.Properties); while (tree.ChildNodes.Count == 1) { NewLine(); tree = tree.ChildNodes[0]; WriteProperties(tree.Properties); } foreach (var childNode in tree.ChildNodes) { WriteSgfTree(childNode); } }
private static IEnumerable <SGFTree> LoadAndParse(string path, List <int> index) { if (File.Exists(path)) { var stream = new StreamReader( File.OpenRead(path), DefaultEncodingConfiguration); try { var loader = new SgfReader(stream); if (index == null) { while (loader.ReadUntil('(') == '(') { var tree = new SGFTree(); if (loader.ReadTree(tree)) { yield return(tree); } } } else { while (index.Count > 0) { stream.BaseStream.Seek(index[0], SeekOrigin.Begin); stream.DiscardBufferedData(); index.RemoveAt(0); loader.ReadUntil('('); var tree = new SGFTree(); if (loader.ReadTree(tree)) { yield return(tree); } } } } finally { stream.Close(); } } }
void Load(string filePath) { try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader(filePath, System.Text.Encoding.GetEncoding("gb2312"), true)) { List <SGFTree> trees = SGFTree.Load(sr); Tree = trees[0]; SetupGame(Tree.Root); } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } }
private static void CompileProperties(GoNode node, SGFTree tree) { foreach (var property in tree.Properties) { var name = property.Name.ToUpper(); switch (name) { case "C": node.Comment += AntiUnixRegex.Replace(property.Value, "\r\n"); break; case "LB": node.EnsureMarkup(); node.Markup.Labels.Add(CompileLabelValue(property.Value)); break; case "SQ": case "CR": case "TR": case "MA": case "SL": node.EnsureMarkup(); var mark = name == "SQ" ? MarkType.Square : name == "CR" ? MarkType.Circle : name == "TR" ? MarkType.Triangle : name == "MA" ? MarkType.Mark : MarkType.Selected; foreach (var stone in CompilePointValues(property.Value)) { node.Markup.Marks.Add(new Mark(stone.X, stone.Y, mark)); } break; } } }
public static void CompareNode(SGFTree excepted, SGFTree actual, string indent) { Trace.WriteLine($"{indent}X"); CompareProp(excepted, actual, indent); while (excepted.ChildNodes.Count == actual.ChildNodes.Count && excepted.ChildNodes.Count == 1) { excepted = excepted.ChildNodes[0]; actual = actual.ChildNodes[0]; CompareProp(excepted, actual, indent); } for (var i = 1; i < excepted.ChildNodes.Count && i < actual.ChildNodes.Count; i++) { CompareNode(excepted.ChildNodes[i], actual.ChildNodes[i], string.Concat(indent, "\t")); } if (excepted.ChildNodes.Count != actual.ChildNodes.Count) { Trace.WriteLine($"{indent}Children skipped : excepted.child='{excepted.ChildNodes.Count}' actual.child='{actual.ChildNodes.Count}'"); } }
public static SGFTree LoadAndParseSingle(string path) { if (File.Exists(path)) { var stream = new StreamReader( File.OpenRead(path), DefaultEncodingConfiguration); SGFTree tree = null; try { var loader = new SgfReader(stream); if (loader.ReadUntil('(') == '(') { tree = new SGFTree(); if (!loader.ReadTree(tree)) { tree = null; } } } finally { stream.Close(); } if (tree != null) { return(tree); } throw new Exception("Couldn't load sgf from " + path); } throw new Exception("Couldn't open " + path); }
public static GoGame Compile(SGFTree gameTree, TransformType transform) { byte size = 19; if (gameTree.ChildNodes.Count == 0) { throw new FormatException("No game data"); } if (gameTree.ChildNodes.Count > 1) { throw new FormatException("Game lists are not supported"); } var rootNode = gameTree.ChildNodes[0]; foreach (var property in rootNode.Properties) { if (property.Name == "SZ") { size = Convert.ToByte(property.Value); if (!(size >= 2 && size <= 19)) { throw new FormatException("Board size should be between 2 and 19"); } } } SetTransformer(size, transform); var game = new GoGame(size); foreach (var property in rootNode.Properties) { if (property.Name == "PW") { game.Info.WhitePlayer = property.Value; } if (property.Name == "PB") { game.Info.BlackPlayer = property.Value; } if (property.Name == "KM") { try { game.Info.Komi = float.Parse(property.Value, CultureInfoDefaultConfiguration); } catch (Exception) { game.Info.Komi = 0.5f; } } if (property.Name == "HA") { try { game.Info.Handicap = Convert.ToByte(property.Value); } catch (Exception) { game.Info.Handicap = 0; } } } game.UpdateBoard = false; useRoot = true; CompileNode(game, rootNode); game.UpdateBoard = true; return(game); }
public static GoGame Compile(SGFTree gameTree) { return(Compile(gameTree, TransformType.None)); }
private static void CompileNode(GoGame game, SGFTree tree) { var singleBranch = true; while (singleBranch) { if (IsMoveNode(tree)) { var node = new GoMoveNode(game.CurrentNode, FindStone(tree), game.MoveNumber % BoardStorageStepConfiguration == 0); game.AddNode(node); } else { GoSetupNode node; if (game.CurrentNode is GoRootNode && useRoot) { node = game.CurrentNode as GoRootNode; } else { node = new GoSetupNode(game.CurrentNode); } foreach (var property in tree.Properties) { var name = property.Name.ToUpper(); switch (name) { case "AB": case "AW": foreach (var stone in CompilePointValues(property.Value)) { stone.IsBlack = name == "AB"; node.AddStone(stone); } break; case "AE": foreach (var stone in CompilePointValues(property.Value)) { node.RemoveStone(stone); } break; case "PL": node.SetPlayer(property.Value.StartsWith("B")); break; } } if (!(game.CurrentNode is GoRootNode && useRoot)) { game.AddNode(node); } else { useRoot = false; } } CompileProperties(game.CurrentNode, tree); if (tree.ChildNodes.Count == 1) { tree = tree.ChildNodes[0]; } else { singleBranch = false; } } var move = game.MoveNumber; foreach (var branch in tree.ChildNodes) { CompileNode(game, branch); game.ToMove(move); } }
public SgfBuilder() { _root = new SGFTree(); _cursor = _root; }
public SgfBuilder Root() { _cursor = _root; return(this); }
public SgfBuilder Next() { _cursor = _cursor.NewNode(); return(this); }