鱼C论坛

 找回密码
 立即注册
查看: 2027|回复: 7

[已解决]我这个乌龟吃鱼的程序不知道对不对啊

[复制链接]
发表于 2020-7-12 22:55:04 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Twilight6 于 2020-7-12 23:46 编辑
  1. import random


  2. class tortoise:
  3.     direction_list = ["x","y"]
  4.     current_location = {"x":0,"y":0}
  5.     value = {"x":1,"y":1}
  6.    
  7.     def move(self):
  8.         run = [self.direction_list[random.randint(0,1)],random.randint(1,2)]
  9.         if 10<self.current_location[run[0]] or self.current_location[run[0]]<0 :
  10.             self.value[run[0]] *= -1
  11.         self.current_location[run[0]] = self.current_location[run[0]] + run[1] * self.value[run[0]]
  12.         return self.current_location

  13. class fishc:
  14.     fishc_list = ["x","y"]
  15.     current_location = {"x":0,"y":0}
  16.     value = {"x":1,"y":1}
  17.    
  18.     def move(self):
  19.         run = [self.fishc_list[random.randint(0,1)],1]
  20.         if 10<self.current_location[run[0]] or self.current_location[run[0]]<0 :
  21.             self.value[run[0]] *= -1
  22.         self.current_location[run[0]] = self.current_location[run[0]] + run[1] * self.value[run[0]]
  23.         return self.current_location


  24. t = tortoise()


  25. yu = list(range(10))
  26. life_fishc = list(range(10))
  27. f = list(range(10))

  28. value = 100
  29. l = 0

  30. gui = {}

  31. for i in range(len(yu)):
  32.     yu[i] = 1
  33.     life_fishc[i] = fishc()
  34.    
  35. while value > 0 and l<8 :
  36.     if value<100 :
  37.         gui = t.move()
  38.     for i in range(len(yu)):
  39.         if yu[i] :
  40.             f[i] = life_fishc[i].move()
  41.         if f[i] == gui and yu[i] != 0:
  42.             l += 1
  43.             yu[i] = 0
  44.             value += 20
  45.             print("第%d条鱼被吃了"%i)
  46.     value -= 1

  47. print("有%d条鱼被吃了"%l)
复制代码

        
最佳答案
2020-7-12 23:58:14



你定义的属性都错了,你这里:
class tortoise:
    direction_list = ["x","y"]
    current_location = {"x":0,"y":0}
    value = {"x":1,"y":1}


和这里:

class fishc:
    fishc_list = ["x","y"]
    current_location = {"x":0,"y":0}
    value = {"x":1,"y":1}


定义的都是类属性,而类属性是所有该类实例对象共享的,所以你创建再多的鱼,都是同个坐标出现的,你应该用 __init__方法来初始化位置坐标,配合 random 达到随机效果

其他的错误没怎么看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-12 22:57:53 | 显示全部楼层
哪位大哥能帮我看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-12 23:02:47 | 显示全部楼层
初探门槛 发表于 2020-7-12 22:57
哪位大哥能帮我看看


你是不是发错类型了,要不要帮转问题求助?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-12 23:35:13 | 显示全部楼层
Twilight6 发表于 2020-7-12 23:02
你是不是发错类型了,要不要帮转问题求助?

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

使用道具 举报

发表于 2020-7-12 23:45:00 | 显示全部楼层
本帖最后由 Twilight6 于 2020-7-12 23:59 编辑




代码中的
  1. [i]
复制代码
被编辑器吃掉了,要这样发才不会没掉:

https://fishc.com.cn/thread-52272-1-1.html

我已经帮你重新编辑了


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

使用道具 举报

发表于 2020-7-12 23:58:14 | 显示全部楼层    本楼为最佳答案   



你定义的属性都错了,你这里:
class tortoise:
    direction_list = ["x","y"]
    current_location = {"x":0,"y":0}
    value = {"x":1,"y":1}


和这里:

class fishc:
    fishc_list = ["x","y"]
    current_location = {"x":0,"y":0}
    value = {"x":1,"y":1}


定义的都是类属性,而类属性是所有该类实例对象共享的,所以你创建再多的鱼,都是同个坐标出现的,你应该用 __init__方法来初始化位置坐标,配合 random 达到随机效果

其他的错误没怎么看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-13 09:54:50 | 显示全部楼层
Twilight6 发表于 2020-7-12 23:58
你定义的属性都错了,你这里:

为什么不写在init里就是公用的,我以为每个实例对象都是独立的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-13 09:55:52 | 显示全部楼层
初探门槛 发表于 2020-7-13 09:54
为什么不写在init里就是公用的,我以为每个实例对象都是独立的



实例属性都是独立的,而类属性是类的,这一类全部都共有的

方法里的变量前加 self.  就是实例属性,类属性是在类内,方法外的属性
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-23 11:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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