コード例 #1
0
def test_move(pos_list, turn, depth, test_strategy, h):
    ''' Prints the move suggested by the given strategy..
    
        pos_list -- a list of nonnegative integers giving the number of seeds in each pit in Kalah
        turn -- 0 or 1 to indicate which player is to make the next move
        depth -- a positive integer giving the depth to search to
        test_strategy -- a function that takes a depth and heuristic and returns
                         a function that takes a position and returns the move suggested
                         by a search to the given depth
        h -- a heuristic function that takes a game position and returns an integer heuristic
             value, positive for positions where P1 has an advantage and negative where
             P2 has an advantage
    '''
    board = Kalah(len(pos_list) // 2 - 1)
    pos = Kalah.Position(board, pos_list, turn)
    h = minimax.Heuristic(h)
    strategy = test_strategy(depth, h)
    print(strategy(pos))
コード例 #2
0
            return None
        if len(pos.legal_moves()) == 1:
            return pos.legal_moves()[0]
        # check if initial
        if pos.is_initial():
            nodeDict.clear()

        for i in range(itermax):
            mcts(pos)

        posPlayer = pos.next_player()
        reslist = list()
        for move in pos.legal_moves():
            res = pos.result(move)
            value = nodeDict.get((posPlayer, res), [0, 0])[1] / nodeDict.get(
                (posPlayer, res), [1, 1])[0]
            reslist.append((value, move))
        # return the move with the maximum value of wins/visits
        return sorted(reslist, key=lambda x: x[0])[-1][1]

    return fxn


if __name__ == '__main__':
    b = Kalah(6)
    pos = Kalah.Position(b, [6, 4, 2, 0, 0, 2, 9, 0, 0, 2, 2, 6, 6, 9], 0)
    print(mcts_strategy(1000)(pos))

    pos = Kalah.Position(b, [0, 0, 2, 2, 6, 6, 9, 6, 4, 2, 0, 0, 2, 9], 1)
    print(mcts_strategy(1000)(pos))