Exemplo n.º 1
0
def process():
    global a_mat
    global topfr
    global boardlist
    global blist
    global status
    global model

    l = np.where(a_mat.reshape(-1) == 0)[0]
    val, pos = pick(l)
    a_mat[int(pos / 4)][pos % 4] = val
    boardlist[int(pos / 4)][pos % 4] = Button(topfr,
                                              text=(val),
                                              bg=bgcol[num.index(val)],
                                              fg=fgcol[num.index(val)],
                                              font=helv36)
    coor = [60 + 50 * (pos % 4), 60 + 50 * (int(pos / 4))]
    boardlist[int(pos / 4)][pos % 4].place(x=coor[0], y=coor[1])
    boardlist[int(pos / 4)][pos % 4].config(width=4, height=2)
    print(a_mat, '\n')
    tot = 0
    l = logic_2048.check(a_mat)
    stlist = ['up', 'down', 'right', 'left']
    for st in stlist:
        s = len(l[st])
        for bt in blist:
            if bt['text'] == st:
                if s <= 0:
                    bt['bg'] = 'slate gray'
                    tot += 1
                else:
                    bt['bg'] = 'black'
    if tot != 4:
        None
        root.after(12, process2)

    else:
        alpha = 0.21
        gamma = 0.9
        reward = -1
        asc = np.log(a_mat + 1).reshape(1, 4, 4, 1)
        val0 = model.predict(asc)
        model.fit(asc, (1 - alpha) * val0 + alpha * (reward + gamma * val0),
                  epochs=5)
        close()
Exemplo n.º 2
0
def right():
    global a_mat
    global boardlist
    global root
    global num
    global status
    global timedelay

    status.destroy()
    m = -1
    l = logic_2048.check(a_mat)
    s = l['right']
    if len(s) <= 0:
        status = Label(root,
                       text='Error',
                       bd=1,
                       relief=SUNKEN,
                       anchor=W,
                       font=helv12)
        status.pack(side=BOTTOM, fill=X)
        return
    status.destroy()
    for j in list(s):
        i = 2
        init = 0
        m = -2
        while i >= 0:
            if i < 3 and a_mat[j, i + 1] == 0:
                init += 1
            if a_mat[j, i] != 0 and init != 0:
                p = 1
                ia = [i for i in range(0, i + 1)]

                ep = init
                for temp in ia:
                    pos = j * 4 + temp
                    if (boardlist[int(pos / 4)][pos % 4] != 0):
                        coor = [
                            60 + 50 * (pos % 4) + p * ep * 50,
                            60 + 50 * (int(pos / 4))
                        ]
                        boardlist[int(pos / 4)][pos % 4].place(x=coor[0],
                                                               y=coor[1])

                a_mat[j, ep:i + ep + 1] = a_mat[j, 0:i + 1]
                a_mat[j, 0:ep] = 0

                for k in range(i, -1, -1):
                    boardlist[j][k + ep] = boardlist[j][k]
                for lf in range(0, ep):
                    boardlist[j][lf] = 0

                i = i + ep + 1

                init = 0
            elif i < 3 and a_mat[j, i] != 0 and a_mat[j, i + 1] == a_mat[
                    j, i] and i != m:
                init = 0
                ep = 1
                p = 1
                a_mat[j, ep:i + ep + 1] = a_mat[j, 0:i + 1]
                a_mat[j, 0:ep] = 0
                a_mat[j, i + 1] *= 2
                ia = [i for i in range(0, i + 1)]
                boardlist[j][i + 1].destroy()
                for temp in ia:
                    pos = 4 * j + temp
                    if (boardlist[int(pos / 4)][pos % 4] != 0):
                        coor = [
                            60 + 50 * (pos % 4) + p * ep * 50,
                            60 + 50 * (int(pos / 4))
                        ]
                        boardlist[int(pos / 4)][pos % 4].place(x=coor[0],
                                                               y=coor[1])
                for k in range(i, -1, -1):
                    boardlist[j][k + ep] = boardlist[j][k]
                for lf in range(0, ep):
                    boardlist[j][lf] = 0

                boardlist[j][i + 1]['text'] = str(int(a_mat[j, i + 1]))
                boardlist[j][i + 1]['bg'] = bgcol[num.index(
                    int(a_mat[j, i + 1]))]
                boardlist[j][i + 1]['fg'] = fgcol[num.index(
                    int(a_mat[j, i + 1]))]
                m = i

            i = i - 1
    print('Right')
    root.after(timedelay, process)
