天天看點

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 - 
           

繼續閱讀