本帖最后由 explore 于 2023-4-22 15:50 编辑
就这样,运行都没问题,但是所有细胞都是死点?
[Python] 纯文本查看 复制代码
'''
Author:Explore
Version:Demo
p.s:10*10范围的版本
'''
#使用列表存储范围状态
#输入格式:
# 第一行:活点数量(int)
# 接下来每一行:活点坐标,格式为“x y”(中间为空格)
life_state=[[False]*10]*10#初始化宇宙(10*10)
#print(life_state)---测试
loops=int(input('初始活细胞数量,没有则填 0:'))#键入第一行数量
lived_cells_near_it_number=0#挨着该细胞的存活数量
if loops!=0:
for _ in range(loops):
x_y=input('点的坐标,以空格分割 x 与 y:').split()#点的坐标
x=int(x_y[0])-1
y=int(x_y[1])-1
life_state[x][y]=True#一个个置活
def get_environment(row,height):#得到从左上到右下的点的存活数量
global lived_cells_near_it_number
try:
if life_state[row-1][height-1]==True:
lived_cells_near_it_number+=1
if life_state[row-1][height]==True:
lived_cells_near_it_number+=1
if life_state[row-1][height+1]==True:
lived_cells_near_it_number+=1
if life_state[row][height-1]==True:
lived_cells_near_it_number+=1
if life_state[row][height+1]==True:
lived_cells_near_it_number+=1
if life_state[row+1][height-1]==True:
lived_cells_near_it_number+=1
if life_state[row+1][height]==True:
lived_cells_near_it_number+=1
if life_state[row+1][height+1]==True:
lived_cells_near_it_number+=1
except:
pass
def new_flash():#衍化新一代
global life_state,changed_life_state,lived_cells_near_it_number
changed_life_state=life_state#一份新宇宙拷贝
for row in range(len(life_state)):
for height in range(len(life_state[row])):
lived_cells_near_it_number=0
get_environment(row,height)
if (life_state[row][height]==False) and (lived_cells_near_it_number==3):
changed_life_state[row][height]=True#繁殖
elif (life_state[row][height]==True) and (lived_cells_near_it_number>=3):
changed_life_state[row][height]=False#过于拥挤
elif (life_state[row][height]==True) and (lived_cells_near_it_number<=2):
changed_life_state[row][height]=False#人口过少
else:
pass
life_state=changed_life_state
def output():
string=str(life_state)
string=string.replace('[[','')
string=string.replace(']]','')
string=string.replace('], [','\n')
string=string.replace('False','0')
string=string.replace('True','1')
print(string)
while input('Enter 下一帧,按 q 退出')!='q':
new_flash()
output()
p.s:我们这里中考信息要考简单的 Python ,但是 Office 却限定 2003 版本
|