# CodeMonkey過關學習筆記系列：特技關卡 14-1 ～ 14-15 關

``````特技關卡 14-1
yummyBanana = (y) ->
if not y.green() and not y.rotten()
goto y
for b in bananas
yummyBanana b
``````

``````特技關卡 14-2
#修復這個函數:
yummyBanana = (y) ->
if y.green() or y.rotten()
return no
return yes

#一旦你修復yummyBanana這個函數，這個編碼是好的。
x = 0
3.times ->
2.times ->
if yummyBanana bananas[x]
goto bananas[x]
x = x + 1
goto turtle
turtle.step 12
``````

``````特技關卡 14-3
#nearestZone函數傳回健康地帶
#那更接近猴子
nearestZone = () ->
d0 = distanceTo healthZones[0]
d1 = distanceTo healthZones[1]
if d0 < d1
#修複函數來傳回正確的對象
return healthZones[0]
else
return healthZones[1]

#一旦你修復nearestZone函數，這個編碼是有效的
for b in bananas
goto b
if health() < 50
zone = nearestZone()
goto zone
until health() == 100
wait()
``````

``````特技關卡 14-4
#nearestBridge 函數傳回最近的橋
nearestBridge = () ->
d0 = distanceTo bridges[0]
d1 = distanceTo bridges[1]
#完成這個函數來傳回最近的橋
if d0 <= d1
return bridges[0]
else
return bridges[1]

#當健康值很低時，猴子後巷最近的橋
#然後去健康地帶
#這個函數是正確的！
getHealthy = () ->
goto nearestBridge()
goto healthZone
until health() == 100
wait()
goto nearestBridge()

#這裏不需要更改
for b in bananas
goto b
if health() < 70
getHealthy()
``````

``````特技關卡 14-5
#safeFrom函數傳回yes如果老虎
#睡覺或是玩耍
safeFrom = (a) ->
return a.sleeping() or a.playing()

for t in tigers
until safeFrom t
wait()
step 8
#現在輪到誰了？
until safeFrom t
wait()
goat.step 8
``````

``````特技關卡 14-6
safeFrom = (a) ->
#這個函數應該傳回什麼呢？
return a.sleeping() or a.playing()

#一旦你修復了 safeFrom這個函數，這個編碼就是正確的。
x = 0
for stepper in [monkey, goat]
#首先猴子先過去，然後是山羊。
until safeFrom(tigers[x])
wait()
stepper.step 12
#等待熊
until safeFrom(bears[x])
wait()
stepper.step 12
x = x + 1
``````

``````特技關卡 14-7
# nearestGoat函數會獲得一個對象
#並比較每隻山羊到對象的距離
#函數傳回哪隻山羊距離對象最近
nearestGoat = (y) ->
d0 = goats[0].distanceTo y
d1 = goats[1].distanceTo y
if d0 < d1
return goats[0]
else
return goats[1]

for b in bananas
#這裏有一個小修復
if b.green()
g = nearestGoat b
say g
g.goto b
else
goto b
``````

``````特技關卡 14-8
whichAnimal = (y) ->
if y.green()
return goat
return monkey
for b in bananas
mover = whichAnimal b
mover.goto b
``````

``````特技關卡 14-9
whichAnimal = (y) ->
if y.green()
return goat
else
return monkey

for b in bananas
mover = whichAnimal b
mover.goto b
``````

``````特技關卡 14-10
#修複函數useGoat傳回yes
#當香蕉是綠色的時候
useGoat = (y) ->
return y.green()

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

for b in bananas
mover = monkey
if useGoat b
mover = nearestGoat b
say mover
mover.goto b
``````

``````特技關卡 14-11
#這個函數傳回哪隻烏龜距離香蕉更近
#這個函數的編碼是正確的！
nearestTurtle = (y) ->
d0 = turtles[0].distanceTo y
d1 = turtles[1].distanceTo y
if d0 < d1
return turtles[0]
else
return turtles[1]

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

``````

``````特技關卡 14-12
#這個函數傳回多少次
#奶牛吃：
leftToEat = (c) ->
return 35 - c.weight()

say leftToEat(cow)

#修復這個條件，什麼時候循環應該停止？
until leftToEat(cow) == 0
cow.eat()

goto banana
``````

``````特技關卡 14-13
#這個函數檢測奶牛的重量是否
#和門上的條件相同
shouldNotEat = (c) ->
return c.weight() == 100

say shouldNotEat(cow)

until shouldNotEat(cow)
cow.eat()
for b in bananas
goto b
``````

``````特技關卡 14-14
#修復這個函數傳回如果奶牛的重量是
#和門上的條件相同
shouldNotEat = (c) ->
return c.weight()==60

#使用shouldNotEat函數打開大門
for i in [0,1,2]
until shouldNotEat(cows[i])
cows[i].eat()
goto bananas[i]
``````

``````特技關卡 14-15
startWithBananaZero = () ->
d0 = distanceTo bananas[0]
d2 = distanceTo bananas[2]
if d0 < d2
return yes
else
return no

if startWithBananaZero()
x = 0
3.times ->
goto bananas[x]
x = x + 1
else
x = 2
3.times ->
goto bananas[x]
x = x - 1
``````