赞
踩
import random class Cell: """ 细胞类,单个细胞 """ def __init__(self, ix, iy, is_live): self.ix = ix self.iy = iy self.is_live = is_live self.neighbour_count = 0 def __str__(self): return "[{},{},{:5}]".format(self.ix, self.iy, str(self.is_live)) # 计算周围活的邻居的个数,计算机中屏幕左上角的坐标为(0,0) def calc_neighbour_count(self): count = 0 pre_x = self.ix - 1 if self.ix > 0 else 0 # 某元胞左邻居的x值 for i in range(pre_x, self.ix+1+1): # 循环的x坐标范围是从某元胞的左邻居到右邻居 pre_y = self.iy - 1 if self.iy > 0 else 0 # 某元胞上邻居的y值 for j in range(pre_y, self.iy+1+1): # 循环的y坐标范围是从某元胞的上邻居到下邻居 # 该元胞是自己,continue,不统计count if i ==
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。