ライフゲーム
Posted feedbacks - Python
枠のセルの判定を簡単にするためにwidth+2, height+2の配列を用意して一番外側に0を入れています. 無限ループにしてますが# if t > 20:returnの部分を変更してやれば任意のステップで停止します. グライダーの実行結果 t=0 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [*] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [*] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [*] [*] [*] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] t=1 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [*] [ ] [*] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [*] [*] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [*] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] t=2 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [*] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [*] [ ] [*] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [*] [*] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #! -*- coding: utf-8 -*-
from random import random
width=10
height=10
def main(L1,L2,t):
print 't=%d'%t
for y in range(1,height+1):
for x in range(1,width+1):
s='[%s]'
if L1[y][x]==1:
s=s%'*'
else:
s=s%' '
print s,
print
for y in range(1,height+1):
for x in range(1,width+1):
cnt=0
for i in range(-1,2):
for j in range(-1,2):
if i==0 and j==0:continue
if L1[y+i][x+j]==1:cnt+=1
if L1[y][x]==0 and cnt==3:L2[y][x]=1
elif L1[y][x]==1 and 2 <= cnt <=3: L2[y][x]=1
else: L2[y][x]=0
# if t > 20:return
main(L2,L1,t+1)
if __name__=='__main__':
L1=[[0 for i in range(width+2)] for j in range(height+2)]
for i in range(1,height+1):
for j in range(1,width+1):
if random()<=0.3:
L1[i][j]=1
L2=[[0 for i in range(width+2)] for j in range(height+2)]
# 確認用 グライダー
# L1=[[0,0,0,0,0,0,0,0,0,0,0,0],
# [0,0,0,0,0,0,0,0,0,0,0,0],
# [0,0,0,0,0,0,0,0,0,0,0,0],
# [0,0,0,0,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,0,1,0,0,0,0,0,0,0,0],
# [0,0,0,1,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]]
main(L1,L2,0)
|
グライダーが動いているので大丈夫だと思います。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | import time
X_MAX = 9
Y_MAX = 9
def lefx(x,mx):
if (x - 1) < 0:
return mx
else:
return x - 1
def rigx(x,mx):
if (x + 1) > mx:
return 0
else:
return x + 1
def up_y(y,my):
if (y - 1) < 0:
return my
else:
return y - 1
def dowy(y,my):
if (y + 1) > my:
return 0
else:
return y + 1
def list_search(motherlist):
buflist = [[0] * len(motherlist) for i in range(len(motherlist))]
#range(9) = [0,1,2,3,4,5,6,7,8]
#range(9+1) = [0,1,2,3,4,5,6,7,8,9]
for x in range(X_MAX+1):
for y in range(Y_MAX+1):
if motherlist[lefx(x,X_MAX)][up_y(y,Y_MAX)] == 1:
buflist[x][y] += 1
if motherlist[lefx(x,X_MAX)][y] == 1:
buflist[x][y] += 1
if motherlist[lefx(x,X_MAX)][dowy(y,Y_MAX)] == 1:
buflist[x][y] += 1
if motherlist[x][up_y(y,Y_MAX)] == 1:
buflist[x][y] += 1
if motherlist[x][dowy(y,Y_MAX)] == 1:
buflist[x][y] += 1
if motherlist[rigx(x,X_MAX)][up_y(y,Y_MAX)] == 1:
buflist[x][y] += 1
if motherlist[rigx(x,X_MAX)][y] == 1:
buflist[x][y] += 1
if motherlist[rigx(x,X_MAX)][dowy(y,Y_MAX)] == 1:
buflist[x][y] += 1
return buflist
def list_weight(motherlist,buflist):
buflist2 = [[0]*len(motherlist) for i in range(len(motherlist))]
for x in range(X_MAX+1):
for y in range(Y_MAX+1):
if buflist[x][y] == 3 and motherlist[x][y] == 0:
buflist2[x][y] = 1
if buflist[x][y] == 2 and motherlist[x][y] == 1:
buflist2[x][y] = 1
if buflist[x][y] == 3 and motherlist[x][y] == 1:
buflist2[x][y] = 1
return buflist2
def main(motherlist,count,sleep):
while(count > 0):
for i in range(len(motherlist)):
print(motherlist[i])
print('\n')
motherlist = list_weight(motherlist,list_search(motherlist))
time.sleep(sleep)
count -= 1
if __name__ == '__main__':
testlist = [[0,0,0,0,0,0,0,0,0,0] for i in range(10)]
testlist[5][6] = 1
testlist[6][7] = 1
testlist[7][5] = 1
testlist[7][6] = 1
testlist[7][7] = 1
main(testlist,100,1)
|


saws
#5330()
Rating7/13=0.54
see: Wikipedia:ライフゲーム
[ reply ]