鱼C论坛

 找回密码
 立即注册
查看: 2077|回复: 11

[已解决]conway生命小游戏中%的问题

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

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

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

x
  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] = ''

复制代码


这个程序有点没搞明白,就是其中的%的用法那里,
leftCoord = (x-1) % WIDTH
rightCoord = (x+1) % WIDTH
aboveCoord = (y-1) % HEIGHT
belowCoord = (y+1) % HEIGHT
这几句话到底是啥意思啊?
哪位前辈给指点一下啊
最佳答案
2021-6-27 17:30:28
本帖最后由 nahongyan1997 于 2021-6-27 17:31 编辑
江湖散人 发表于 2021-6-27 16:44
不好意思啊,忘记和你说了,这是细胞自动机的一个例子。
#表示活的方块,' '表示死的方块。
如果一个活 ...


是为了防止索引的时候超界的, 举个例子:
(0,0)位置的元素左边的元素是 (-1,0),可是 -1 在列表中并不存在,直接索引肯定会出错,

但是 如果如 当前位置0 x % 左边一个的位置-1  = 0,所以这样就不会出现索引越界的情况了,


这样  0 % -1 = 0

右边同理,
已知最大宽度 60

60 右边是 61

60 % 61 = 60,

完美答案

给个最佳
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-26 07:36:08 | 显示全部楼层
% 的作用是求余数,比如

3 % 2 = 1   3 除以 2 等于 1 余 1
2 % 1 = 0   2 除以 1 等于 2 余 0

但是我得说句实话,
  1.    for x in range(WIDTH):
  2.         for y in range(HEIGHT):
  3.             leftCoord = (x-1) % WIDTH
  4.             rightCoord = (x+1) % WIDTH
  5.             aboveCoord = (y-1) % HEIGHT
  6.             belowCoord = (y+1) % HEIGHT
  7.             numNeighbors = 0
  8.             if currentCells[leftCoord][aboveCoord] == '#':
  9.                 numNeighbors += 1
  10.             if currentCells[x][aboveCoord] == '#':
  11.                 numNeighbors += 1
  12.             if currentCells[rightCoord][aboveCoord] == '#':
  13.                 numNeighbors += 1
  14.             if currentCells[leftCoord][y] == '#':
  15.                 numNeighbors += 1
  16.             if currentCells[rightCoord][y] == '#':
  17.                 numNeighbors += 1
  18.             if currentCells[leftCoord][belowCoord] == '#':
  19.                 numNeighbors += 1
  20.             if currentCells[x][belowCoord] == '#':
  21.                 numNeighbors += 1
  22.             if currentCells[rightCoord][belowCoord] == '#':
  23.                 numNeighbors += 1

  24.             if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
  25.                 nextCells[x][y] = '#'
  26.             elif currentCells[x][y] == ' ' and numNeighbors == 3:
  27.                 nextCells[x][y] = '#'
  28.             else:
  29.                 nextCells[x][y] = ''
复制代码

这段代码执行不执行对输出结果没有任何影响。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-26 10:55:27 | 显示全部楼层
nahongyan1997 发表于 2021-6-26 07:36
% 的作用是求余数,比如

3 % 2 = 1   3 除以 2 等于 1 余 1

书上说这是“取模环绕”技术 ,我搞不懂为啥要这么写。
听你这么一说,感觉你是比较了解这个程序的,能不能麻烦你帮我解答一下呢?
谢谢啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-27 08:39:48 | 显示全部楼层
江湖散人 发表于 2021-6-26 10:55
书上说这是“取模环绕”技术 ,我搞不懂为啥要这么写。
听你这么一说,感觉你是比较了解这个程序的,能 ...

书本上的东西还得结合实践使用啊,实践出真知吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-27 16:26:59 | 显示全部楼层
nahongyan1997 发表于 2021-6-27 08:39
书本上的东西还得结合实践使用啊,实践出真知吗


