鱼C论坛

 找回密码
 立即注册
查看: 1480|回复: 4

[已解决]37课后题有王八没饿死的吗= =

[复制链接]
发表于 2018-2-1 14:11:27 | 显示全部楼层 |阅读模式

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

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

x
好像王八的确容易饿死 ,还是圈小点好,养青蛙也不容易啊
  1. import random as r
  2. def move(pos,stepmax):
  3.     (x,y)=pos
  4.     direction=r.randint(0,4)
  5.     step=r.randint(1,stepmax)
  6.     if direction==0:
  7.         y+=step
  8.     elif direction==1:
  9.         y-=step
  10.     elif direction==2:
  11.         x-=step
  12.     else:
  13.         x+=step
  14.     if x<0:
  15.         x *=-1
  16.     if x>10:
  17.         x=20-x
  18.     if y<0:
  19.         y *= -1
  20.     if y>10:
  21.         y=20-y
  22.     return (x,y)

  23. class Turtle():
  24.     def __init__(self):
  25.         self.x=r.randint(0,10)
  26.         self.y=r.randint(0,10)
  27.         self.hp=100
  28.     def getpos(self):
  29.         return (self.x,self.y)
  30.     def setpos(self,pos):
  31.         self.x,self.y=pos
  32.         
  33. class Fish():
  34.     def __init__(self):
  35.         self.x=r.randint(0,10)
  36.         self.y=r.randint(0,10)
  37.         
  38. turtle=Turtle()
  39. fish=[]

  40. for i in range(10):
  41.     newfish=Fish()
  42.     fish.append((newfish.x,newfish.y))
  43. def Game():
  44.     count=10
  45.    
  46.     while turtle.hp>0 and count>0:
  47.         index=[]
  48.         for i in range(len(fish)):
  49.             if fish[i]==turtle.getpos():
  50.                 turtle.hp += 20
  51.                 print("王八hp+20,hp为",turtle.hp)
  52.                 count -= 1
  53.                 index.append(i)
  54.                 print(10-count,'鱼被吃')
  55.         sorted(index)
  56.         for each in index:
  57.             fish.pop(each)
  58.         turtle.setpos(move(turtle.getpos(),2))
  59.         turtle.hp -= 1
  60.         print('王八动了,hp-1,hp为',turtle.hp)
  61.         for each_fish in fish:
  62.             each_fish=move(each_fish,1)
  63.     print('GAME OVER!')
  64.     if turtle.hp==0:
  65.         print('王八是饿死的!')
  66.     else:
  67.         print('鱼都被吃完啦!')
  68. Game()
复制代码
最佳答案
2018-2-1 14:25:49
根据条件,基本都是饿死的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-1 14:14:52 | 显示全部楼层
课后题答案不允许hp超过100= =,还有题干的意思应该是只能x或y方向走,结果答案是还能斜着走
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-1 14:25:49 | 显示全部楼层    本楼为最佳答案   
根据条件,基本都是饿死的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-1 14:28:28 | 显示全部楼层
BngThea 发表于 2018-2-1 14:25
根据条件,基本都是饿死的

也是,粥越喝越稀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-9 16:00:54 | 显示全部楼层
1个王八10条鱼,王八奇迹般的赢了一盘
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-16 04:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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