Codemonkey之编码冒险之特技模式1-165关卡学习笔记与参考答案

CodeMonkey过关学习笔记系列1-165关卡

image1.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 1-1
你可以在不使用 右转 turn right 指令的前提下解决这一关吗 ?

turn left
turn left
turn left
step 15


或者:
turn -90
step 15

image2.jpeg

1
2
3
4
5
6
7
8
特技关卡 1-2
turn left
step 10
turn left
step 12



image3.jpeg

1
2
3
4
5
6
7
8
特技关卡 1-3
step 10
turn right
step 15
turn right
step 10


image4.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 1-4
step 17
turn right
step 10
turn right

step 8
turn left
step 8
turn right
step 10



image5.jpeg

1
2
3
4
5
特技关卡 1-5
turn 25
step 18


image6.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 1-6
turn 24
step distanceTo bananas[0]

turn -130

step distanceTo bananas[1]


image7.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 1-7
step 15
turn left
step 10
turn left
step 15






image8.jpeg

1
2
3
4
5
6
7
8
特技关卡 1-8
step 15
turn right
step 15
turn right
step 15


image9.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 1-9
step 5
step -10
turn right
step 10
step -20



image10.jpeg

1
2
3
4
5
6
7
特技关卡 1-10

step -15
turn left
step 15


image11.jpeg

1
2
3
4
5
6
7
特技关卡 1-11
turn 45
step -5
step 20
turn left
step 10

image12.jpeg

1
2
3
4
5
6
7
特技关卡 2-1
turnTo bridge
step 15
turnTo banana
step 15


image13.jpeg

1
2
3
4
5
6
7
8
特技关卡 2-2

turnTo bush
step 20
turnTo banana
step 22


image14.jpeg

1
2
3
4
5
6
7
特技关卡 2-3
turtle.step 10
step 10
turn left
step 10


image15.jpeg

1
2
3
4
5
6
7
特技关卡 2-4
step 10
step -10
turtle.step 20
step 10


image16.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 2-5
step 15
turn right
step 15
turn right

turtle.step 15
step 15


image17.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 2-6
turtle.turn left
monkey.step 10

turtle.step 20

monkey.step 10


image18.jpeg

1
2
3
4
5
6
7
8
特技关卡 2-7
turtle.step 15
monkey.turnTo turtle
monkey.step 11
turtle.step -14
monkey.step -10


image19.jpeg

1
2
3
4
5
6
7
特技关卡 2-8
turtle.turnTo banana
monkey.turnTo turtle
step 15
turtle.step 15


image20.jpeg

1
2
3
4
5
特技关卡 2-9
turtle.step 15
turtle.step -30


image21.jpeg

1
2
3
4
5
6
7
特技关卡 2-10
turtle.turnTo monkey
turtle.step 10
turtle.step -25



image22.jpeg

1
2
3
4
5
6
特技关卡 3-1
3.times ->
turn right
step 15


image23.jpeg

1
2
3
4
5
6
特技关卡 3-2
7.times ->
step 8
turn 45


image24.jpeg

1
2
3
4
5
特技关卡 3-3
15.times ->
turn 21
step 3

image25.jpeg

1
2
3
4
5
6
7
8
特技关卡 3-4
3.times ->
step 8
turn 140
step 8
turn -140


image26.jpeg

1
2
3
4
5
6
7
8
特技关卡 3-5
4.times ->
step 5
turn left
step 5
turn right
step -20

image27.jpeg

1
2
3
4
5
6
7
8
特技关卡 3-6
step 4
10.times ->
turtle.step 5
turtle.turn 36



image28.jpeg

1
2
3
4
5
6
特技关卡 3-7
8.times ->
step 10
step -10
turn 45

image29.jpeg

1
2
3
4
5
6
7
8
特技关卡 3-8
3.times ->
turn right
step 10

step 10


image30.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 3-9
turnTo turtle
step 3
10.times ->
turtle.turn -18
turtle.step 3
turtle.step 5
step 4

image31.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 3-10
turn left
3.times ->
step 10
step -20
step 10
turtle.step 10


image32.jpeg

1
2
3
4
特技关卡 4-1
x = 20
step x

image33.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 4-2
x = 10
5.times ->
turn 60
step x
或者
x = -10
5.times ->
step x
turn -60


