Menu

GameTreeToSgfs

Patrick Näf

Here is a c++ program to parse tree-structured sgf files generated by fuego to a set of sgf files of only one game.

a sample to save fuego search tree to a tree-structured sgf:

uct_param_player max_games 99 (small enough to not run out of memory)
uct_param_search keep_games 1

(keep simulations stored in memory)
uct_savegames /Users/mmueller/Desktop/games.sgf

a c++ program to parse above tree-structured sgf file. 2 parameters needed: tree-sgf and folder-to-save-sgf. by the way, there will be, except .sgf files, some .txt files recording intree-step and total-step num of accordingly sgf files. please ignore them, if not useful for you, thanks.

code:

/*
 * FuegoSGFParser.cpp
 *
 *  Created on: Sep 19, 2010
 *      Author: xiefan
 */

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char** argv){

    if(argc != 3){
        fprintf(stderr, "usage: ./xxx [tree.sgf] [dir to save sgf]");
    }

    int sgf_id = 0;//game id of every sgf
    int nuLBracket = 0;//num of total (
    int nuRBracket = 0;//num of total )
    std::string title;//begining info for all sgf

    //the following valuables will be reset after very simulation
    int nuLParenthese = 0;// num of total [
    int nuRParenthese = 0;// num of total ]
    int nuIntreeStep = 0;//num of move in tree
    int nuTotalStep = 0;//num of total moves
    bool inTree = false;//whether still in tree, helping parse intree step
    std::string game_str = "";

    /**
     * valuables used through this whole file
     */
    std::ifstream sgfs;
    sgfs.open(argv[1], ifstream::binary);//must using binary mode, because '\n' is needed in parsing
    int index = 0; //index of this tree sgf file
    char c=' '; //value used in get char

    /**
     * firstly, parse title info to title
     */
    while (true){
        sgfs.get(c);
        if(c == '('){
            if (nuLBracket == 0)
                nuLBracket ++;
            else
                break;
        }
        title += c;
    }

    sgfs.unget();//put last '(' back

    /**
     * begin to pars sgfs...
     */
    while(true){
        sgfs.get(c);
        if(c =='('){
            nuLBracket ++;
            game_str += c;
        }else if(c == ')'){
            nuRBracket ++;
            if(nuLBracket > nuRBracket){
                sgf_id ++;
                game_str += c;

                /**
                 * save sgfs
                 */
                char sgf_name[20];
                char step_name[20];
                sprintf(sgf_name, "%s/%d_sgf.sgf", argv[2], sgf_id);
                sprintf(step_name, "%s/%d_step.txt", argv[2], sgf_id);
                ofstream o_sgf;
                ofstream o_step;

                o_sgf.open(sgf_name);
                o_step.open(step_name);

                o_sgf << title << game_str << ")" << std::endl;
                //one more ; counted
                o_step << nuIntreeStep-1 << " " << nuTotalStep-1 << std::endl;

                o_sgf.close();
                o_step.close();

                //clear info of one simulation
                nuLParenthese = 0;// num of total [
                nuRParenthese = 0;// num of total ]
                nuIntreeStep = 0;//num of move in tree
                nuTotalStep = 0;//num of total moves
                inTree = false;//whether still in tree, helping parse intree step
                game_str.clear();

            }else
                break;//whole file finished
        }else if (c == '['){
            nuLParenthese ++;
            game_str += c;
        }else if (c == ']'){
            nuRParenthese ++;
            game_str += c;
        }else if (c == ';'){
            if(nuRParenthese == 1 && nuLParenthese == 1){
                //intree moves
                nuIntreeStep ++;
                nuTotalStep ++;
                inTree = true;
            }else if (inTree){
                nuIntreeStep ++;
                nuTotalStep ++;
            }else {
                nuTotalStep ++;
            }
            game_str += c;
        }else if(c == '\n'){
            if(inTree == true)
                inTree = false;
            game_str += c;
        }else{
            game_str += c;
        }
    }

    return 0;
}

Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.