예제 #1
0
파일: ai.py 프로젝트: LuneZ99/2048
def choose_min_max(m0, q0):
    dic = {}
    for d1 in (1, 2, 3, 4):
        if g.allow(m0, d1):
            dic[d1] = {'m': g.move(m0, d1)[0]}
            for d2 in range(4):
                for d3 in range(4):
                    if dic[d1]['m'][d2][d3] == 0:
                        dic[d1][str(d2) + str(d3) + '2'] = g.gen_xy2(dic[d1]['m'], (d2, d3))
                        dic[d1][str(d2) + str(d3) + '4'] = g.gen_xy4(dic[d1]['m'], (d2, d3))
            del(dic[d1]['m'])

    for c1 in dic:
        if c1 != {}:
            for c2 in dic[c1]:
                dic[c1][c2] = t.test(dic[c1][c2])

    for c1 in dic:
        if c1 != {}:
            dic[c1] = dic[c1][min(dic[c1], key=dic[c1].get)]
        else:
            dic[c1] = float('-inf')

    d = max(dic, key=dic.get)

    return d
예제 #2
0
파일: ai.py 프로젝트: LuneZ99/2048
def choose_max_depth(m0, q0, d0):
    dt = 0
    mt = c.deepcopy(m0)
    lst0 = []
    lst0.append(mt)
    lst1 = []
    mf = [
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ]

    while True:
        dt += 1

        for jt in lst0:
            for it in (1, 2, 3, 4):
                if dt == 1:
                    lst1.append((g.move(jt, it)[0], it))
                else:
                    lst1.append((g.move(jt[0], it)[0], jt[1]))
        lst0 = []
        for m1 in lst1:
            for xt in range(3):
                for yt in range(3):
                    if m1[0][xt][yt] == 0:
                        lst0.append((g.gen_xy2(m1[0], (xt, yt)), m1[1]))
                        lst0.append((g.gen_xy4(m1[0], (xt, yt)), m1[1]))

        if dt == d0:
            break

    dr = []
    for mr in lst0:
        dr.append(t.test(mr[0], q0))
    s = dr.index(max(dr))
    return lst0[s][1]
예제 #3
0
파일: ai.py 프로젝트: LuneZ99/2048
def score(mt, qt):

    si = 0                   # 此项无用
    ls = [[], [], [], []]    # 上 下 左 右 移动一步,所有生成情况的评分表   f ∈ (-inf, inf)
    dl2 = [[], [], [], []]   # 评分表对应位置的生成坐标与值    (x, y, 2or4)

    for d1 in (1, 2, 3, 4):

        if g.allow(mt, d1) != 1:
            ls[d1 - 1] = [-float('inf')]
            dl2[d1 - 1] = []
            continue

        mp, si = g.move(mt, d1, si)

        dl1 = []
        for x in (0, 1, 2, 3):
            for y in (0, 1, 2, 3):
                if mp[x][y] == 0:
                    dl1.append((x, y))

        for d2 in dl1:
            md = g.gen_xy2(mp, d2)
            ls[d1 - 1].append(t.test(md))
            dl2[d1 - 1].append((d2[0], d2[1], 2))

            md = g.gen_xy4(mp, d2)
            ls[d1 - 1].append(t.test(md, qt))
            dl2[d1 - 1].append((d2[0], d2[1], 4))
    '''
    for line in ls:
        print(line)
    for line in dl2:
        print(line)
    '''
    return ls, dl2
예제 #4
0
파일: ai.py 프로젝트: LuneZ99/2048
def choose_ex(m0, q0):
    dic = {}
    for d1 in (1, 2, 3, 4):
        if g.allow(m0, d1):
            dic[d1] = {'m': g.move(m0, d1)[0]}
            for d2 in range(4):
                for d3 in range(4):
                    if dic[d1]['m'][d2][d3] == 0:
                        dic[d1][str(d2) + str(d3) + '2'] = g.gen_xy2(dic[d1]['m'], (d2, d3))
                        dic[d1][str(d2) + str(d3) + '4'] = g.gen_xy4(dic[d1]['m'], (d2, d3))
            del (dic[d1]['m'])

    for c1 in dic:
        if c1 != {}:
            for c2 in dic[c1]:
                dic[c1][c2] = t.test(dic[c1][c2])

    for c1 in dic:
        if c1 != {}:
            dic[c1] = dic[c1][min(dic[c1], key=dic[c1].get)]
        else:
            dic[c1] = float('-inf')

    k = 0
    for i in m0:
        for j in i:
            if j == 0:
                k += 1

    dm = max(dic, key=dic.get)
    for c1 in dic:
        if c1 != dm:
            p = r.random()
            if p < math.exp(((dic[c1]-dic[dm])/(k+1))):
                return c1
    return dm
