Esempio n. 1
0
def READY_configuration(game):
    # Game Configuration
    response = client.handle_ready()

    if not response[0]:
        # server END message
        return False, response[1]

    parameters = response[1]
    ''' 
        Start Game configuration
    '''
    # Initial State
    for y,row in enumerate(parameters['initialState']['board']):
        for x,color in enumerate(row):
            if color == '.':
                continue
            game.next_player = gotypes.Player.black if color == 'B' else gotypes.Player.white
            move = goboard.Move(gotypes.Point((y+1,x+1)))
            game,_ = game.apply_move(move)
    
    captures = {
        gotypes.Player.black : parameters['initialState']['players']['B']['prisoners'],
        gotypes.Player.white : parameters['initialState']['players']['W']['prisoners'],
    }
    
    remainingTime = {
        gotypes.Player.black : parameters['initialState']['players']['B']['remainingTime'],
        gotypes.Player.white : parameters['initialState']['players']['W']['remainingTime'],
    }

    game.next_player = gotypes.Player.black if parameters['initialState']['turn'] == 'B' else gotypes.Player.white
    # fill the board with move logs
    for logEntry in parameters['moveLog']:
        move = logEntry["move"]
        deltaTime = logEntry['deltaTime']
        if move['type'] == 'pass':
            move = goboard.Move(is_pass=True)
        elif move['type'] == 'resign':
            move = goboard.Move(is_resign=True)
        else:
            col = move['point']['column'] + 1
            row = move['point']['row'] + 1
            move = goboard.Move(gotypes.Point(row,col))

        remainingTime[game.next_player] -= deltaTime
        game,numberOfCaptures = game.apply_move(move)
        if game.next_player == gotypes.Player.white:
            captures[gotypes.Player.black] += numberOfCaptures
        else:
            captures[gotypes.Player.white] += numberOfCaptures

    ourColor = gotypes.Player.black if parameters['ourColor'] == 'B' else gotypes.Player.white
    ''' 
        End Game configuration
    '''

    return game, captures, remainingTime, ourColor
Esempio n. 2
0
def THINKING(game, captures):
    global agent
    global encoder
    played = False
    remaining_time = None
    captures = {
        '0': captures[gotypes.Player.black],
        '1': captures[gotypes.Player.white]
    }

    moves_count = 0
    while not played:
        point = -1
        player = '0' if game.next_player == gotypes.Player.black else '1'

        if moves_count == 0 or True:
            move_result, new_game, new_captures, play_point = monte_carlo_tree_search(
                agent, encoder, game, point, player, num_rounds, captures,
                depth,
                len(game.legal_moves()) - 2)
            # print(new_captures , play_point)
        else:
            # another option
            pass
        if (move_result):
            decision = 0
        else:
            decision = 1
        b_time = 0
        w_time = 0
        moves_count += 1

        play_move = goboard.Move(play_point)
        client.handle_thinking(play_move)

        response = client.handle_await_response()
        if not response[0]:
            # server END msg
            return False, response[1]

        if response[1]['valid']:
            played = True
            game = new_game
            captures = {
                gotypes.Player.black: new_captures['0'],
                gotypes.Player.white: new_captures['1']
            }
            remaining_time = response[1]['remaning_time']

        if not response[1]['valid']:
            reason = response[1]['message']
            # print('not valid: ', reason)
            remaining_time = response[1]['remaning_time']

    # send_move_to_gui(decision,play_point,b_time,w_time,player)
    # send_board_to_gui(decision,game.board)

    return game, captures, remaining_time, play_move
Esempio n. 3
0
def IDLE(game, captures):
    response = client.handle_idle()
    if not response[0]:
        # server END msg
        return False, response[1]

    if response[1]['type'] == 'place':
        point = gotypes.Point(response[1]['row'] + 1, response[1]['column'] + 1)
        move = goboard.Move(point)
    elif response[1]['type'] == 'pass':
        move = goboard.Move(is_pass = True)
    elif response[1]['type'] == 'resign':
        move = goboard.Move(is_resign = True)
    
    game, prisoners = game.apply_move(move)
    captures[game.next_player.other] += prisoners

    return game, captures, response[1]['remaning_time'], move
Esempio n. 4
0
def get_game_mode_from_gui():
    global game_mode
    game_mode = int(interface.get_game_mode())
    # # print("game mode received --> ", game_mode)
    game = goboard.GameState.new_game(board_size)
    player = '0'
    opponent = '1'
    captures = {
        '0': 0,
        '1': 0,
    }
    if (game_mode == 1):  # PC mode
        #get player color from server
        # # print('ai vs ai')
        pass
    elif (game_mode == 0):
        # # print('ai vs human')
        player, opponent = get_player_color_from_gui()
        initial_state = interface.get_initial_board()
        # print("initial Board len",len(initial_state))
        if (len(initial_state) == 0):
            pass
        else:
            # for move_ in initial_state:
            #     if move_[i][2] == '.':
            #         continue
            #     game.next_player = gotypes.Player.black if move_[i][2] == '0' else gotypes.Player.white
            #     move = goboard.Move(gotypes.Point((int(move_[i][1]),int(move_[i][0]))))
            #     game,captures = game.apply_move(move)
            for i in range(0, len(initial_state)):
                point = gotypes.Point(row=int(initial_state[i][1]),
                                      col=int(initial_state[i][0]))
                move = goboard.Move(point)
                # print(">>>> move", move.point)
                next_player = gotypes.Player.black
                prisoners = [0]
                turn = '0'
                if (initial_state[i][2] == '1'):
                    next_player = gotypes.Player.white
                    turn = '1'
                game.next_player = next_player
                game, _ = game.apply_move(move)
                if (prisoners[0] > 0):
                    captures[turn] += prisoners[0]
                    send_board_to_gui(0, game.board)
        # print(opponent)

    return game, captures, player, opponent
