简体   繁体   English

在二维数组python中移动项目

[英]moving items in 2d array python

I'm trying to recreate the game 2048 as a project and i'm having trouble with manipulating the 2d array. 我正在尝试将游戏2048重新创建为项目,并且在处理2d数组时遇到了麻烦。 I'm trying to move the elements in the 2d array around and without them going off the screen. 我正在尝试移动2d数组中的元素,并且不让它们离开屏幕。

def move(self, direction):

    if (self.d == UP):
        for i in range(3):
            for j in range(3):
                if(self.line[i][j]!= self.line[0][0]):
                    self.line[i][j] = self.line[i + 1][j]
                    self.line[i + 1][j] = self.line[i + 2][j] 
                    self.line[i + 2][j] = self.line[i + 3][j]

this is what i currently have just for them to move up and they always seem to either disappear or fails. 这就是我目前为他们提供的服务,他们似乎总是消失或失败。

I made this might be what you are looking for, it is in JavaScript because I am not currently on my own computer but here it is: 我使这可能是您要查找的内容,它使用JavaScript,因为我当前不在自己的计算机上,但是在这里:

function up(tbl) {
    var x = 0;
    while (x < tbl[0].length) {
        do {
            var move = 0;
            var y = 0;
            while (y < tbl.length - 1) {
                if (tbl[y][x] == 0 && tbl[y + 1][x] != 0) {
                    tbl[y][x] = tbl[y + 1][x];
                    tbl[y + 1][x] = 0;
                    move = 1;
                }
                else if (tbl[y][x] != 0 && tbl[y][x] == tbl[y + 1][x]) {
                    tbl[y][x] += tbl[y + 1][x];
                    tbl[y + 1][x] = 0;
                    move = 1;
                }
                y++;
            }
        } while (move == 1)
        x++;
    }
    return tbl;
}

/* Just a testing table */
var t = [[1,   1,   0,   1],
         [0,   2,   2,   8],
         [0,   128, 1,   1],
         [2,   0,   1,   4]];

up(t);

What it does is go column by column, from the top to bottom, and checks: 它的作用是从上到下逐列进行检查,并检查:

  • if there is a gap, and the cell under it isn't, it moves the cell up 如果有缝隙,但下面的单元格没有,它将向上移动单元格
  • else if two cells are the same number, they are added up if none of 否则,如果两个单元格的数目相同,则如果没有
  • if after one loop (from top to bottom), neither of the above conditions took place, it moves to the next column 如果在一个循环(从上到下)之后,以上两种情况均未发生,则移至下一列

Also, in case you would like to apply this for the three other directions, I recommend rotating the table instead of doing 3 more of the above function. 另外,如果您想将此方向应用于其他三个方向,建议您旋转工作台,而不要再执行上述三个功能。 If you have trouble transcribing it to Python, I'll be able to help you later this evening. 如果您在将其转录到Python时遇到问题,今天晚上晚些时候我会为您提供帮助。

EDIT: 编辑:

Here you go, the same thing, but in Python: 同样,但是在Python中:

def up(tbl):
    x = 0
    while (x < len(tbl[0])):
        while True:
            move = 0
            y = 0
            while (y < (len(tbl) - 1)):
                if (tbl[y][x] == 0 and tbl[y + 1][x] != 0):
                    tbl[y][x] = tbl[y + 1][x]
                    tbl[y + 1][x] = 0
                    move = 1
                elif (tbl[y][x] != 0 and tbl[y][x] == tbl[y + 1][x]):
                    tbl[y][x] += tbl[y + 1][x]
                    tbl[y + 1][x] = 0
                    move = 1
                y = y + 1
            if (move == 0):
                break
        x = x + 1
    return tbl

t = [[1,   1,   0,   1],
     [0,   2,   2,   8],
     [0,   128, 1,   1],
     [2,   0,   1,   4]];

up(t)

print('\n'.join([''.join(['{:5}'.format(item) for item in row]) for row in t]))

Oh, and the original 2048 does not allow a tile to merge twice during the same turn, so you might want to tweak the elif to fit that, since it merges them until it cannot anymore. 哦,原始的2048不允许磁贴在同一回合中合并两次,因此您可能需要调整elif以使其适合,因为它将合并它们直到不能再合并为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM