鱼C论坛

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

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

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

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

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

x
好像王八的确容易饿死 ,还是圈小点好,养青蛙也不容易啊
import random as r
def move(pos,stepmax):
    (x,y)=pos
    direction=r.randint(0,4)
    step=r.randint(1,stepmax)
    if direction==0:
        y+=step
    elif direction==1:
        y-=step
    elif direction==2:
        x-=step
    else:
        x+=step
    if x<0:
        x *=-1
    if x>10:
        x=20-x
    if y<0:
        y *= -1
    if y>10:
        y=20-y
    return (x,y)

class Turtle():
    def __init__(self):
        self.x=r.randint(0,10)
        self.y=r.randint(0,10)
        self.hp=100
    def getpos(self):
        return (self.x,self.y)
    def setpos(self,pos):
        self.x,self.y=pos
        
class Fish():
    def __init__(self):
        self.x=r.randint(0,10)
        self.y=r.randint(0,10)
        
turtle=Turtle()
fish=[]

for i in range(10):
    newfish=Fish()
    fish.append((newfish.x,newfish.y))
def Game():
    count=10
    
    while turtle.hp>0 and count>0:
        index=[]
        for i in range(len(fish)):
            if fish[i]==turtle.getpos():
                turtle.hp += 20
                print("王八hp+20,hp为",turtle.hp)
                count -= 1
                index.append(i)
                print(10-count,'鱼被吃')
        sorted(index)
        for each in index:
            fish.pop(each)
        turtle.setpos(move(turtle.getpos(),2))
        turtle.hp -= 1
        print('王八动了,hp-1,hp为',turtle.hp)
        for each_fish in fish:
            each_fish=move(each_fish,1)
    print('GAME OVER!')
    if turtle.hp==0:
        print('王八是饿死的!')
    else:
        print('鱼都被吃完啦!')
Game()
最佳答案
2018-2-1 14:25:49
根据条件,基本都是饿死的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

使用道具 举报

发表于 2018-2-1 14:25:49 | 显示全部楼层    本楼为最佳答案   
根据条件,基本都是饿死的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

也是,粥越喝越稀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-9 16:00:54 | 显示全部楼层
1个王八10条鱼,王八奇迹般的赢了一盘
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-17 04:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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