python写的大型游戏-Python实现数据量较大的生命游戏

我现在要用串行的方法实现一个生命游戏,这个生命游戏是在一个100000 x 100000的棋盘上,并且繁衍500代。我选择用list形成一个二维数组来装这个棋盘上的数据,可是程序运行到一半,报错说memory error,如果数据量小一点的话运行时没问题的。请问我应该怎么改进呢

# 棋盘的行列数

row = 100000

column = 100000

# 进化代数

count = 50

nCellSta = [[random.randint(0, 1) for i in range(column)] for j in range(row)]

nTempSta = [[0 for p in range(column)] for q in range(row)]

def CellCount(nRow, nColumn):

global row, column, nCellSta

nSum = 0

for i in range(nRow - 1, nRow + 2):

for j in range(nColumn - 1, nColumn + 2):

if i < 0 or i > row - 1 or j < 0 or j > column - 1 or i == nRow and j == nColumn:

continue

if nCellSta[i][j] == 1:

nSum += 1

if nSum == 0 or nSum == 1 or nSum == 4 or nSum == 5 or nSum == 6 or nSum == 7 or nSum == 8:

return 0

elif nSum == 2:

return nCellSta[nRow][nColumn]

elif nSum == 3:

return 1

# 输出列表nCellSta的值

def printValue():

nSum = 0

global row, column

for i in range(row):

for j in range(column):

# 打印效果

# print(nCellSta[i][j], " ", end="")

nSum += nCellSta[i][j]

# print(" ")

return nSum

def main():

global count, nCellSta, nTempSta

printValue()

startTime = time.perf_counter()

for k in range(count - 1):

# print("新一轮游戏: ")

for i in range(row):

for j in range(column):

nTempSta[i][j] = CellCount(i, j)

nCellSta = copy.deepcopy(nTempSta)

if not printValue():

print("全部死亡,进化结束")

break

endTime = time.perf_counter()

seconds = endTime - startTime

m, s = divmod(seconds, 60)

h, m = divmod(m, 60)

print("花费时间为:%d:%02d:%.3f" % (h, m, s))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值