コード例 #1
0
ファイル: server.py プロジェクト: Nail1959/Code_Go
    def select_move(bot_name):
        content = request.json
        board_size = content['board_size']
        game_state = goboard.GameState.new_game(board_size)

        # Replay the game up to this point.
        for move in content['moves']:
            if move == 'pass':
                next_move = goboard.Move.pass_turn()
            elif move == 'resign':
                next_move = goboard.Move.resign()
            else:
                next_move = goboard.Move.play(point_from_coords(move))
            game_state = game_state.apply_move(next_move)

        bot_agent = bot_map[bot_name]

        bot_move = bot_agent.select_move(game_state)
        if bot_move.is_pass:
            bot_move_str = 'pass'
        elif bot_move.is_resign:
            bot_move_str = 'resign'
        else:
            bot_move_str = coords_from_point(bot_move.point)

        result_scoring = gr(game_state)
        print('Current Result = ', result_scoring, ' Bot_move = ',
              bot_move_str)
        print('Diagnostics: ', bot_agent.diagnostics())
        return jsonify({
            'bot_move': bot_move_str,
            'diagnostics': bot_agent.diagnostics()
        })
コード例 #2
0
ファイル: my_server.py プロジェクト: Nail1959/Code_Go
    def select_move(bot_name):
        content = request.json
        board_size = content['board_size']
        game_state = goboard.GameState.new_game(board_size)
        board_ext = goboard.Board_Ext(game_state.board)  #Nail
        # Replay the game up to this point.
        for move in content['moves']:
            if move == 'pass':
                next_move = goboard.Move.pass_turn()
            elif move == 'resign':
                next_move = goboard.Move.resign()
            else:
                pm = point_from_coords(move)
                next_move = goboard.Move.play(pm)
            game_state = game_state.apply_move(next_move)

            p = next_move.point  # Nail
            #board_ext.place_stone_ext(game_state.board, game_state.next_player.other.name[0], p)  #Nail
        bot_agent = bot_map[bot_name]

        bot_move  = bot_agent.select_move(game_state)  #,board_ext)  # Nail
        if bot_move.is_pass:
            bot_move_str = 'pass'
        elif bot_move.is_resign:
            bot_move_str = 'resign'
        else:
            bot_move_str = coords_from_point(bot_move.point)

        result_scoring, territory_black, territory_white = gr(game_state)  # Nail
        winner = result_scoring.winner.name
        if winner == 'white':
            winner = 'Белые'
        else:
            winner = 'Черные'
        score = str(result_scoring.winning_margin)
        result_scoring = result_scoring.winner.name + ' : ' + str(result_scoring.winning_margin)
        print(' Current Result = ', result_scoring, ' Bot_move = ', bot_move_str) #Nail
        print('territory_black=', territory_black, '  territory_white=', territory_white)
        return jsonify({
            'score': score,
            'winner': winner,
            'territory_black': territory_black,
            'territory_white': territory_white,
            'bot_move': bot_move_str,
            'diagnostics': bot_agent.diagnostics()
        })
コード例 #3
0
ファイル: predict.py プロジェクト: Nail1959/Code_Go
    def my_select_move(self, game_state, board_ext=None):
        num_moves = self.encoder.board_width * self.encoder.board_height
        move_probs = self.predict(game_state, board_ext)
        # end::dl_agent_predict[]

        # tag::dl_agent_probabilities[]
        move_probs = move_probs**3  # <1>
        eps = 1e-6
        move_probs = np.clip(move_probs, eps, 1 - eps)  # <2>
        move_probs = move_probs / np.sum(move_probs)  # <3>
        # <1> Increase the distance between the move likely and least likely moves.
        # <2> Prevent move probs from getting stuck at 0 or 1
        # <3> Re-normalize to get another probability distribution.
        # end::dl_agent_probabilities[]

        # tag::dl_agent_candidates[]
        candidates = np.arange(num_moves)  # <1>
        ranked_moves = np.random.choice(candidates,
                                        num_moves,
                                        replace=False,
                                        p=move_probs)  # <2>

        possible_point = []  # Список всех доступных ходов предложенных сетью.
        wait_score = 0  # Счет на доске, выбрать ход приносящий максимально допустимый счет #<5>
        for point_idx in ranked_moves:
            point = self.encoder.decode_point_index(point_idx)
            if game_state.is_valid_move(goboard.Move.play(point)) and \
                    not is_point_an_eye(game_state.board, point, game_state.next_player):  # <3>
                possible_point.append(point)
        if not possible_point:  # Нет допустимых ходов, тогда пас.
            return goboard.Move.pass_turn(), wait_score  # <4>
        # Выбрать из всех возможных ходов приносящий лучший счет на доске
        #cand = []
        for p in possible_point:
            game_state_copy = copy.deepcopy(game_state)
            next_move = goboard.Move.play(p)
            game_state_copy = game_state_copy.apply_move(next_move)
            res = str(gr(game_state_copy)[0])[
                1:]  # Отбрасываю B или W, оставляю знак
            res = float(res)
            if res > wait_score:
                wait_score = res
                point = p
                #cand.append(p)

        return goboard.Move.play(point), wait_score
コード例 #4
0
def territory(game_state):

    return gr(game_state)
コード例 #5
0
def territory_diff(game_state):
    res, tb, tw = gr(game_state)
    #print('result = ',res, 'territory_diff = ', tb - tw)
    return tb - tw