def do_POST(self): response = {'board': [], 'last_move': [], 'info': []} post_data = self.rfile.read(int( self.headers['Content-Length'])).decode('utf-8') data = parse_qs(post_data) command = data['command'][0] print(command) prev_game = gnubg.match(0)['games'][-1]['game'] if gnubg.match( 0) else [] gnubg.command(command) # check if the game is started/exists (handle the case the command executed is set at the beginning) if gnubg.match(0): # get the board after the execution of a move response['board'] = gnubg.board() # get the last games games = gnubg.match(0)['games'][-1] # get the last game game = games['game'][-1] # save the state of the game before and after having executed a command response['last_move'] = [prev_game, game] # save the info al all games played so far for idx, g in enumerate(gnubg.match(0)['games']): info = g['info'] response['info'].append({ 'winner': info['winner'], 'n_moves': len(g['game']), 'resigned': info['resigned'] if 'resigned' in info else None }) self._set_headers() self.wfile.write(json.dumps(response))
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 playMatchSeries( statsFile=None, matchLength=7, noOfMatches=100, sgfBasePath=None, matBasePath=None # log file # optional ): # 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 PlayGame(board, num_games=1): bg.command("set automatic game on") # Auto play bg.command("set jacoby off") # I don't know, I just turned it off anyway... bg.command("set crawford off") # I don't know, I just turned it off anyway... bg.command("set automatic crawford off") bg.command("set automatic roll off") bg.command("set automatic move off") bg.command("set matchlength 0") bg.command("set cube use on") bg.command("new match") bg.command("set player 0 name O") bg.command("set player 1 name X") command_set_board = "set board simple " + ' '.join(str(x) for x in board) for count in range(num_games): bg.command("new game") # bg.command(command_set_board) # print("BOARD IS SET!!!") # board = bg.board() # simple_board = BoardPositionToSimple(board) # print(board) # print(simple_board) winner = None while winner is None: cube_info = bg.cubeinfo() cube_owner = cube_info['cubeowner'] # whosmove = cube_info['move'] bg.command("swap players") bg.command("play") bg.command("swap players") bg.command("play") game_info = bg.match(0,1,0,0)['games'][0]['info'] winner = game_info['winner'] print(game_info) # raw_input("Press Enter to continue...") # bg.command("end game") # bg.command("end match") match_info = bg.match(0,1,0,0)['games'] for game_info in match_info: print(game_info['info']) winner = game_info['info']['winner'] if winner is None: points_won = 0 elif winner=='X': points_won = -game_info['info']['points-won'] elif winner=='O': points_won = game_info['info']['points-won'] else: raise KeyError("NO INFO!") print(winner, points_won)
def reset(self): bg.command("new match") bg.command("set automatic game off") # Auto play bg.command("set jacoby off") # I don't know, I just turned it off anyway... bg.command("set crawford off") # I don't know, I just turned it off anyway... bg.command("set automatic crawford off") bg.command("set automatic roll off") bg.command("set automatic move off") bg.command("set matchlength 0") bg.command("set cube use off") bg.command("set player 0 name O") bg.command("set player 1 name X") bg.command("new game") bg.command("end game") game_info = bg.match(0,1,0,1)['games'] print(len(game_info)) game_info = game_info[0] self._moves = [move for move in game_info['game'] if move.get('board') and move['player']=='X'] self._num_moves = len(self._moves) self._move_count = 0 #for move in self._moves: # print(move) observation = bg.positionfromid(self._moves[self._move_count]['board']) observation = np.concatenate(observation) return observation
def step(self, action): move = self._moves[self._move_count] bg.command("new match") bg.command("set cube use on") bg.command("new game") bg.command("set board {}".format(move['board'])) bg.command("set turn {}".format(move['player'])) if action==1: bg.command("double") bg.command("end game") game_info = bg.match(0,1,0,0)['games'][0]['info'] winner = game_info['winner'] if winner=='X': points_won = -game_info['points-won'] elif winner=='O': points_won = game_info['points-won'] reward = points_won self._move_count += 1 if self._move_count==self._num_moves: done = True observation = bg.positionfromid(move['board']) else: done = False observation = bg.positionfromid(self._moves[self._move_count]['board']) info = bg.positionfromid(move['board']) info = np.concatenate(info) observation = np.concatenate(observation) return observation, reward, done, info