鱼C论坛

 找回密码
 立即注册
查看: 1517|回复: 2

[已解决]conway的生命游戏

[复制链接]
发表于 2021-6-25 14:13:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import random, time, copy
  2. WIDTH = 60
  3. HEIGHT = 20

  4. nextCells = []
  5. for i in range(WIDTH):
  6.     column = []
  7.     for y in range(HEIGHT):
  8.         if random.randint(0, 1):
  9.             column.append('#')
  10.         else:
  11.             column.append(' ')
  12.     nextCells.append(column)

  13. while True:
  14.     print('\n\n\n\n\n')
  15.     currentCells = copy.deepcopy(nextCells)
  16.     for y in range(HEIGHT):
  17.         for x in range(WIDTH):
  18.             print(currentCells[x][y], end='')
  19.         print()

  20. for x in range(WIDTH):
  21.     for y in range(HEIGHT):
  22.         leftCoord = (x-1) % WIDTH
  23.         rightCoord = (x+1) % WIDTH
  24.         aboveCoord = (y-1) % HEIGHT
  25.         belowCoord = (y+1) % HEIGHT
  26.         numNeighbors = 0
  27.         if currentCells[leftCoord][aboveCoord] == '#':
  28.             numNeighbors += 1
  29.         if currentCells[x][aboveCoord] == '#':
  30.             numNeighbors += 1
  31.         if currentCells[rightCoord][aboveCoord] == '#':
  32.             numNeighbors += 1
  33.         if currentCells[leftCoord][y] == '#':
  34.             numNeighbors += 1
  35.         if currentCells[rightCoord][y] == '#':
  36.             numNeighbors += 1
  37.         if currentCells[leftCoord][belowCoord] == '#':
  38.             numNeighbors += 1
  39.         if currentCells[x][belowCoord] == '#':
  40.             numNeighbors += 1
  41.         if currentCells[rightCoord][belowCoord] == '#':
  42.             numNeighbors += 1

  43.         if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
  44.             nextCells[x][y] = '#'
  45.         elif currentCells[x][y] == ' ' and numNeighbors == 3:
  46.             nextCells[x][y] = '#'
  47.         else:
  48.             nextCells[x][y] = ''
  49.     time.sleep(1)
复制代码


这是一个小游戏。它在《python编程快速上手-让繁琐工作自动化(第2版)》中的第四章。
我按照书原样输入,结果却是错误的。我尝试了把while循环后的第二个嵌套for循环减少缩进,但是结果还是只输入了一副图。
什么原因呢?哪位前辈帮忙解决一下啊?谢谢
最佳答案
2021-6-25 14:23:20
你看看你要的是不是这个意思:
  1. import random, time, copy
  2. WIDTH = 60
  3. HEIGHT = 20

  4. while True:
  5.     nextCells = []
  6.     for i in range(WIDTH):
  7.         column = []
  8.         for y in range(HEIGHT):
  9.             if random.randint(0, 1):
  10.                 column.append('#')
  11.             else:
  12.                 column.append(' ')
  13.         nextCells.append(column)


  14.     print('\n\n\n\n\n')
  15.     currentCells = copy.deepcopy(nextCells)
  16.     for y in range(HEIGHT):
  17.         for x in range(WIDTH):
  18.             print(currentCells[x][y], end='')
  19.         print()

  20.     for x in range(WIDTH):
  21.         for y in range(HEIGHT):
  22.             leftCoord = (x-1) % WIDTH
  23.             rightCoord = (x+1) % WIDTH
  24.             aboveCoord = (y-1) % HEIGHT
  25.             belowCoord = (y+1) % HEIGHT
  26.             numNeighbors = 0
  27.             if currentCells[leftCoord][aboveCoord] == '#':
  28.                 numNeighbors += 1
  29.             if currentCells[x][aboveCoord] == '#':
  30.                 numNeighbors += 1
  31.             if currentCells[rightCoord][aboveCoord] == '#':
  32.                 numNeighbors += 1
  33.             if currentCells[leftCoord][y] == '#':
  34.                 numNeighbors += 1
  35.             if currentCells[rightCoord][y] == '#':
  36.                 numNeighbors += 1
  37.             if currentCells[leftCoord][belowCoord] == '#':
  38.                 numNeighbors += 1
  39.             if currentCells[x][belowCoord] == '#':
  40.                 numNeighbors += 1
  41.             if currentCells[rightCoord][belowCoord] == '#':
  42.                 numNeighbors += 1

  43.             if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
  44.                 nextCells[x][y] = '#'
  45.             elif currentCells[x][y] == ' ' and numNeighbors == 3:
  46.                 nextCells[x][y] = '#'
  47.             else:
  48.                 nextCells[x][y] = ''

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-25 14:23:20 | 显示全部楼层    本楼为最佳答案   
你看看你要的是不是这个意思:
  1. import random, time, copy
  2. WIDTH = 60
  3. HEIGHT = 20

  4. while True:
  5.     nextCells = []
  6.     for i in range(WIDTH):
  7.         column = []
  8.         for y in range(HEIGHT):
  9.             if random.randint(0, 1):
  10.                 column.append('#')
  11.             else:
  12.                 column.append(' ')
  13.         nextCells.append(column)


  14.     print('\n\n\n\n\n')
  15.     currentCells = copy.deepcopy(nextCells)
  16.     for y in range(HEIGHT):
  17.         for x in range(WIDTH):
  18.             print(currentCells[x][y], end='')
  19.         print()

  20.     for x in range(WIDTH):
  21.         for y in range(HEIGHT):
  22.             leftCoord = (x-1) % WIDTH
  23.             rightCoord = (x+1) % WIDTH
  24.             aboveCoord = (y-1) % HEIGHT
  25.             belowCoord = (y+1) % HEIGHT
  26.             numNeighbors = 0
  27.             if currentCells[leftCoord][aboveCoord] == '#':
  28.                 numNeighbors += 1
  29.             if currentCells[x][aboveCoord] == '#':
  30.                 numNeighbors += 1
  31.             if currentCells[rightCoord][aboveCoord] == '#':
  32.                 numNeighbors += 1
  33.             if currentCells[leftCoord][y] == '#':
  34.                 numNeighbors += 1
  35.             if currentCells[rightCoord][y] == '#':
  36.                 numNeighbors += 1
  37.             if currentCells[leftCoord][belowCoord] == '#':
  38.                 numNeighbors += 1
  39.             if currentCells[x][belowCoord] == '#':
  40.                 numNeighbors += 1
  41.             if currentCells[rightCoord][belowCoord] == '#':
  42.                 numNeighbors += 1

  43.             if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
  44.                 nextCells[x][y] = '#'
  45.             elif currentCells[x][y] == ' ' and numNeighbors == 3:
  46.                 nextCells[x][y] = '#'
  47.             else:
  48.                 nextCells[x][y] = ''

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-25 22:23:05 | 显示全部楼层
nahongyan1997 发表于 2021-6-25 14:23
你看看你要的是不是这个意思:

对的,谢谢啊。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 04:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表