import gnubg def playMatchSeries( statsFile = None, # log file matchLength = 7, noOfMatches = 100, sgfBasePath = None, # optional matBasePath = None): # optional """Starts noOfMatches matchLength pointers. For every match the running score, gammoms (g) and backgammons (b) and the match winner is written to 'statsFile': g gammon b backgammon (g) gammon, but not relevsnt (end of match) (b) backgammon, but not relevant g(b) backgammon, but gammon would have been enough to win the match If the optional parameters 'sgfBasePath' and 'matBasePath' are set to a path, the matches are saved as sgf or mat file.""" for i in range(0, noOfMatches): if not statsFile: raise ValueError('Parameter "statsFile" is mandatory') gnubg.command('new match ' + str(matchLength)) matchInfo = formatMatchInfo(gnubg.match(analysis=0, boards=0)) f = open(statsFile, 'a') f.write(matchInfo) f.close if sgfBasePath: gnubg.command('save match ' + sgfBasePath +\ str(i) + '.sgf') if matBasePath: gnubg.command('export match mat ' + matBasePath +\ str(i) + '.mat') def formatMatchInfo(matchInfo): outString = '' tempS = '' score = [0,0] matchLength = matchInfo['match-info']['match-length'] for game in matchInfo['games']: pw = game['info']['points-won'] if game['info']['winner'] == 'O': oldScore = score[0] score[0] += pw winner = 0 else: oldScore = score[1] score[1] += pw winner =1 cube = getCube(game) print 'cube: ', cube if pw == cube: gammon = '' elif pw == 2 * cube: gammon = 'g' if (cube + oldScore) >= matchLength: # gammon not relevant gammon = '(g)' elif pw == 3 * cube: gammon = 'b' if (cube + oldScore) >= matchLength: # backgammon not relevant gammon = '(b)' elif (2 * cube + oldScore) >= matchLength: # backgammon not relevant, but gammon gammon = 'g(b)' outString += '%d:%d%s ' % (score[0], score[1], gammon) outString += str(winner) + '\n' return outString def getCube(game): """returns the cube value of the game""" doubled = 0 cube = 1 for turn in game['game']: if turn['action'] == 'double': doubled = 1 continue if doubled: if turn['action'] == 'take': cube *= 2 doubled = 0 else: # dropped break return cube