Exemplo n.º 3
0
def left():
    global a_mat
    global boardlist
    global root
    global num
    global status
    global timedelay

    m = -1
    l = logic_2048.check(a_mat)
    s = l['left']
    status.destroy()
    if len(s) <= 0:
        status = Label(root,
                       text='Error',
                       bd=1,
                       relief=SUNKEN,
                       anchor=W,
                       font=helv12)
        status.pack(side=BOTTOM, fill=X)
        return
    status.destroy()
    for j in list(s):
        i = 1
        init = 0
        m = -2
        while i < 4:
            if i > 0 and a_mat[j, i - 1] == 0:
                init = init + 1

            if a_mat[j, i] != 0 and init != 0:
                p = 1
                ia = [i for i in range(i, 4)]

                ep = init
                for temp in ia:
                    pos = j * 4 + temp
                    if (boardlist[int(pos / 4)][pos % 4] != 0):
                        coor = [
                            60 + 50 * (pos % 4) - p * ep * 50,
                            60 + 50 * (int(pos / 4))
                        ]
                        boardlist[int(pos / 4)][pos % 4].place(x=coor[0],
                                                               y=coor[1])

                a_mat[j, i - ep:4 - ep] = a_mat[j, i:4]
                a_mat[j, 4 - ep:4] = 0
                l = len(ia)
                for k in range(i - ep, 4 - ep):

                    boardlist[j][k] = boardlist[j][k + ep]
                for lf in range(4 - ep, 4):
                    boardlist[j][lf] = 0

                i = i - init - 1

                init = 0

            elif i > 0 and a_mat[j, i] != 0 and a_mat[j, i - 1] == a_mat[
                    j, i] and i != m:
                ia = [i for i in range(i, 4)]
                p = 1
                ep = 1
                init = 0

                for temp in ia:
                    pos = j * 4 + temp
                    if (boardlist[int(pos / 4)][pos % 4] != 0):
                        coor = [
                            60 + 50 * (pos % 4) - p * ep * 50,
                            60 + 50 * (int(pos / 4))
                        ]
                        boardlist[int(pos / 4)][pos % 4].place(x=coor[0],
                                                               y=coor[1])

                a_mat[j, i - ep:4 - ep] = a_mat[j, i:4]
                a_mat[j, 4 - ep:4] = 0
                a_mat[j, i - 1] *= 2

                boardlist[j][i - 1].destroy()
                for k in range(i - ep, 4 - ep):

                    boardlist[j][k] = boardlist[j][k + ep]
                for lf in range(4 - ep, 4):
                    boardlist[j][lf] = 0

                boardlist[j][i - 1]['text'] = str(int(a_mat[j, i - 1]))
                boardlist[j][i - 1]['bg'] = bgcol[num.index(
                    int(a_mat[j, i - 1]))]
                boardlist[j][i - 1]['fg'] = fgcol[num.index(
                    int(a_mat[j, i - 1]))]
                m = i
            i = i + 1
    print('Left')
    root.after(timedelay, process)
Exemplo n.º 4
0
def up():
    global a_mat
    global boardlist
    global root
    global num
    global status
    global timedelay

    m = -1
    p = 0
    l = logic_2048.check(a_mat)
    s = l['up']
    status.destroy()
    if len(s) <= 0:
        status = Label(root,
                       text='Error',
                       bd=1,
                       relief=SUNKEN,
                       anchor=W,
                       font=helv12)
        status.pack(side=BOTTOM, fill=X)
        return
    status.destroy()
    init = 0
    i = 1

    for j in list(s):
        i = 1
        init = 0
        m = -1
        while i < 4:
            if i > 0 and a_mat[i - 1, j] == 0:
                init = init + 1
            if a_mat[i, j] != 0 and init != 0:
                p = 1
                ia = [i for i in range(i, 4)]
                ep = init
                for temp in ia:
                    pos = temp * 4 + j
                    if (boardlist[int(pos / 4)][pos % 4] != 0):
                        coor = [
                            60 + 50 * (pos % 4),
                            60 + 50 * (int(pos / 4)) - p * ep * 50
                        ]
                        boardlist[int(pos / 4)][pos % 4].place(x=coor[0],
                                                               y=coor[1])

                a_mat[i - ep:i - ep + len(ia), j] = a_mat[i:4, j]
                a_mat[i - ep + len(ia):4, j] = 0
                l = len(ia)
                for lp in range(1, init + 1):
                    for k in range(i - lp, i - lp + l):
                        boardlist[k][j] = boardlist[k + 1][j]
                for lf in range(i - init + l, 4):
                    boardlist[lf][j] = 0

                i = i - init - 1
                init = 0

            elif i > 0 and a_mat[i][j] != 0 and a_mat[
                    i - 1][j] == a_mat[i][j] and i != m:
                ia = [i for i in range(i, 4)]
                l = len(ia)
                p = 1
                ep = 1
                for temp in ia:
                    pos = temp * 4 + j
                    if (boardlist[int(pos / 4)][pos % 4] != 0):
                        coor = [
                            60 + 50 * (pos % 4),
                            60 + 50 * (int(pos / 4)) - p * ep * 50
                        ]
                        boardlist[int(pos / 4)][pos % 4].place(x=coor[0],
                                                               y=coor[1])

                a_mat[i - ep:i - ep + len(ia), j] = a_mat[i:4, j]
                a_mat[i - ep + len(ia):4, j] = 0
                a_mat[i - 1, j] *= 2

                boardlist[i - 1][j].destroy()
                for lp in range(1, 2):
                    for k in range(i - lp, i - lp + l):
                        boardlist[k][j] = boardlist[k + 1][j]
                for lf in range(i - 1 + l, 4):
                    boardlist[lf][j] = 0

                boardlist[i - 1][j]['text'] = str(int(a_mat[i - 1, j]))
                boardlist[i - 1][j]['bg'] = bgcol[num.index(
                    int(a_mat[i - 1, j]))]
                boardlist[i - 1][j]['fg'] = fgcol[num.index(
                    int(a_mat[i - 1, j]))]

                m = i

            i += 1
    print('UP')
    root.after(timedelay, process)