image34.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
特技关卡 4-3
num = 8
distance = 7
degree = 45

num.times ->
step distance
turn degree
或者
num = 8
distance = -7
degree = -45
num.times ->
turn degree
step distance


image35.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 4-4
d = 20

13.times ->
step 6
turn d
d = d + 4


image36.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 4-5
x = 15

turtle.step x
step x
turn left
step x


image37.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 4-6
x = 8
4.times ->
step x
step 0-x-x
step x
turtle.step 7.5


image38.jpeg

1
2
3
4
5
6
7
8
特技关卡 4-7
x = 20
say x

turnTo banana
step x


image39.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 4-8
x = 17
turtle.step x
2.times ->
turn right
step x



image40.jpeg

1
2
3
4
5
特技关卡 4-9
x = distanceTo banana
step x


image41.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 4-10
x = monkey.distanceTo bridge
y = bridge.distanceTo banana

step x
turnTo banana
step y




image42.jpeg

1
2
3
4
5
6
特技关卡 4-11
d = distanceTo banana

step -d


image43.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 4-12
x = turtle.distanceTo banana
y = monkey.distanceTo banana

turtle.turnTo banana
turtle.step x
monkey.step y


image44.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 4-13
d = turtle.distanceTo islands[4]
turtle.step d
m = monkey.distanceTo turtle
step m

turtle.step -d
turtle.turnTo banana
turtle.step m


image45.jpeg

1
2
3
4
5
6
7
8
特技关卡 4-14
say "RUN!"
monkey.step distanceTo bush

monkey.turnTo banana
monkey.step distanceTo banana


image46.jpeg

1
2
3
4
5
6
7
8
特技关卡 4-15
x = 0
3.times ->
turnTo bananas[x]
step 15
x = x + 1


image47.jpeg

1
2
3
4
5
6
7
特技关卡 5-1
turtles[0].step 6
turtles[2].step -8
turtles[1].step 6+6
step 19


image48.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 5-2
step 6
turtles[2].step 20
step 6
turtles[1].step 8
step 6
turtles[0].step -12


image49.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
特技关卡 5-3
turtle.turnTo bananas[0]
turtle.step 9

turtle.turnTo bananas[1]
turtle.step 19


turtle.turnTo bananas[2]
turtle.step 19

//或者利用distanceTo来测量距离
turtle.turnTo bananas[0]
turtle.step distanceTo bananas[0]

turtle.turnTo bananas[1]
turtle.step distanceTo bananas[1]


turtle.turnTo bananas[2]
turtle.step distanceTo bananas[2]

//或者 使用之前我们学过的times循环
i = 0
3.times ->
turtle.turnTo bananas[i]
turtle.step distanceTo bananas[i]
i++

//或者 使用我们还没学到的for循环
for b in bananas
turtle.turnTo b
turtle.step distanceTo b


image50.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
特技关卡 5-4
turnTo bananas[4]
step distanceTo bananas[4]

turnTo bananas[7]
step distanceTo bananas[7]

turnTo bananas[10]
step distanceTo bananas[10]

turnTo bananas[7*2]
step distanceTo bananas[7*2]



image51.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 5-5
step 15
beavers[1].step 15
beavers[0].step -15
turn left
step 15
turn left
step 15


image52.jpeg

1
2
3
4
5
6
7
8
特技关卡 5-6
x=0
4.times ->
beavers[x].step 2.5*(x+1)
x += 1
step distanceTo banana


image53.jpeg

1
2
3
4
5
6
7
特技关卡 5-7
beavers[0].step 5
beavers[1].step 10
beavers[2].step 10
step distanceTo banana


image54.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 5-8
x = 0
7.times ->
turnTo bananas[x]
step distanceTo bananas[x]
x = x+1



image55.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 5-9

crocodiles[2].turn left
crocodiles[0].turn -45


step 9
turn left
step 9
turn right
step 9



image56.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
特技关卡 5-10
goats[0].step 3
goats[0].hit()
goats[0].step -11
goats[0].hit()
goats[1].step 14
goats[1].hit()
goats[1].step -18
goats[1].hit()
4.times ->
step 20
turn right

