python列表用copy方法复制修改复制列表原列表却也发生了变动,哪里出了问题?

m, n = 10, 10
world = [[0 for _ in range(0, m)] for _ in range(0, n)]
world[0][1], world[1][2], world[2][0], world[2][1], world[2][2] = 1, 1, 1, 1, 1

while True:
    _world = world.copy()
    for i in range(0, m):
        for j in range(0, n):
            state = 0
            if not (i == 0 or j == 0):
                state += world[i - 1][j - 1]
            if not i == 0:
                state += world[i - 1][j]
            if not (i == 0 or j == n - 1):
                state += world[i - 1][j + 1]
            if not j == 0:
                state += world[i][j - 1]
            if not j == n - 1:
                state += world[i][j + 1]
            if not (i == m - 1 or j == 0):
                state += world[i + 1][j - 1]
            if not i == m - 1:
                state += world[i + 1][j]
            if not (i == m - 1 or j == n - 1):
                state += world[i + 1][j + 1]

            if state < 2 or state > 3:
                    _world[i][j] = 0
            elif state == 3:
                    _world[i][j] = 1
            print(('\033[47m  \033[0m' if _world[i][j] else '  '), end='')
        print()
    world = _world.copy()
    input()
python
147 views
Comments
登录后评论
Sign In
·

·
# {生命游戏:对抗}
import time #暂时没有什么用
import numpy as np
import copy
import os
# import matplotlib.pyplot as plt:这里也没什么用
from random import randint

# [这里是游戏的基础规则函数:num是用于计算周围生命的数量]

def near_num(array,point):
    num = -array[point[0]][point[1]]
    around = [-1,1,0]
    for i in around:
        for j in around:
            if array[point[0]+i][point[1]+j] == 1:
                num += 1
            elif array[point[0]+i][point[1]+j] == -1:
                num -= 1 
    return num

# [这里是对矩阵代入值:game_map为矩阵中的值(-1,0,1)]

def refresh(game_map):
    copy_map = copy.copy(game_map)
    for x in range(copy_map.shape[0]-1):
        for y in range(copy_map.shape[1]-1):
            num=near_num(copy_map,[x,y])
            if num > 3 or num < -3:
                game_map[x][y] = 0
            elif num == 3:
                game_map[x][y] = 1
            elif num == 0 or num == 1 or num == -1:
                game_map[x][y] = 0
            elif num == -3:
                game_map[x][y] = -1
    return game_map

# [代换矩阵对应的对象:line将值转换为对应颜色的字符]

def output(game_map):
    for line in game_map:
        out_line=""
        for point in line:
            if point == 1:
                out_line += "□"
            elif point == -1:
                out_line += "■"
            else :
                out_line+="  "
        print(out_line)
    return "done"
'''
# [生成器集合]
# 子弹生成器
def bullet(a,b):
    bullet_x = [a,a,a,a,a,a,a+1,a+1,a+2,a+3,a+3,a+4,a+4]
    bullet_y = [b,b+1,b+2,b+3,b+4,b+5,b,b+6,b,b+1,b+6,b+3,b+4]
    for i in range(len(bullet_x)):
            game_map[bullet_x[i]][bullet_y[i]] = 1
    return game_map
# 震荡器生成器
def Oscillator(a,b):
    Oscillator_x = [a+9,a+9,a+10,a,a+1,a+4,a+5,a+10,a+11,a+20,a+21,a,a+1,a+4,a+5,a+10,a+11,a+12,a+20,a+21,a+4,a+5,a+10,a+11,a+9,a+10,a+9]
    Oscillator_y = [b,b+1,b+1,b+2,b+2,b+2,b+2,b+2,b+2,b+2,b+2,b+3,b+3,b+3,b+3,b+3,b+3,b+3,b+3,b+3,b+4,b+4,b+4,b+4,b+5,b+5,b+6]
    for i in range(len(Oscillator_x)):
        game_map[Oscillator_x[i]][Oscillator_y[i]] = -1
    return game_map
'''
# [随机生命的生成]

# (初始数量)
# 同矩阵对抗模型
'''
start_num = 5000
size = 150
#mid=int(size/2):好像没有什么用来着
#对矩阵大小进行设置
game_map_blue=np.zeros([size,size])
game_map_red=np.zeros([size,size])
start_point_x=np.random.randint(0,size,size=start_num)
start_point_y=np.random.randint(0,size,size=start_num)
for i in range(len(start_point_x)):
        game_map_blue[start_point_x[i]][start_point_y[i]] = 1
start_point_x=np.random.randint(0,size,size=start_num)
start_point_y=np.random.randint(0,size,size=start_num)
for j in range(len(start_point_x)):
        game_map_red[start_point_x[j]][start_point_y[j]] = -1
game_map = game_map_blue + game_map_red
'''
# 异矩阵对抗模型

start_num = 50
size = 12
game_map=np.zeros([size+size,size*2])
start_point_x=np.random.randint(0,size + size,size=start_num)
start_point_y=np.random.randint(0,size,size=start_num)
for i in range(len(start_point_x)):
        game_map[start_point_x[i]][start_point_y[i]] = 1
start_point_x=np.random.randint(0,size + size,size=start_num)
start_point_y=np.random.randint(0,size,size=start_num)
for i in range(len(start_point_x)):        
        game_map[start_point_x[i]][size + start_point_y[i]] = -1

'''
# 控制矩阵对抗模型 最大约为240
size = 150
game_map = np.zeros([size, size])
# 装填器
while True:
    axis = str.split(str(input('\033[1;30;34m请输入子弹坐标:\033[0m')),sep = ',')
    if int(axis[0]) != 0:
        x = int(axis[0])
        y = int(axis[1])
        game_map = bullet(x,y)
    else:
        break
axis = str.split(str(input('\033[1;30;31m请输入震荡器坐标:\033[0m')),sep = ',')
x = int(axis[0])
y = int(axis[1])
game_map = Oscillator(x,y)
'''
# [生命数量计算]
while True:
    time.sleep(0.2) #刷新时间设置,需导入import time
    # plt.colorbar(game_map,shrink=0.8,autoscale_None=None) 以后可能会用到的代码
    alive=0
    os.system("cls")
    output(game_map)
    for line in game_map:
        for point in line:
            if point == 1:
                alive += 1
            elif point == -1:
                alive -= 1
    print(alive)
    game_map = refresh(game_map)
·
import numpy as np
m,n = 5,5
world = np.zeros((m,n))
world[0][1], world[1][2], world[2][0], world[2][1], world[2][2] = 1, 1, 1, 1, 1
for day in range(1):
    _world = world.copy()
    print("_world的值为:\n",_world)
    for i in range(0, m):
        for j in range(0, n):
            state = 0
            if not (i == 0 or j == 0):
                state += world[i - 1][j - 1]
            if not i == 0:
                state += world[i - 1][j]
            if not (i == 0 or j == n - 1):
                state += world[i - 1][j + 1]
            if not j == 0:
                state += world[i][j - 1]
            if not j == n - 1:
                state += world[i][j + 1]
            if not (i == m - 1 or j == 0):
                state += world[i + 1][j - 1]
            if not i == m - 1:
                state += world[i + 1][j]
            if not (i == m - 1 or j == n - 1):
                state += world[i + 1][j + 1]

            if state < 2 or state > 3:
                    _world[i][j] = 0
            elif state == 3:
                    _world[i][j] = 1
            print("world的值为:\n",world)
    world = _world.copy()
    print("将_world拷贝后world的值为:\n",world)