鱼C论坛

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

037 类和对象课后题王八吃小鱼

[复制链接]
发表于 2018-2-23 19:09:02 | 显示全部楼层 |阅读模式

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

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

x
我仿写了小甲鱼的代码,但是运行结果不是一下全吃完小鱼游戏通过,就是一条鱼也没吃掉直接失败,偶尔吃掉几条小鱼之后游戏失败。而在修改了一个类中的函数调用之后就恢复正常了。


最初的代码:
  1. import random as r

  2. limit_x = [0,10]
  3. limit_y = [0,10]
  4. mo = [1,2,-1,-2]
  5. class wugui:
  6.     def __init__(self):
  7.         self.power = 100
  8.         self.x = r.randint(limit_x[0],limit_x[1])
  9.         self.y = r.randint(limit_y[0],limit_y[1])
  10.       
  11.     def move(self):
  12.         new_x = self.x + r.choice([1,2,-1,-2])
  13.         new_y = self.y + r.choice([1,2,-1,-2])
  14.         if new_x < limit_x[0]:
  15.             self.x = limit_x[0] - (new_x - limit_x[0])
  16.         elif new_x > limit_x[1]:
  17.             self.x = limit_x[1] - (new_x - limit_x[1])
  18.         else:
  19.             self.x = new_x
  20.         if new_y < limit_y[0]:
  21.             self.y = limit_y[0] - (new_y - limit_y[0])
  22.         elif new_y > limit_y[1]:
  23.             self.y = limit_y[1] - (new_y - limit_y[1])
  24.         else:
  25.             self.y = new_y
  26.         self.power -= 1
  27.         return (self.x,self.y)
  28.     def eat(self):
  29.         self.power += 20
  30.         if self.power > 100:
  31.             self.power = 100

  32. class xiaoyu:
  33.     def __init__(self):
  34.         self.x = r.randint(limit_x[0],limit_x[1])
  35.         self.y = r.randint(limit_y[0],limit_y[1])
  36.       
  37.     def move(self):
  38.         new_x = self.x + r.choice([1,-1])
  39.         new_y = self.y + r.choice([1,-1])
  40.         if new_x < limit_x[0]:
  41.             self.x = limit_x[0] - (new_x - limit_x[0])
  42.         elif new_x > limit_x[1]:
  43.             self.x = limit_x[1] - (new_x - limit_x[1])
  44.         else:
  45.             self.x = new_x
  46.         if new_y < limit_y[0]:
  47.             self.y = limit_y[0] - (new_y - limit_y[0])
  48.         elif new_y > limit_y[1]:
  49.             self.y = limit_y[1] - (new_y - limit_y[1])
  50.         else:
  51.             self.y = new_y
  52.         return (self.x,self.y)

  53. wu = wugui()

  54. yuer = []
  55. for i in range(10):
  56.     yu = xiaoyu()
  57.     yuer.append(yu)

  58. while True:
  59.     if not len(yuer):
  60.         print('鱼儿被吃完了,游戏结束!')
  61.         break
  62.     if not wu.power:
  63.         print('小乌龟体力耗尽,游戏结束!')
  64.         break

  65.     for eachfish in yuer[:]:
  66.         if eachfish.move() == wu.move():
  67.             
  68.             wu.eat()
  69.             yuer.remove(eachfish)
  70.             print('有一条鱼儿被吃掉了!')
复制代码


修改后正常的代码,区别在倒数第四行

  1. import random as r

  2. limit_x = [0,10]
  3. limit_y = [0,10]
  4. mo = [1,2,-1,-2]
  5. class wugui:
  6.     def __init__(self):
  7.         self.power = 100
  8.         self.x = r.randint(limit_x[0],limit_x[1])
  9.         self.y = r.randint(limit_y[0],limit_y[1])
  10.       
  11.     def move(self):
  12.         new_x = self.x + r.choice([1,2,-1,-2])
  13.         new_y = self.y + r.choice([1,2,-1,-2])
  14.         if new_x < limit_x[0]:
  15.             self.x = limit_x[0] - (new_x - limit_x[0])
  16.         elif new_x > limit_x[1]:
  17.             self.x = limit_x[1] - (new_x - limit_x[1])
  18.         else:
  19.             self.x = new_x
  20.         if new_y < limit_y[0]:
  21.             self.y = limit_y[0] - (new_y - limit_y[0])
  22.         elif new_y > limit_y[1]:
  23.             self.y = limit_y[1] - (new_y - limit_y[1])
  24.         else:
  25.             self.y = new_y
  26.         self.power -= 1
  27.         return (self.x,self.y)
  28.     def eat(self):
  29.         self.power += 20
  30.         if self.power > 100:
  31.             self.power = 100

  32. class xiaoyu:
  33.     def __init__(self):
  34.         self.x = r.randint(limit_x[0],limit_x[1])
  35.         self.y = r.randint(limit_y[0],limit_y[1])
  36.       
  37.     def move(self):
  38.         new_x = self.x + r.choice([1,-1])
  39.         new_y = self.y + r.choice([1,-1])
  40.         if new_x < limit_x[0]:
  41.             self.x = limit_x[0] - (new_x - limit_x[0])
  42.         elif new_x > limit_x[1]:
  43.             self.x = limit_x[1] - (new_x - limit_x[1])
  44.         else:
  45.             self.x = new_x
  46.         if new_y < limit_y[0]:
  47.             self.y = limit_y[0] - (new_y - limit_y[0])
  48.         elif new_y > limit_y[1]:
  49.             self.y = limit_y[1] - (new_y - limit_y[1])
  50.         else:
  51.             self.y = new_y
  52.         return (self.x,self.y)

  53. wu = wugui()

  54. yuer = []
  55. for i in range(10):
  56.     yu = xiaoyu()
  57.     yuer.append(yu)

  58. while True:
  59.     if not len(yuer):
  60.         print('鱼儿被吃完了,游戏结束!')
  61.         break
  62.     if not wu.power:
  63.         print('小乌龟体力耗尽,游戏结束!')
  64.         break
  65.     there = wu.move()
  66.     for eachfish in yuer[:]:
  67.         if eachfish.move() == there:
  68.             
  69.             wu.eat()
  70.             yuer.remove(eachfish)
  71.             print('有一条鱼儿被吃掉了!')