//另外一种解法
turnStepHit = (goat, banana) ->
goat.turnTo banana
goat.step goat.distanceTo banana
goat.hit()
turnStepHit(goats[0], bananas[1])
turnStepHit(goats[0], bananas[2])
turnStepHit(goats[1], bananas[0])
turnStepHit(goats[1], bananas[3])
4.times ->
step 20
turn right

//另外一种解法
eatBananas = (monkey, goat, banana, next) ->
goat.turnTo(banana)
goat.step(goat.distanceTo(banana))
goat.hit()
monkey.step(monkey.distanceTo(banana))
monkey.turnTo(next)

eatBananas(monkey, goats[1], bananas[0], bananas[1])
eatBananas(monkey, goats[0], bananas[1], bananas[2])
eatBananas(monkey, goats[0], bananas[2], bananas[3])
eatBananas(monkey, goats[1], bananas[3], bananas[0])

//monkey eat bananas with goat help
eatBananas = (banana, next, goat) ->
goat.turnTo(banana)
goat.step(goat.distanceTo(banana))
goat.hit()
step(distanceTo(banana))
turnTo(next)
eatBananas(bananas[0], bananas[1],goats[1]) # 第一个参数顺序很重要
eatBananas(bananas[1], bananas[2],goats[0])
eatBananas(bananas[2], bananas[3],goats[0])
eatBananas(bananas[3], bananas[0],goats[1])

image57.jpeg

1
2
3
4
5
6
特技关卡 6-1
for b in bananas
turnTo b
step distanceTo b


image58.jpeg

1
2
3
4
5
6
7
8
特技关卡 6-2
for b in bananas
turnTo bridge
step distanceTo bridge
turnTo b
step distanceTo b


image59.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 6-3
turnTo bridges[0]
step distanceTo bridges[0]
for b in bananas
turnTo b
step distanceTo b



image60.jpeg

1
2
3
4
5
6
特技关卡 6-4
for b in bushes
turnTo b
step distanceTo b


image61.jpeg

1
2
3
4
5
6
特技关卡 6-5
for t in turtles
step distanceTo t
t.step 12


image62.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 6-6
for t in turtles
step distanceTo t
2.times ->
t.step 14
t.turn 360



image63.jpeg

1
2
3
4
5
特技关卡 6-7
for t in turtles
t.step 8
step 3

image64.jpeg

1
2
3
4
5
6
特技关卡 6-8
for b in bananas
turnTo b
step distanceTo b


image65.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 6-9
for c in crocodiles
c.turnTo bananas[6]

for i in islands
turnTo i
step distanceTo i


image66.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 6-10
for c in crocodiles
step distanceTo c
c.turn left
step 10
step -10
c.turn right



image67.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 6-11
for c in crocodiles
turnTo c
step distanceTo c

for r in rafts
turnTo r
step distanceTo r

for i in islands
turnTo i
step distanceTo i


image68.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 6-12
i = 0
for t in turtles
d = t.distanceTo bananas[i++]
t.step d
monkey.step distanceTo t
t.step -d
t.step d



image69.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 6-13
for c in crocodiles
c.turn 360
step -6.5
或者
for c in crocodiles
c.turn 180
step 2
turn 180



image70.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 6-14
x = 0
#这个循环会帮助你拿到河边的香蕉
3.times ->
turnTo bananas[x]
step distanceTo bananas[x]
beaver.step 6
x = x + 1
#现在写一个 for 循环,拿到剩下的香蕉吧
for b in bushes
turnTo b
step distanceTo b


image71.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 6-15
for g in goats
g.step 9
g.hit()
monkey.step distanceTo bananas[3]






image72.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 7-1

step distanceTo match
grab()
step -(distanceTo bush)
turnTo pile
step distanceTo pile
drop()


image73.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 7-2
turnTo match
step distanceTo match
grab()
turtle.step 10
turnTo pile
step distanceTo pile
drop()


image74.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 7-3
goto = (m) ->
turnTo m
step distanceTo m

goto match
grab()
goto pile
drop()

image75.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 7-4
goto = (t) ->
turnTo t
step distanceTo t

for m in matches
goto m
grab()
goto pile
drop()

image76.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
特技关卡 7-5
goto = (t) ->
turnTo t
step distanceTo t

grab()
newturtles = turtles.reverse()
for t in newturtles
goto t
t.step 20
goto pile

