检查矩阵中其他值的对角线的问题。 (蟒蛇)

时间:2019-02-22 02:10:57

标签: python

所以我和我的朋友试图用python重新创建Conway的生活游戏,但是遇到了一个问题,即试图检查我们的单元矩阵中对角相邻的值。我们的代码寻找与所讨论的值成对角线的值,但由于某种原因,它似乎无法找到它们。例如,一个具有3个相邻像元(相邻2个,对角线1个)的像元将作为2个相邻像元返回。 为了进行调试,我们列出了所有活细胞的近邻及其邻居计数。 这是我们的代码:

initial_frame = [
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

next_frame = [
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

row = []

neighborcount = 0

next_frame = initial_frame

while True:

    for e in range(1, 10):

        for a in range(1, 10):

            row.append(initial_frame[e][a])

        print(row)

        row = []

    print("\n\n\n\n")

    input()

    for i in range(1, 10):

        for o in range(1, 10):

            neighborcount = 0

            #Down 1

            if initial_frame[(o + 1)][i] == 1:

                neighborcount += 1



            #Up 1

            if initial_frame[(o - 1)][i] == 1:

                neighborcount += 1



            #Right 1

            if initial_frame[o][(i + 1)] == 1:

                neighborcount += 1



            #Left 1

            if initial_frame[o][(i - 1)] == 1:

                neighborcount += 1



            #Down 1, Right 1

            if initial_frame[(o + 1)][(i + 1)] == 1:

                neighborcount += 1



            #Down 1, Left 1

            if initial_frame[(o + 1)][(i - 1)] == 1:

                neighborcount += 1



            #Up 1, Left 1

            if initial_frame[(o - 1)][(i - 1)] == 1:

                neighborcount += 1



            #Up 1, Right 1

            if initial_frame[(o - 1)][(i + 1)] == 1:

                neighborcount += 1



            #If dead cell has exactly 3 neighbors, set it to be born

            if initial_frame[o][i] == 0 and neighborcount == 3:

                next_frame[o][i] = 1



            #If living cell:

            if initial_frame[o][i] == 1:

                #does not have either 2 or 3 neighbors, set it to die

                if neighborcount != 2 and neighborcount != 3:

                    next_frame[o][i] = 0

                print(str(o) + ", " + str(i) + ": " + str(neighborcount))

            #reset neighbors

            neighborcount = 0

    #Project set values onto real board

    initial_frame = next_frame

2 个答案:

答案 0 :(得分:0)

一种避免相邻坐标到达边的问题的方法是在矩阵的每一侧填充零行和零列。这样一来,您就可以使用偏移量列表来访问8个相邻值,而无需进行任何边界检查。

[编辑]我刚刚注意到您已经填充了矩阵。 (我相应地调整了示例)。似乎问题之一是您使用的范围(1,10)仅会通过1..9,但您需要通过1..10,因此它们应该是范围(1,11)

例如:

next_Frame  = [ line.copy() for line in initial_frame ]
offsets     = [ (-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1) ]
for row in range(1,11):
    for col in range(1,11):
        neighbors = sum([ initial_frame[row+r][col+c] for r,c in offsets ])
        if next_Frame[row][col] == 0 and neighbors == 3:
            next_Frame[row][col] = 1 
        elif next_Frame[row][col] == 1 and neighbors not in [2,3] 
            next_Frame[row][col] = 0

答案 1 :(得分:0)

问题是,在这一行

next_frame = initial_frame

Python实际上并不复制整个数组。 next_frame刚开始引用initial_frame所引用的内容,因此next_frame is initial_frame将返回true。

这可以通过在生成计算结束时交换数组来解决。像这样:

@@ -30,8 +30,6 @@ row = []

neighborcount = 0

-next_frame = initial_frame
-
while True:

    for e in range(1, 10):
@@ -120,6 +118,8 @@ while True:

            #If dead cell has exactly 3 neighbors, set it to be born

+            next_frame[o][i] = initial_frame[o][i]
+
            if initial_frame[o][i] == 0 and neighborcount == 3:

                next_frame[o][i] = 1
@@ -143,5 +143,6 @@ while True:
            neighborcount = 0

    #Project set values onto real board
-
+    garbage_arr = initial_frame
    initial_frame = next_frame
+    next_frame = garbage_arr

结果代码:

initial_frame = [
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

next_frame = [
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

row = []

neighborcount = 0

while True:

    for e in range(1, 10):

        for a in range(1, 10):

            row.append(initial_frame[e][a])

        print(row)

        row = []

    print("\n\n\n\n")

    input()

    for i in range(1, 10):

        for o in range(1, 10):

            neighborcount = 0

            #Down 1

            if initial_frame[(o + 1)][i] == 1:

                neighborcount += 1



            #Up 1

            if initial_frame[(o - 1)][i] == 1:

                neighborcount += 1



            #Right 1

            if initial_frame[o][(i + 1)] == 1:

                neighborcount += 1



            #Left 1

            if initial_frame[o][(i - 1)] == 1:

                neighborcount += 1



            #Down 1, Right 1

            if initial_frame[(o + 1)][(i + 1)] == 1:

                neighborcount += 1



            #Down 1, Left 1

            if initial_frame[(o + 1)][(i - 1)] == 1:

                neighborcount += 1



            #Up 1, Left 1

            if initial_frame[(o - 1)][(i - 1)] == 1:

                neighborcount += 1



            #Up 1, Right 1

            if initial_frame[(o - 1)][(i + 1)] == 1:

                neighborcount += 1



            #If dead cell has exactly 3 neighbors, set it to be born

            next_frame[o][i] = initial_frame[o][i]

            if initial_frame[o][i] == 0 and neighborcount == 3:

                next_frame[o][i] = 1



            #If living cell:

            if initial_frame[o][i] == 1:

                #does not have either 2 or 3 neighbors, set it to die

                if neighborcount != 2 and neighborcount != 3:

                    next_frame[o][i] = 0

                print(str(o) + ", " + str(i) + ": " + str(neighborcount))

            #reset neighbors

            neighborcount = 0

    #Project set values onto real board
    garbage_arr = initial_frame
    initial_frame = next_frame
    next_frame = garbage_arr