我剛開始學習Python,並且正在努力爲康威的人生遊戲編寫一個程序。我試圖用邊界條件創建一個封閉的宇宙(這是對面/角落)。我想我已經完成了這個工作,但是我沒有在循環運行時迭代,也無法解決如何執行此操作。首先十分感謝!康威的生活:迭代Python中的封閉宇宙
def iterate_conway_closed(universe_t0, n_iter=1, display=False):
# This function is similar to your iterate_conway_open function
# but instead of adding a zero guard ring it should add a
# ‘wrapping ring’ as explained earlier. Use the same apply_rules
# function as part 1) to actually apply the rules once you have
# padded the universe.
# Note that unlike the always 0 guard ring for an open universe
# the wrapping ring will need updating after every call to
# apply rules to reflect changes in the universe
height, width=universe_t0.shape
universe_array=numpy.zeros((height+2, width+2), dtype=numpy.uint8)
universe_array[1:-1, 1:-1]=universe_t0
def count(n_iter):
n=0
while n<= n_iter:
yield n
n+=1
for n in range(0,n_iter):
universe_array[:1,1:-1]=universe_t0[-1:,:] # Maps the bottom row
universe_array[0,1:-1]=universe_t0[-1:,0:]# Maps the top row
universe_array[1:-1,0]=universe_t0[:,0]# Maps the left column
universe_array[1:-1,-1]=universe_t0[:,-1]# Maps the right column
universe_array[0,0]=universe_t0[-1,0]# Maps the bottom left corner
universe_array[0,-1]=universe_t0[-1,-1]# Maps the bottom right corner
universe_array[-1,0]=universe_t0[0,0]# Maps the top left corner
universe_array[-1,-1]=universe_t0[0,-1]# Maps the top right corner
for i in range(0, n_iter):
universe_array=apply_rules(universe_array)
if display==True:
b_print(universe_array)
return universe_array[1:-1, 1:-1]
我們需要更多的細節,以瞭解你遇到的問題是什麼。 – 2009-12-08 15:42:37
我只是不能解決爲什麼我的程序沒有迭代 - 它應該更新每個進化的邊界條件 – user227194 2009-12-08 22:06:30
看到我的修改答案爲可能的問題 – cobbal 2009-12-09 18:13:50