drop()


image77.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 7-6
goto = (t) ->
turnTo t
step distanceTo t

for m in matches
goto m
grab()
goto pile
drop()


image78.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
特技关卡 7-7
getAndReturn = (d) ->
step d
grab()
step -d
drop()

x = 9
4.times ->
getAndReturn x
x = x + 3


image79.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 7-8
halfCircle = () ->
18.times ->
step 2
turn 10

halfCircle()
grab()
halfCircle()
drop()

image80.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 7-9
goto = (t) ->
turnTo t
step distanceTo t

grab()
goto bushes[1]
goto bushes[0]

goto pile
drop()

image81.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 7-10
goto = (t) ->
turnTo t
step distanceTo t
goto bushes[13]
goto pile
goto match
grab()
goto pile
drop()

image82.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
特技关卡 7-11
goto = (t) ->
turnTo t
step distanceTo t

gotoBanana = (index) ->
#你只能修改这里的代码
goto bananas[index]

gotoBanana 3
gotoBanana 1
gotoBanana 2
gotoBanana 4
gotoBanana 0

image83.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
特技关卡 7-12
goto = (d) ->
turnTo d
step distanceTo d
rideTurtle = (t) ->
goto turtle
turtle.step t
getMatches = () ->
for m in matches
rideTurtle 20
goto m
grab()
rideTurtle -20
goto pile
drop()
getMatches()

image84.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 7-13
foo = (x) ->
beavers[x].step x*2
goto = (t) ->
turnTo t
step distanceTo t
i = 0
4.times ->
foo i
i = i + 1
grab()
goto pile
drop()

image85.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
特技关卡 7-14
goto = (t) ->
turnTo t
step distanceTo t

allStep = (kind) ->
for a in kind
a.step 5

#这两行应该建成两座桥
allStep beavers
#完成这里的代码

goto match
grab()
allStep turtles

goto pile
drop()

image86.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

特技关卡 7-15
goto = (t) ->
turnTo t
step distanceTo t

allCrocsTurn = (d) ->
for c in crocodiles
c.turn d


allCrocsTurn right
goto raft
allCrocsTurn left
for b in bananas
goto b

image87.jpeg

1
2
3
4
5
特技关卡 8-1
until near banana
turnTo banana
step 1

image88.jpeg

1
2
3
4
5
6
特技关卡 8-2
for b in bananas
until near b
turnTo b
step 1

image89.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 8-3
chase = (b) ->
until near b
turnTo b
step 1

chase bananas[1]
chase bridge
chase bananas[0]

image90.jpeg

1
2
3
4
5
特技关卡 8-4
until near banana
say "Not yet..."
crocodile.turn 45

image91.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 8-5
until beavers[0].near bananas[2]
beavers[0].step 1
until beavers[2].near bananas[0]
beavers[2].step 1
until beavers[1].near bananas[1]
beavers[1].step 1

step distanceTo islands[1]

image92.jpeg

1
2
3
4
5
特技关卡 8-6
until near banana
turtle.turnTo banana
turtle.step 1

image93.jpeg

1
2
3
4
5
6
特技关卡 8-7
until cat.sleeping()
wait()

step distanceTo banana

image94.jpeg

1
2
3
4
5
6
7
特技关卡 8-8

step distanceTo islands[4]

until near banana
wait()

image95.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 8-9
i = 0
for b in bridges
turnTo b
step distanceTo b
until near bananas[i]
wait()
i++

image96.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 8-10
for c in crocodiles
c.turn 90
for b in bananas

step 7
until near b
wait()


image97.jpeg

1
2
3
4
5
特技关卡 8-11
for banana in bananas
until near banana
wait()

image98.jpeg

1
2
3
4
5
6
7
特技关卡 8-12
for banana in bananas
until near banana
crocodile.turn 360
wait()


image99.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 8-13
until cats[1].sleeping()
wait()
step 10
until cats[0].sleeping()
wait()
step 10


image100.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 8-14
i = 0
for t in turtles
step distanceTo t
d = distanceTo bushes[i]
t.step d
until near bananas[i]
wait()
t.step -d
i = i + 1

image101.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 8-15
goto = (t) ->
turnTo t
step distanceTo t
until cat.sleeping()
wait()
goto bananas[0]
until cat.sleeping()
wait()
goto bushes[6]
goto bushes[15]
until near bananas[1]
wait()