예제 #5
0
파일: ai.py 프로젝트: LuneZ99/2048
def choose_max_depth_dic(m0, q0, d0):

    mt = c.deepcopy(m0)
    dic = {
        'm': mt
    }
    for a1 in ('1', '2', '3', '4'):
        if g.allow(dic['m'], int(a1)):
            dic[a1] = {
                'm': g.move(dic['m'], int(a1), 0)[0]
                }
    # for a1 in dic:
    #     if a1 != 'm':
            for xt in range(4):
                for yt in range(4):
                    if dic[a1]['m'][xt][yt] == 0:
                        dic[a1][str(xt) + str(yt) + '2'] = {'m': g.gen_xy2(dic[a1]['m'], (xt, yt))}
                        dic[a1][str(xt) + str(yt) + '4'] = {'m': g.gen_xy4(dic[a1]['m'], (xt, yt))}

    for b1 in dic:
        if b1 != 'm':
            for b2 in dic[b1]:
                if b2 != 'm':
                    for b3 in ('1', '2', '3', '4'):
                        if g.allow(dic[b1][b2]['m'], int(b3)):
                            dic[b1][b2][b3] = {'m': g.move(dic[b1][b2]['m'], int(b3), 0)[0]}
                            for xt in range(4):
                                for yt in range(4):
                                    if dic[b1][b2][b3]['m'][xt][yt] == 0:
                                        dic[b1][b2][b3][str(xt) + str(yt) + '2'] = {
                                            'm': g.gen_xy2(dic[b1][b2][b3]['m'], (xt, yt))}
                                        dic[b1][b2][b3][str(xt) + str(yt) + '4'] = {
                                            'm': g.gen_xy4(dic[b1][b2][b3]['m'], (xt, yt))}

    alpha = float('+inf')
    beta = float('-inf')

    for c1 in dic:
        if c1 != 'm':
            for c2 in dic[c1]:
                if c2 != 'm':
                    for c3 in dic[c1][c2]:
                        if c3 != 'm':
                            for c4 in dic[c1][c2][c3]:
                                if c4 != 'm':
                                    for c5 in ('1', '2', '3', '4'):
                                        if g.allow(dic[c1][c2][c3][c4]['m'], int(c5)):
                                            dic[c1][c2][c3][c4][c5] = {
                                                'm': g.move(dic[c1][c2][c3][c4]['m'], int(c5), 0)[0]
                                                }

                                            for xt in range(4):
                                                for yt in range(4):
                                                    if dic[c1][c2][c3][c4][c5]['m'][xt][yt] == 0:
                                                        dic[c1][c2][c3][c4][c5][str(xt) + str(yt) + '2'] = {
                                                            'm': g.gen_xy2(dic[c1][c2][c3][c4][c5]['m'], (xt, yt))
                                                            }
                                                        dic[c1][c2][c3][c4][c5][str(xt) + str(yt) + '4'] = {
                                                            'm': g.gen_xy4(dic[c1][c2][c3][c4][c5]['m'], (xt, yt))
                                                            }
                                        '''
                                        for c6 in dic[c1][c2][c3][c4][c5]:
                                            if c6 != 'm':
                                                dic[c1][c2][c3][c4][c5][c6] = t.test(
                                                    dic[c1][c2][c3][c4][c5][c6]['m'])
                                                if dic[c1][c2][c3][c4][c5][c6] < beta:
                                                    beta = dic[c1][c2][c3][c4][c5][c6]
                                                    break
                                        '''
    # pp = pprint.PrettyPrinter(indent=4, width=100)
    # pp.pprint(dic)
    del(dic['m'])
    for c1 in dic:
        del(dic[c1]['m'])
        for c2 in dic[c1]:
            del(dic[c1][c2]['m'])
            for c3 in dic[c1][c2]:
                del(dic[c1][c2][c3]['m'])
                for c4 in dic[c1][c2][c3]:
                    del(dic[c1][c2][c3][c4]['m'])
                    for c5 in dic[c1][c2][c3][c4]:
                        del (dic[c1][c2][c3][c4][c5]['m'])

    # pp = pprint.PrettyPrinter(indent=4, width=100)
    # pp.pprint(dic)
    for c1 in dic:
        for c2 in dic[c1]:
            for c3 in dic[c1][c2]:
                for c4 in dic[c1][c2][c3]:
                    for c5 in dic[c1][c2][c3][c4]:
                        for c6 in dic[c1][c2][c3][c4][c5]:
                            dic[c1][c2][c3][c4][c5][c6] = t.test(dic[c1][c2][c3][c4][c5][c6]['m'])

                        if dic[c1][c2][c3][c4][c5] != {}:
                            dic[c1][c2][c3][c4][c5] = dic[c1][c2][c3][c4][c5][
                                min(dic[c1][c2][c3][c4][c5], key=dic[c1][c2][c3][c4][c5].get)]
                        else:
                            dic[c1][c2][c3][c4][c5] = float('-inf')

                    if dic[c1][c2][c3][c4] != {}:
                        dic[c1][c2][c3][c4] = dic[c1][c2][c3][c4][max(dic[c1][c2][c3][c4], key=dic[c1][c2][c3][c4].get)]
                    else:
                        dic[c1][c2][c3][c4] = float('+inf')

                if dic[c1][c2][c3] != {}:
                    dic[c1][c2][c3] = dic[c1][c2][c3][min(dic[c1][c2][c3], key=dic[c1][c2][c3].get)]
                else:
                    dic[c1][c2][c3] = float('-inf')
            if dic[c1][c2] != {}:
                dic[c1][c2] = dic[c1][c2][max(dic[c1][c2], key=dic[c1][c2].get)]
            else:
                dic[c1][c2] = float('+inf')
        if dic[c1] != {}:
            dic[c1] = dic[c1][min(dic[c1], key=dic[c1].get)]
        else:
            dic[c1] = float('-inf')
    d = max(dic, key=dic.get)
    return int(d)