首页 > 解决方案 > 检查矩阵中其他值的对角线值的问题。(Python)

问题描述

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

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

标签: python

解决方案


问题是,在这一行

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

推荐阅读