image102.jpeg

1
2
3
4
5
6
7
特技关卡 9-1

for b in bananas
if b.frozen()
goat.goto b
goat.hit()
goto b

image103.jpeg

1
2
3
4
5
6
7
8
特技关卡 9-2
goat.goto(bridge)
for b in bananas
if b.frozen()
goat.goto(b)
goat.hit(b)
goto b

image104.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 9-3
for t in turtles
t.step 15

for b in bananas
if b.frozen()
goat.goto(b)
goat.hit(b)
goto b


image105.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
特技关卡 9-4
x = 5

6.times ->
if bananas[x].frozen()
goat.goto bananas[x]
goat.hit()


x = x - 1


goto turtle
turtle.step 20
for b in bananas
goto b

image106.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 9-5
#你知道演习吗?谁先去?
for b in bananas
if b.frozen()
turtle.goto b
goat.hit()
turtle.step -5
step 12

image107.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 9-6
i = 0
4.times ->
if bananas[i].frozen()
goats[i].goto bananas[i]
goats[i].hit()
monkey.goto bananas[i]
i = i + 1

image108.jpeg

1
2
3
4
5
6
7
8
特技关卡 9-7
for b in bananas
if b.frozen()
turtle.goto b
goat.hit()
turtle.step -5
goto b

image109.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 9-8
#提示:这里我们有多少只乌龟?

for b in bananas
if b.frozen()
goat.hit()
goto b
goto turtles[1]
for t in turtles
t.step 7

image110.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
特技关卡 9-9
#提示:猴子可以等待山羊完成
#首先撞击所有的冰冻香蕉

for b in bananas
if b.frozen()
goat.goto b
goat.hit()
#现在让我们获得那些香蕉。
#我们应该从哪里开始?并且多少次?
x = 5
6.times ->
goto bananas[x]
x = x - 1


image111.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 9-10
x = 0
for g in goats
3.times ->
g.goto bananas[x]
if bananas[x].frozen()
g.hit()
x = x + 1
for b in bananas
goto b

image112.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 9-11
x = 0
2.times ->
4.times ->
if bananas[x].frozen()
goat.goto bananas[x]
goat.hit()
goto bananas[x]
x = x + 1
goat.goto bridge
goto bridge

image113.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 10-1
for b in bananas
if not b.green()
goto bridges[1]
goto b
else
goat.goto bridges[0]
goat.goto b

image114.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 10-2
for b in bananas
#山羊和猴子都不可以得到所有的香蕉
#完成编码
if b.green()
goat.goto crocodile
crocodile.turnTo b
goat.goto b
else
turtle.goto b
goto b

image115.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
特技关卡 10-3
for b in bananas
#首先,我们需要打破冰冻的香蕉
if b.frozen()
goat.goto b
goat.hit()
goat.goto turtles[1]
#现在,如果香蕉是绿色的,山羊会吃掉它
if b.green()
goat.goto b
goat.step -7
#如果不是,猴子会吃掉它
else
goto b
step -7

for t in turtles
t.step 6

image116.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 10-4
#确定你可以解决这个
#提示:检查哪只海狸可以帮助哪只动物
#过河
beavers[1].step 5
beavers[0].step 5

for b in bananas
if b.green()
goat.goto b
else
monkey.goto b


image117.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
特技关卡 10-5
breakBanana = (banana) ->
#对冰冻的香蕉增加山羊撞击的编码
say "frozen"
turtles[0].goto banana
goat.hit()


eatBanana = (banana) ->
#增加山羊吃绿色香蕉的编码
if banana.green()
turtles[0].goto banana
else
turtles[1].goto banana


for b in bananas
if b.frozen()
breakBanana(b)
eatBanana b


image118.jpeg

1
2
3
4
5
6
7
8
9

特技关卡 10-6
for b in bananas
if b.green()
mover = goat
else
mover = monkey
say mover
mover.goto b

image119.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 10-7
mover = monkey
for b in bananas
if b.green()
x = 5
mover = goat
else
x = 10
mover = monkey
mover.step x

image120.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
特技关卡 10-8
#如果x>0,那么乌龟前进
#如果x<0,那么乌龟后退

