# To run this script # open a Command Prompt or PowerShell in "C:\Program Files (x86)\gnubg>" # and run ".\gnubg-cli.exe --python=D:\MuratMutantCubeFirstCube.py" # or run .\gnubg-cli.exe then type ">"+[ENTER] to get in Python interpreter # and run "exec(open('D:\MuratMutantCubeFirstCube.py').read())" # replace "D:\" with the path to the script file import sys experiment = 'MuratMutantCubeFirstCube' # used in filenames to save games and matches # mutant owns 2 cube if it wins the opening roll, bot auto-plays the rest of the game # this seems pointless?? session = games = game = movecount = snum = gnum = 0 opoints = xpoints = cubevalue = points = winfactor = 0 setowner = 'set cube owner 1' setcube = 'set cube value 2' endgame = 'end game' session = int(input('Enter beginning game: ')) # i.e. 0, 1000, 2000, etc. games = int(input('Enter games to run: ')) # games in filenames will start with session+1 saveall = input('Save individual games? y/n: ') matchlog = input('Save matches to logfile? y/n: ') gameslog = input('Save games to logfile? y/n: ') if matchlog == 'y': matchfile = open('D:\\' + experiment + 'Session.txt', 'a') if gameslog == 'y': gamesfile = open('D:\\' + experiment + str(int(session)).zfill(6) + '.txt', 'a') def getpos(): global posinfo, dice, player, resigned, doubled, gamestate posinfo = gnubg.posinfo() dice = posinfo['dice'] player = posinfo['turn'] resigned = posinfo['resigned'] doubled = posinfo['doubled'] gamestate = posinfo['gamestate'] # 0=no game, 1=playing, 2=over, 3=resigned, 4=dropped 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 game < games: # loop to run number of games entered above game = game + 1 gnubg.command('new game') getpos() if dice != (0, 0): # mutant won the opening roll gnubg.command(setowner) # mutant owns cube gnubg.command(setcube) # just to compare cube owned by bot vs by mutant gnubg.command(endgame) # 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(session + 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 if gameslog == 'y': gamesfile.write(filename + '\n') filename = 'save game D:\\' + filename + '.sgf' if saveall == 'y': gnubg.command(filename) # session ended, build match filename and save snum = '_S' + str(session).zfill(5) # session begin number + games in session opoints = '_O' + str(opoints).zfill(16) # O's total points won xpoints = '_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 + opoints + xpoints + cubecount + cubeskill + cubecost if matchlog == 'y': matchfile.write(filename + '\n') filename = 'save match D:\\' + filename + '.sgf' gnubg.command(filename) gamesfile.close() 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