整个程序我知道是干什么的,但是具体到这里我就搞不懂了,这几个百分号语句是什么作用啊?能解释一下吗?谢谢啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-27 16:31:51 | 显示全部楼层
江湖散人 发表于 2021-6-27 16:26
整个程序我知道是干什么的,但是具体到这里我就搞不懂了,这几个百分号语句是什么作用啊?能解释一下吗 ...

我都不知道你的程序是干什么的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-27 16:44:09 | 显示全部楼层
nahongyan1997 发表于 2021-6-27 16:31
我都不知道你的程序是干什么的

不好意思啊,忘记和你说了,这是细胞自动机的一个例子。
#表示活的方块,' '表示死的方块。
如果一个活的方块与2个或3个活的方块为邻,那么下一步它还是活的。
如果一个死的方块正好有3个活的邻居,那么下一步它就是活的。
其他的方块在下一步会保持死亡或者活的。
这里每一个方块都有8个邻居,也就是8个方向,上下左右,左上,左下,右下,右上。
这个程序就是要表达这么个意思。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-27 17:30:28 | 显示全部楼层    本楼为最佳答案   
本帖最后由 nahongyan1997 于 2021-6-27 17:31 编辑
江湖散人 发表于 2021-6-27 16:44
不好意思啊,忘记和你说了,这是细胞自动机的一个例子。
#表示活的方块,' '表示死的方块。
如果一个活 ...


是为了防止索引的时候超界的, 举个例子:
(0,0)位置的元素左边的元素是 (-1,0),可是 -1 在列表中并不存在,直接索引肯定会出错,

但是 如果如 当前位置0 x % 左边一个的位置-1  = 0,所以这样就不会出现索引越界的情况了,


这样  0 % -1 = 0

右边同理,
已知最大宽度 60

60 右边是 61

60 % 61 = 60,

完美答案

给个最佳
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-27 17:47:18 | 显示全部楼层
这才是你想要的效果,
  1. import random, time, copy
  2. WIDTH = 50
  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.         
  21.     for x in range(WIDTH):
  22.         for y in range(HEIGHT):
  23.             leftCoord = (x-1) % WIDTH
  24.             rightCoord = (x+1) % WIDTH
  25.             aboveCoord = (y-1) % HEIGHT
  26.             belowCoord = (y+1) % HEIGHT
  27.             numNeighbors = 0
  28.             if currentCells[leftCoord][aboveCoord] == '#':
  29.                 numNeighbors += 1
  30.             if currentCells[x][aboveCoord] == '#':
  31.                 numNeighbors += 1
  32.             if currentCells[rightCoord][aboveCoord] == '#':
  33.                 numNeighbors += 1
  34.             if currentCells[leftCoord][y] == '#':
  35.                 numNeighbors += 1
  36.             if currentCells[rightCoord][y] == '#':
  37.                 numNeighbors += 1
  38.             if currentCells[leftCoord][belowCoord] == '#':
  39.                 numNeighbors += 1
  40.             if currentCells[x][belowCoord] == '#':
  41.                 numNeighbors += 1
  42.             if currentCells[rightCoord][belowCoord] == '#':
  43.                 numNeighbors += 1

  44.             if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
  45.                 nextCells[x][y] = '#'
  46.             elif currentCells[x][y] == ' ' and numNeighbors == 3:
  47.                 nextCells[x][y] = '#'
  48.             else:
  49.                 nextCells[x][y] = ' '   # 看看这里跟你的代码是不是不一样


复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-27 17:49:53 | 显示全部楼层
nahongyan1997 发表于 2021-6-27 17:30
是为了防止索引的时候超界的, 举个例子:
(0,0)位置的元素左边的元素是 (-1,0),可是 -1 在列表 ...

谢谢,终于搞明白了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-27 17:51:42 | 显示全部楼层
江湖散人 发表于 2021-6-27 17:49
谢谢,终于搞明白了。

对比一下我新给你改好的代码,看看是不是效果不一样了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-27 17:54:26 | 显示全部楼层
nahongyan1997 发表于 2021-6-27 17:51
对比一下我新给你改好的代码,看看是不是效果不一样了

对的,每个图都不一样 了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 06:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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