x = 12
for b in bananas
if b.green()
turtle.step -x
goat.goto turtle
turtle.step 2*x
goat.goto b
else
monkey.goto turtle
turtle.step -2*x
monkey.goto b

image121.jpeg

1
2
3
4
5
6
7
特技关卡 11-1
#提示:你应该等待直到什么时候?
for b in bananas
until tiger.sleeping() and bear.sleeping()
wait()
goto b

image122.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 11-2
for b in bananas

until tiger.sleeping() and bear.sleeping()
wait() #在这里增加编码
if b.frozen()
goat.goto b
goat.hit()
until tiger.sleeping() and bear.sleeping()
wait()
goto b

image123.jpeg

1
2
3
4
5
6
7
8
特技关卡 11-3
x = 0
2.times ->
until bears[x].sleeping() and tigers[x].sleeping()
wait()
goto bananas[x]
x = x + 1

image124.jpeg

1
2
3
4
5
6
7
特技关卡 11-4

until tiger.sleeping() and bear.sleeping()
wait()
for b in bananas
goto b

image125.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
特技关卡 11-5
for b in bananas
#我们需要等待直到熊睡觉或是玩耍
#或者意味着我们要等到其中之一的条件出现
until bear.sleeping() or bear.playing()
wait()

#当安全条件错过时,我们应该怎么做?
if b.green()
goat.goto b
else
monkey.goto b

image126.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 11-6
for b in bananas

if b.green()
until tiger.sleeping() or tiger.playing()
wait()
goat.goto b
#因此如果可以安全通行,谁应该获得香蕉呢?
else
until bear.sleeping() or bear.playing()
wait()
#在这里增加你的编码
goto b

image127.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 11-7
x = 2
3.times ->
until bears[x].sleeping() or bears[x].playing()
wait()
#我们最好等待熊睡觉或是玩耍
goto bananas[x]
x = x - 1

image128.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
特技关卡 11-8

until tiger.sleeping() or tiger.playing()
wait()
goat.goto bridges[0]


monkey.goto bridges[1]

#都是你的…
for b in bananas
if b.frozen()
goat.goto(b)
goat.hit()
monkey.goto b

image129.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 11-9
for b in bananas
#首先我们希望山羊走过所有的香蕉(绿色的或是冰冻的)
#打破冰冻的香蕉并拿到绿色的香蕉。
#然后,猴子只收集剩下的
if b.green() or b.frozen()
if b.frozen()
goat.goto b
goat.hit()
goat.goto b

#现在轮到猴子了
goto bananas[3]

image130.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
特技关卡 11-10
#首先让山羊走过所有的香蕉
#打破冰冻的香蕉并获得绿色的香蕉
for b in bananas
if b.green() or b.frozen()
if b.frozen()
goat.goto b
goat.hit()
goat.goto b

#现在这里只有非冰冻的黄色香蕉
#猴子可以得到所有的香蕉
2.times ->
step 20
turn left

image131.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 11-11
#变量chaser 可以在老虎和熊之间变换
chaser = bear
2.times ->
say chaser
until chaser.sleeping() or chaser.playing()
wait()
step 10
chaser= tiger

image132.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
特技关卡 11-12
#设置chaser 和 mover根据香蕉是绿色的或是其他
#首先chaser是老虎,mover是猴子
chaser = tiger
mover = monkey

for b in bananas
if b.green()
#完成我:
chaser = bear
mover = goat
else
#和我:
chaser = tiger
mover = monkey

until chaser.sleeping() or chaser.playing()
wait()
mover.goto b

image133.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 11-13
#首先猴子移动然后是山羊
x = 0
3.times ->
for stepper in [monkey,goat]
until tigers[x].sleeping() and bears[x].sleeping()
wait()
stepper.step 10
x = x + 1

image134.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
特技关卡 11-14
waitFor = (b) ->
until b.sleeping() or b.playing()
wait()
x = 0
2.times ->
waitFor(bears[x])
goto bananas[x]
waitFor(bears[x])
goto turtle
turtle.step 10
x = x + 1

image135.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 11-15
#我们将使用圆括号检查他们中的条件
#检查剩下的之前是yes还是no