复制代码

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

使用道具 举报

发表于 2018-2-23 19:13:39 | 显示全部楼层
1只乌龟10条鱼那必须是乌龟死,1乌龟赢的机率百万分之一都没有。
下面我修改加了个乌龟,2只乌龟赢的机率高一些,3只乌龟基本都赢。
  1. import random as rd

  2. class Turtle:
  3.         turtle_num = 0
  4.         def __init__(self):
  5.                 self.turtle_pos = [rd.randint(0,10),rd.randint(0,10)]
  6.                 self.turtle_life = 100
  7.                 Turtle.turtle_num += 1
  8.        
  9.         def Turtle_move(self):
  10.                 direction = rd.choice(['x','y'])
  11.                 if direction == 'x':
  12.                         self.turtle_pos[0] = self.turtle_pos[0] + rd.choice([-2,-1,1,2])
  13.                         if self.turtle_pos[0] < 0:
  14.                                 self.turtle_pos[0] = self.turtle_pos[0] + 10
  15.                         elif self.turtle_pos[0] > 10:
  16.                                 self.turtle_pos[0] = self.turtle_pos[0] - 10
  17.                 else:
  18.                         self.turtle_pos[1] = self.turtle_pos[1] + rd.choice([-2,-1,1,2])
  19.                         if self.turtle_pos[1] < 0:
  20.                                 self.turtle_pos[1] = self.turtle_pos[0] + 10
  21.                         elif self.turtle_pos[1] > 10:
  22.                                 self.turtle_pos[1] = self.turtle_pos[0] - 10
  23.                 self.turtle_life -= 1
  24.                 if self.turtle_life <= -1:
  25.                         self.Die()
  26.                 return self.turtle_pos
  27.                
  28.         def Turtle_eat(self):
  29.                 self.turtle_life += 20
  30.                 if self.turtle_life > 100:
  31.                         self.turtle_life = 100
  32.         def Die(self):
  33.                 Turtle.turtle_num -= 1
  34.                
  35. class Fish:
  36.         fish_num = 0
  37.         def __init__(self):
  38.                 self.fish_pos = [rd.randint(0,10),rd.randint(0,10)]
  39.                 Fish.fish_num += 1
  40.                
  41.         def Fish_move(self):
  42.                 direction = rd.choice(['x','y'])
  43.                 if direction == 'x':
  44.                         self.fish_pos[0] = self.fish_pos[0] + rd.choice([-1,1])
  45.                         if self.fish_pos[0] < 0:
  46.                                 self.fish_pos[0] = self.fish_pos[0] + 10
  47.                         elif self.fish_pos[0] > 10:
  48.                                 self.fish_pos[0] = self.fish_pos[0] - 10
  49.                 else:
  50.                         self.fish_pos[1] = self.fish_pos[1] + rd.choice([-1,1])
  51.                         if self.fish_pos[1] < 0:
  52.                                 self.fish_pos[1] = self.fish_pos[0] + 10
  53.                         elif self.fish_pos[1] > 10:
  54.                                 self.fish_pos[1] = self.fish_pos[0] - 10
  55.                 return self.fish_pos
  56.         def Die(self):
  57.                 Fish.fish_num -= 1


  58. turtle = []
  59. fish = []
  60. for j in range(2):
  61.         new_turtle = Turtle()
  62.         turtle.append(new_turtle)
  63. for i in range(100):
  64.         new_fish = Fish()
  65.         fish.append(new_fish)

  66. round_n = 0

  67. while True:
  68.         round_n += 1
  69.         if not Fish.fish_num:
  70.                 print('鱼吃完啦!')
  71.                 break
  72.         if not Turtle.turtle_num:
  73.                 print('乌龟挂完啦!')
  74.                 break
  75.         for each_turtle in turtle[:]:
  76.                 pos = each_turtle.Turtle_move()
  77.                 if each_turtle.turtle_life == -1:
  78.                         turtle.remove(each_turtle)
  79.                         print('第%d回合, 挂了一只乌龟, 还剩%d只乌龟' % (round_n, Turtle.turtle_num))
  80.                 for each_fish in fish[:]:
  81.                         if each_fish.Fish_move() == pos:
  82.                                 each_turtle.Turtle_eat()
  83.                                 fish.remove(each_fish)
  84.                                 Fish.fish_num -= 1
  85.                                 print('第%d回合, 有一条鱼被吃掉了~还剩%d条鱼, %d只乌龟' % (round_n, Fish.fish_num, Turtle.turtle_num))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-23 23:46:23 | 显示全部楼层
there = wu.move()   #乌龟位置已确定
    for eachfish in yuer[:]:   #小鱼位置遍历时乌龟位置不变,相当于10条鱼对应一个乌龟
        if eachfish.move() == there:
            
            wu.eat()
            yuer.remove(eachfish)
            print('有一条鱼儿被吃掉了!')
下面的代码
for eachfish in yuer[:]:   #遍历小鱼位置
        if eachfish.move() == wu.move():   #for循环里,每遍历一次小鱼位置,就生成一个乌龟位置,相当于十条小鱼对应十条乌龟
            
            wu.eat()
            yuer.remove(eachfish)
            print('有一条鱼儿被吃掉了!')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 16:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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