天天看点

CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关

CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
yummyBanana = (y) ->
    if not y.green() and not y.rotten()
        goto y
for b in bananas
    yummyBanana b
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
#修复这个函数:
yummyBanana = (y) ->
    if y.green() or y.rotten()
        return no
    return yes

#一旦你修复yummyBanana这个函数,这个编码是好的。
x = 
times ->
    times ->
        if yummyBanana bananas[x]
            goto bananas[x]
        x = x + 
        goto turtle
    turtle.step 
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
#nearestZone函数传回健康地带
#那更接近猴子
nearestZone = () ->
    d0 = distanceTo healthZones[]
    d1 = distanceTo healthZones[]
    if d0 < d1
        #修复函数来传回正确的对象
        return healthZones[]
    else
        return healthZones[]

#一旦你修复nearestZone函数,这个编码是有效的
for b in bananas
    goto b
    if health() < 
        zone = nearestZone()
        goto zone
        until health() == 
            wait()
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
#nearestBridge 函数传回最近的桥
nearestBridge = () ->
    d0 = distanceTo bridges[]
    d1 = distanceTo bridges[]
    #完成这个函数来传回最近的桥
    if d0 <= d1
        return bridges[]
    else
        return bridges[]

#当健康值很低时,猴子后巷最近的桥
#然后去健康地带
#这个函数是正确的!
getHealthy = () ->
    goto nearestBridge()
    goto healthZone
    until health() == 
        wait()
    goto nearestBridge()

#这里不需要更改
for b in bananas
    goto b
    if health() < 
        getHealthy()
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
#safeFrom函数传回yes如果老虎
#睡觉或是玩耍
safeFrom = (a) ->
    return a.sleeping() or a.playing()

for t in tigers
    until safeFrom t
        wait()
    step 
    #现在轮到谁了?
    until safeFrom t
        wait()
    goat.step 
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
safeFrom = (a) ->
    #这个函数应该传回什么呢?
    return a.sleeping() or a.playing()

#一旦你修复了 safeFrom这个函数,这个编码就是正确的。
x = 
for stepper in [monkey, goat]
    #首先猴子先过去,然后是山羊。
    until safeFrom(tigers[x])
        wait()
    stepper.step 
    #等待熊
    until safeFrom(bears[x])
        wait()
    stepper.step 
    x = x + 
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
# nearestGoat函数会获得一个对象
#并比较每只山羊到对象的距离
#函数传回哪只山羊距离对象最近
nearestGoat = (y) ->
    d0 = goats[].distanceTo y
    d1 = goats[].distanceTo y
    if d0 < d1
        return goats[]
    else
        return goats[]

for b in bananas
    #这里有一个小修复
    if b.green()
        g = nearestGoat b
        say g
        g.goto b
    else
        goto b
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
whichAnimal = (y) ->
    if y.green()
        return goat
    return monkey
for b in bananas
    mover = whichAnimal b
    mover.goto b
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
whichAnimal = (y) ->
    if y.green()
        return goat
    else
        return monkey


for b in bananas
    mover = whichAnimal b
    mover.goto b
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
#修复函数useGoat传回yes
#当香蕉是绿色的时候
useGoat = (y) ->
    return y.green()

nearestGoat = (y) ->
    d0 = goats[].distanceTo y
    d1 = goats[].distanceTo y
    if d0 < d1
        return goats[]
    else
        return goats[]

for b in bananas
    mover = monkey
    if useGoat b
        mover = nearestGoat b
    say mover
    mover.goto b
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
#这个函数传回哪只乌龟距离香蕉更近
#这个函数的编码是正确的!
nearestTurtle = (y) -> 
    d0 = turtles[].distanceTo y
    d1 = turtles[].distanceTo y
    if d0 < d1
        return turtles[]
    else
        return turtles[]

for b in bananas
    t = nearestTurtle(b)
    t.goto b

           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
#这个函数传回多少次
#奶牛吃:
leftToEat = (c) ->
    return  - c.weight()


say leftToEat(cow)

#修复这个条件,什么时候循环应该停止?
until leftToEat(cow) == 
    cow.eat()

goto banana
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
#这个函数检测奶牛的重量是否
#和门上的条件相同
shouldNotEat = (c) ->
    return c.weight() == 

say shouldNotEat(cow)

until shouldNotEat(cow)
    cow.eat()
for b in bananas
    goto b
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
#修复这个函数传回如果奶牛的重量是
#和门上的条件相同
shouldNotEat = (c) ->
    return c.weight()==

#使用shouldNotEat函数打开大门
for i in [,,]
    until shouldNotEat(cows[i]) 
        cows[i].eat()
    goto bananas[i]
           
CodeMonkey过关学习笔记系列:特技关卡 14-1 ~ 14-15 关
特技关卡 -
startWithBananaZero = () ->
    d0 = distanceTo bananas[]
    d2 = distanceTo bananas[]
    if d0 < d2
        return yes
    else
        return no

if startWithBananaZero()
    x = 
    times ->
        goto bananas[x]
        x = x + 
else
    x = 
    times ->
        goto bananas[x]
        x = x - 
           

继续阅读