#这里,我们检查:
#1.老虎是玩耍还是在睡觉
#2.熊是在玩耍还是在睡觉
#3.1是yes和2是yes
until (tiger.playing() or tiger.sleeping()) and (bear.playing() or bear.sleeping())
wait()

#现在可以安全通过
goto banana

image136.jpeg

1
2
3
4
5
6
7
特技关卡 11-16
for b in bananas
#在这里增加你的编码
until ( bear.playing() or bear.sleeping() ) and ( tiger.playing() or tiger.sleeping() )
wait()
goto b

image137.jpeg

1
2
3
4
5
6
7
8
特技关卡 12-1
a = yes

if not a
turn left

step 10

image138.jpeg

1
2
3
4
5
6
7
特技关卡 12-2
for b in bananas
if b.green()
goat.goto b
else if not b.rotten()
goto b

image139.jpeg

1
2
3
4
5
特技关卡 12-3
for b in bananas
if not b.green()
goto b

image140.jpeg

1
2
3
4
5
特技关卡 12-4
for b in bananas
if not b.green()
turtle.goto b

image141.jpeg

1
2
3
4
5
特技关卡 12-5
for b in bananas
if not (b.green() or b.rotten())
goto b

image142.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 12-6
x = 6
7.times ->
b = bananas[ x-- ]
if not b.green() and not b.rotten()
goto b



image143.jpeg

1
2
3
4
5
6
7
8
特技关卡 13-1
#奶牛会一直吃,直到大门上的条件变为真实的
#然后,大门被打开了!
until cow.weight() == 20
cow.eat()

goto banana

image144.jpeg

1
2
3
4
5
6
7
特技关卡 13-2
#我们需要做什么直到奶牛的体重将打开大门?
until cow.weight() == 30
cow.eat()

goto banana

image145.jpeg

1
2
3
4
5
6
7
8
9
10
11
特技关卡 13-3
turtle.step 10
goto turtle

#检查门上的条件来打开大门。
until cow.weight() >= 22
cow.eat()

for b in bananas
goto b

image146.jpeg

1
2
3
4
5
6
特技关卡 13-4
until cow.weight() == 35
cow.eat()

goto banana

image147.jpeg

1
2
3
4
5
6
7
8
特技关卡 13-5
for c in cows
until c.weight() == 20
#修复这里
c.eat()

goto banana

image148.jpeg

1
2
3
4
5
6
7
特技关卡 13-6
until cows[1].weight() == 30
cows[1].eat()
cows[0].eat()
for b in bananas
goto b

image149.jpeg

1
2
3
4
5
6
7
特技关卡 13-7
for c in cows
until c.weight() == 35
c.eat()
for b in bananas
goto b

image150.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
特技关卡 13-8

until cow.weight() == 18
cow.eat()
for b in bananas
goto b
if health() < 50

goto healthZone

until monkey.health() == 100
wait()


image151.jpeg

1
2
3
4
5
6
7
8
9
10
特技关卡 13-9
for cow in cows
until cow.weight() > 19
cow.eat()
i = 3
4.times ->
goto bananas[i--]



image152.jpeg

1
2
3
4
5
6
7
特技关卡 14-1
yummyBanana = (y) ->
if not y.green() and not y.rotten()
goto y
for b in bananas
yummyBanana b

image153.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
特技关卡 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

image154.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
特技关卡 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()

image155.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
特技关卡 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()

image156.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
特技关卡 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

image157.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
特技关卡 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

image158.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
特技关卡 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

image159.jpeg

1
2
3
4
5
6
7
8
9
特技关卡 14-8
whichAnimal = (y) ->
if y.green()
return goat
return monkey
for b in bananas
mover = whichAnimal b
mover.goto b

image160.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 14-9
whichAnimal = (y) ->
if y.green()
return goat
else
return monkey


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

image161.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
特技关卡 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

image162.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
特技关卡 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


image163.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
特技关卡 14-12
#这个函数传回多少次
#奶牛吃:
leftToEat = (c) ->
return 35 - c.weight()


say leftToEat(cow)

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

goto banana

image164.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
特技关卡 14-13
#这个函数检测奶牛的重量是否
#和门上的条件相同
shouldNotEat = (c) ->
return c.weight() == 100

say shouldNotEat(cow)

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

image165.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
特技关卡 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]

image166.jpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
特技关卡 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