Esempio n. 5
0
def get_opponent_game_from_gui(current_state,captures,opponent):
    # # print("get_opponent_game_from_gui")
    global consequitive_passes
    global opponont_resigns
    new_game_state= current_state
    point = 0
    decision = interface.get_opponent_move().split('#')
    if decision[0] == '0' : # play  
        consequitive_passes = 0
        # print("decision ", decision)
        pos = decision[1].split('-')
        point =  gotypes.Point(row= int(pos[1]),col= int(pos[0]))
        move = goboard.Move(point)
        # # print(">>>> move", move.point)
        new_game_state , prisoners  = current_state.apply_move(move)
        captures[opponent]+=prisoners
    elif decision[0] == '1' :  # opponont_opponont_resignss
        opponont_resigns = True
    elif decision[0] == '2' : # pass
        consequitive_passes+=1
    return decision[0] , new_game_state , captures , point
Esempio n. 6
0
def recommend_move(game_state):
    state = elevenplanes.ElevenPlaneEncoder((19,19))
    state = state.encode(game_state)
    state = np.expand_dims(state,axis=0)
    probability_matrix=predict.model.predict(state)[0]
    probability_matrix = np.reshape(probability_matrix, (-1, 19))
    new_point = -1
    for j in range(361):
        max = probability_matrix.max()
        coordinates = np.where(probability_matrix == max)
        row = coordinates[0][0]
        col = coordinates[1][0]
        probability_matrix[row][col]= 0
        new_point = gotypes.Point( row=row+1,col=col+1)
        move = goboard.Move(new_point)
        if game_state.is_valid_move(move):
            break
    if(j == 361):
        return False , game_state , new_point
    new_game_state , prisoners  = game_state.apply_move(move)
    # print('recommend move function',new_point)
    return True , new_game_state , new_point
Esempio n. 7
0
def get_best_three(root, available_moves):
    state = elevenplanes.ElevenPlaneEncoder((19, 19))
    state = state.encode(root.game_state)
    state = np.expand_dims(state, axis=0)

    probability_matrix = predict.model.predict(state)[0]
    probability_matrix = np.reshape(probability_matrix, (-1, 19))
    num_moves = 3
    if (available_moves == 0):
        return False
    if (available_moves < num_moves):
        num_moves = available_moves
    for i in range(num_moves):
        for k in range(361):
            max = probability_matrix.max()
            coordinates = np.where(probability_matrix == max)
            row = coordinates[0][0]
            col = coordinates[1][0]
            probability_matrix[row][col] = 0
            new_point = gotypes.Point(row=row + 1, col=col + 1)
            move = goboard.Move(new_point)
            ## print(new_point)
            if root.game_state.is_valid_move(move):
                break
        if (k == 361):
            return False
        #print('move ',move)
        legal_state, prisoners = root.game_state.apply_move(move)
        capture = 0
        ## print('prisoners',prisoners)
        child_captures = copy.copy(root.captures)
        child_captures[player] += prisoners
        child = MCTS_node(legal_state, root, child_captures, new_point)
        root.children.append(child)
    # print(probability_matrix)
    return True
Esempio n. 8
0
def rollout(node, depth):
    game_state = node.game_state
    parent = node
    j = 0
    global encoding_time
    global move_time
    global cnn_time
    global prisoners_time
    prisoners = 0
    while not game_state.is_over() and j < depth:
        j += 1
        t1 = time.time()
        state = elevenplanes.ElevenPlaneEncoder((19, 19))
        state = state.encode(game_state)
        t2 = time.time()
        state = np.expand_dims(state, axis=0)

        probability_matrix = predict.model.predict(state)[0]
        probability_matrix = np.reshape(probability_matrix, (-1, 19))

        t3 = time.time()
        new_point = 0
        for j in range(361):
            max = probability_matrix.max()
            coordinates = np.where(probability_matrix == max)
            row = coordinates[0][0]
            col = coordinates[1][0]
            probability_matrix[row][col] = 0
            new_point = gotypes.Point(row=row + 1, col=col + 1)
            move = goboard.Move(new_point)
            if game_state.is_valid_move(move):
                break
        if (j == 361):
            break
        new_game_state, prisoners = game_state.apply_move(move)

        t4 = time.time()
        capture = 0
        child_captures = copy.copy(parent.captures)
        child_captures[player] += prisoners

        t5 = time.time()
        new_node = MCTS_node(new_game_state, parent, child_captures, new_point)
        # check if game is over
        game_state = new_game_state
        parent = new_node
        encoding_time += (t2 - t1)
        cnn_time += (t3 - t2)
        move_time += (t4 - t3)
        prisoners_time += (t5 - t4)
    # evaluate game state

    last_captures = copy.copy(parent.captures)
    # # print(last_captures)
    game_captures = {
        gotypes.Player.black: last_captures['0'],
        gotypes.Player.white: last_captures['1']
    }
    winner, _ = game_state.semi_winner(game_captures)
    result = '0'
    if (winner == gotypes.Player.white):
        result = '1'
    return result, parent