Python 2048游戏

2024-07-05 14:55:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个项目,我要做一个2048游戏。在

现在,我在函数上,如果两个数相等,求和,如果两个数之间有0,它也应该把它们拉上来。在

基本上我就是这么做的:

'''tab creates the 2048 game board'''
def tab():
    return ({
        (1, 1): 0, (1, 2): 0, (1, 3): 0, (1, 4): 0,
        (2, 1): 0, (2, 2): 0, (2, 3): 0, (2, 4): 0,
        (3, 1): 0, (3, 2): 0, (3, 3): 0, (3, 4): 0,
        (4, 1): 0, (4, 2): 0, (4, 3): 0, (4, 4): 0
        })

#tab_reduc(t,d) gets a board(t) and a place to move as a string ('N','S','E','W') and moves the board as ordered
def tab_reduc(t,d):
    for i in range(1,4):
        for c in range(1,4):
            if t[(i,c)] == t[(i,c+1)] and t[(i,c+1)] != 0:
                t[(i,c)] = t[(i,c)] + t[(i,c+1)]
                t[(i,c+1)] = 0

            elif t[(i,c)] != t[(i,c+1)] and t[(i,c)] != 0:
                t[(i,c)] = t[(i,c)]
                t[(i,c+1)] = t[(i,c+1)]

            elif t[(i,c)] == 0 and t[(i,c+1)] != 0:
                t[(i,c)] = t[(i,c+1)]
                t[(i,c+1)] = 0

    return t

例如,如果我有:

^{pr2}$

当我运行“tab_reduce(t,'N')”时,游戏应该开始运行,我确实得到了

(1,1) = 8
(1,2) = 8
(1,3) = 4
(1,4) = 0

但如果我有

(1,1) = 4
(1,2) = 0
(1,3) = 0
(1,4) = 4

我演北方戏,我明白

(1,1) = 4
(1,2) = 0
(1,3) = 4
(1,4) = 0

如果我再做一次,我会得到:

(1,1) = 4
(1,2) = 4
(1,3) = 0
(1,4) = 0

再说一遍:

(1,1) = 8
(1,2) = 0
(1,3) = 0
(1,4) = 0

问题是,这应该在一个重头戏中完成,而不是几出戏。在

有人能帮我吗?在


Tags: andthe项目函数inboard游戏for
1条回答
网友
1楼 · 发布于 2024-07-05 14:55:47

想法…

移动N时,可能需要向后迭代。在

for i in range(1, 4):
    for c in reversed(range(1, 4)):
        ...

因此…

如果你这么做,你就会离你的解决方案更近了。但是,在这种情况下仍然会有一个bug:

^{pr2}$

改进后的代码将生成:

8 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

当它应该产生:

4 0 0 0
4 0 0 0
0 0 0 0
0 0 0 0

我将把解决这个问题留给读者练习。我想,解决这个问题最简单的方法是在成功地进行积累时中断迭代。在

还有一些最后的想法:

如果我自己来实现这一点,我会使用稍微不同的策略。这里的关键是把问题分解成步骤。我们继续只谈北方。在

步骤:

  1. 让我们删除数字上方的所有0。也就是说,让我们把桌子向北展平。所以这个:

    0 0 0 0             1 1 0 1
    1 0 0 0  Becomes..  0 0 0 1
    0 1 0 1             0 0 0 0
    0 0 0 1             0 0 0 0
    
  2. 接下来,让我们看看邻居,如果可以的话合并它们。所以:

    0 0 0 1             0 0 0 2
    0 0 0 1  Becomes..  0 0 0 0
    0 0 0 1             0 0 0 1
    0 0 0 0             0 0 0 0
    
  3. 第三步是再次删除0。我们需要这样做,因为我们在上一步中插入了一些额外的。在

    0 0 0 1             0 0 0 2
    0 0 0 1  Becomes..  0 0 0 1
    0 0 0 1             0 0 0 0
    0 0 0 0             0 0 0 0
    

现在。我们如何在代码中实现这样的东西?在

def tab():
    return ({
        (1, 1): 0, (1, 2): 0, (1, 3): 0, (1, 4): 0,
        (2, 1): 0, (2, 2): 0, (2, 3): 0, (2, 4): 0,
        (3, 1): 0, (3, 2): 0, (3, 3): 0, (3, 4): 0,
        (4, 1): 0, (4, 2): 0, (4, 3): 0, (4, 4): 0
        })

def tab_reduc(t,d):
    if d in ('n', 'N'):
        # Merge whitespace
        for _ in range(4): # FIXME: Massive hack...
            for i in range(1,4):
                for c in range(1,5):
                    if t[i+0, c] == 0 and t[i+1, c] != 0:
                        t[i+0, c] = t[i+1, c]
                        t[i+1, c] = 0

        # Merge neighbors
        for i in reversed(range(1, 4)):
            for c in range(1,5):
                if t[i+0, c] == t[i+1, c] and t[i, c] != 0:
                    t[i+0, c] *= 2
                    t[i+1, c] = 0

        # Merge whitespace
        for _ in range(4): # FIXME: Massive hack
            for i in range(1,4):
                for c in range(1,5):
                    if t[i+0, c] == 0 and t[i+1, c] != 0:
                        t[i+0, c] = t[i+1, c]
                        t[i+1, c] = 0

def tab_print(t):
    for i in range(1, 5):
        for c in range(1, 5):
            print '{:2d}'.format(t[i,c]),
        print
    print

t = tab()
t[(1,4)] = 2
t[(2,4)] = 2
t[(3,4)] = 2
t[(4,4)] = 2
tab_print(t)
tab_reduc(t, 'N')
tab_print(t)

运行时输出:

 0  0  0  2
 0  0  0  2
 0  0  0  2
 0  0  0  2

 0  0  0  4
 0  0  0  4
 0  0  0  0
 0  0  0  0

我为了自己的理智改变了一些内在的东西。在这段代码中,(i, j)向下是i-1行,右边是j-1列。在

相关问题 更多 >