Thursday, April 9, 2015

Serializing to SGF

At the moment my bots produce stacks of boards (aimed at easily displaying the progress of a game). Also I speculate that 19^2 is small enough that passing them around will not matter a lot.

But the go world runs on Smart Game Format. I really don't write files of arbitrary strings in MATLAB a lot.


function [] = WriteSgfOfBoardStack( boardStack, fileName)
%WriteSgfOfBoardStack Writes an array of boards to an SGF.
%   V. http://www.red-bean.com/sgf/sgf4.html

LETTERS = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's','t'};

sgfText = '(;FF[4](;';

for i = 2 : length(boardStack);
    changesBoard = boardStack(:,:,i)-boardStack(:,:,i-1);
    changesBoard(changesBoard<0)=0;
   
    if mod(i-1,2) == 0
        C = 'W';
    else
        C = 'B';
    end
   
    [row, col] = find(changesBoard);
   
    moveString = strcat(';', C, '[',LETTERS{col},LETTERS{row},']');
   
    sgfText = strcat(sgfText, moveString);
end

sgfText = strcat(sgfText, '))');

f = fopen(fileName, 'w+');

fprintf(f,sgfText);

fclose(f);

end

No comments:

Post a Comment