# To run this script # open a Command Prompt or PowerShell in "C:\Program Files (x86)\gnubg>" # and run ".\gnubg-cli.exe --python=D:\MuratMutantCubeCubeful.py" # or run .\gnubg-cli.exe then type ">"+[ENTER] to get in Python interpreter # and run "exec(open('D:\Python\MuratMutantCubeCubeful.py').read())" # replace "D:\" with the path to the script file import sys experiment = 'MuratMutantCubeCubeful' # used in filenames to save games and matches # no mutant, bot auto-plays 20,000 cubeful games for both sides game = snum = gnum = 0 batch = 0 opoints = xpoints = points = winfactor = 0 matchfile = open('D:\\' + experiment + 'Session.txt', 'a') def getcube(): global cubeinfo, cubeowner, cubevalue, beaver cubeinfo = gnubg.cubeinfo() cubeowner = cubeinfo['cubeowner'] # 0=bot, 1=mutant, -1=centerd cubevalue = cubeinfo['cube'] beaver = cubeinfo['beavers'] def errorend(error): print(error) input('Enter to exit') sys.exit() while batch < 20: # loop to run number of games entered above gnubg.command('new session') gamesfile = open('D:\\' + experiment + str(int(batch)).zfill(6) + '.txt', 'a') game = opoints = xpoints = 0 while game < 1000: # loop to run number of games entered above game = game + 1 gnubg.command('new game') gnubg.command('end game') # bot plays game to the end for both players # game ended, build game filename and save getcube() gameinfo = gnubg.match(analysis=0, boards=0, statistics=1, verbose=0)['games'][-1] gnum = '_G' + str(batch + game).zfill(5) # game number winner = "_" + gameinfo['info']['winner'] # O or X won points = gameinfo['info']['points-won'] # points won integer winfactor = points / cubevalue if winfactor == 3: wintype = '_W3' # won backgammon elif winfactor == 2: wintype = '_W2' # won gammon elif winfactor == 1: wintype = '_W1' # won normal else: errorend('invalid multiplier') # this should never happen cval = '_C' + str(cubevalue).zfill(16) # cube value formatted points = int(cubevalue * winfactor) if winner == '_O': # to fix gnubg bug, players reversed winner = '_X' xpoints = xpoints + points elif winner == '_X': winner = '_O' opoints = opoints + points else: errorend('invalid winner') # this should never happen points = str(points).zfill(16) # points won formatted if gameinfo['game'][-1]['action'] == 'resign': endtype = '_R' # resigned elif gameinfo['game'][-1]['action'] == 'drop': endtype = '_D' # dropped elif gameinfo['game'][-1]['action'] == 'move': endtype = '_F' # finished else: errorend('invalid how game ended') # this should never happen lastacted = gameinfo['game'][-1]['player'] # player who made last action if lastacted == 'O': # fix gnubg bug, players reversed lastacted = 'X' elif lastacted == 'X': lastacted = 'O' else: errorend('invalid last player') # this should never happen cubeskill = round(gameinfo['stats']['O']['cube']['error-skill'], 4) # cube error for mutant cubeskill = '_E' + str(int(cubeskill * 1000)).zfill(6) # formatted without decimal filename = experiment + gnum + winner + points + cubeskill + cval + wintype + endtype + lastacted gamesfile.write(filename + '\n') gamesfile.close() # session ended, build match filename and save snum = '_S' + str(batch).zfill(5) # session begin number + games in session opointe = '_O' + str(opoints).zfill(16) # O's total points won xpointe = '_X' + str(xpoints).zfill(16) # X's total points won gameinfo = gnubg.match(analysis=0, boards=0, statistics=1, verbose=0)['stats'] cubecount = gameinfo['O']['cube']['total-cube'] # total cube actions for mutant cubecount = '_CC' + str(cubecount).zfill(6) # formatted without decimal cubeskill = round(gameinfo['O']['cube']['error-skill'], 4) # total cube error for mutant cubeskill = '_ES' + str(int(cubeskill * 10000)).zfill(8) # formatted without decimal cubecost = round(gameinfo['O']['cube']['error-cost'], 2) # total cube error for mutant cubecost = '_EC' + str(int(cubecost * 100)).zfill(8) # formatted without decimal filename = experiment + snum + gnum + opointe + xpointe + cubecount + cubeskill + cubecost matchfile.write(filename + '\n') filename = 'save match D:\\' + filename + '.sgf' gnubg.command(filename) batch = batch + 1 matchfile.close() print('\a') # sounds bell to alert that it's done # all prefixes starting with an "_" are unique single characters # to use in wild-card filename searches within a directory # in order to select certain games, i.e. *_W3* to select games ended in a backgammon # prefixes used: _G, _O, _X, _C, _W, _R, _D, _F, _M, _E, _S, _CC, _ES, _EC