Giter VIP home page Giter VIP logo

sgf4j's Introduction

SGF4J

I've been doing some serious yak shaving. Instead of solving Go problems to improve my game I've been writing a SGF viewer to make it easier to go through my problem files.

As a side result of that viewer I had to do some SGF parsing. Hence this project. You can use this library to parse SGF files and then actually play it out on a virtual board and then write out the SGF if you want to. I test the library with bunch of databases to make sure it is able to parse enough different SGF files and also when writing those out no data is lost.

I use this parser on a weekly basis when I solve Go problems and haven't found any parsing or serialising issues for some time. I also test the library with 90 000+ SGF files that I parse with this library to make sure the lib is mature enough to parse most of the games out there. I also write the games out again to SGF format to re-parse them and make sure that by writing them out again we are not losing any data. If you find SGF files that don't parse with this library then please let me know.

Include in your project

The project is synced to Maven Central and to use it just include it in your pom.xml.

<dependencies>
  <dependency>
    <groupId>com.toomasr</groupId>
    <artifactId>sgf4j-parser</artifactId>
    <version>0.0.3</version>
  </dependency>
<dependencies>

Parsing from file

Simple parsing looks like this. You either have a SGF file in a String or a Path of the location of the file. You create a Game object of it and then start walking the nodes from the root node. There are methods to start from the end too.

    Game game = Sgf.createFromPath(Paths.get("location-of-your.sgf"));

    GameNode node = game.getRootNode();
    do {
      System.out.println(node);
    }
    while ((node = node.getNextNode()) != null);

Remember that not every node is an actual move. Also note that a node might have multiple children. I have built it in a way that node.getNextMove() returns the next move in the main line of play while getChildren() will return the rest of the possible next moves.

Writing to file

Once you have parsed a file into a Game object you can also write it back to a SGF file.

Game game = Sgf.createFromPath(sgfPath);
//... add/remove/update nodes
game.saveToFile(Paths.get("/tmp/output.sgf"))

This project writes the SGF string of the game to the output.sgf file. This code path uses the Game.getGeneratedSgf() method that returns the SGF string of the current Game object.

sgf4j's People

Contributors

dependabot[bot] avatar toomasr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sgf4j's Issues

Saving SGF fails with VW node

After loading and saving this file:

20200614-023719.zip

VW node is created like a root node. This causes the game tree to have a new node in the beginning.

According to SGF documentation, VW node is a "Miscellaneous Property" and it´s valid in any node.

The problem here is that when saving the file, this property is added with a ";" even when it´s in the header.
This causes the issue. Node ends up being added here:

rtrnNode.addProperty(key, cleanValue(value));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.