macOS 上的 Playgrounds 的小游戏蛮好玩的,据说是拿来学习 苹果的编程语言 Swift 的,但说实话玩通这章没觉得学到啥,不过好玩就行了。

学习编程 1
学习编程 1
通关截图
通关截图

命令

发出命令

1
2
3
4
moveForward()
moveForward()
moveForward()
collectGem()

添加新命令

1
2
3
4
5
6
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
collectGem()

切换开关

1
2
3
4
5
6
7
8
9
10
moveForward()
moveForward()
turnLeft()
moveForward()
collectGem()
moveForward()
turnLeft()
moveForward()
moveForward()
toggleSwitch()

传送门练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
moveForward()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
toggleSwitch()
moveForward()
toggleSwitch()
moveForward()
turnLeft()
moveForward()
moveForward()
collectGem()
turnLeft()
turnLeft()
moveForward()
moveForward()
turnLeft()
turnLeft()
turnLeft()
moveForward()
moveForward()

发现并修复程序错误

1
2
3
4
5
6
7
moveForward()
moveForward()
turnLeft()
moveForward()
collectGem()
moveForward()
toggleSwitch()

消灭程序错误练习

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
moveForward()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
turnLeft()
turnLeft()
turnLeft()
moveForward()
moveForward()
collectGem()
turnLeft()
turnLeft()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
turnLeft()
turnLeft()
turnLeft()
moveForward()
moveForward()
turnLeft()
turnLeft()
turnLeft()
moveForward()
moveForward()
toggleSwitch()
turnLeft()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()

最短路线

1
2
3
4
5
6
7
8
9
10
11
12
moveForward()
moveForward()
moveForward()
collectGem()
moveForward()
moveForward()
moveForward()
moveForward()
toggleSwitch()
moveForward()
moveForward()
moveForward()

函数

组合新行为

1
2
3
4
5
6
7
8
9
10
moveForward()
moveForward()
moveForward()
turnLeft()
turnLeft()
turnLeft()
moveForward()
moveForward()
moveForward()
collectGem()

创建新函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func turnRight() {
turnLeft()
turnLeft()
turnLeft()
}
moveForward()
turnLeft()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnLeft()
moveForward()
toggleSwitch()

收集、切换、重复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func go_AHEAD() {
moveForward()
collectGem()
moveForward()
toggleSwitch()
}
go_AHEAD()
moveForward()
turnLeft()
go_AHEAD()
moveForward()
moveForward()
turnLeft()
go_AHEAD()
moveForward()
turnLeft()
go_AHEAD()

尽收囊中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func forward_and_collect() {
collectGem()
moveForward()
}
forward_and_collect()
forward_and_collect()
func turnRight(){
turnLeft()
turnLeft()
turnLeft()
}
turnRight()
forward_and_collect()
forward_and_collect()
turnRight()
forward_and_collect()
forward_and_collect()
turnRight()
forward_and_collect()
turnRight()
forward_and_collect()
collectGem()

嵌套模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func turnAround() {
turnLeft()
turnLeft
}
func solveStair() {
moveForward()
collectGem()
}
solveStair()
turnAround()
moveForward()
solveStair()
turnAround()
moveForward()
turnRight()
solveStair()
turnAround()
moveForward()
solveStair()

嵌入式阶梯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func collectGemTurnAround() {
moveForward()
moveForward()
collectGem()
turnLeft()
turnLeft()
moveForward()
moveForward()
}
func solveRow() {
collectGemTurnAround()
collectGemTurnAround()
}
solveRow()
turnRight()
moveForward()
turnLeft()
solveRow()
turnRight()
moveForward()
turnLeft()
solveRow()

寻宝

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
func moveThenToggle()
{
moveForward()
moveForward()
toggleSwitch()
}
func turnAround()
{
turnLeft()
turnLeft()
}
func twoMoveForward()
{
moveForward()
moveForward()
}
moveThenToggle()
turnAround()
twoMoveForward()
moveThenToggle()
turnAround()
twoMoveForward()
turnLeft()
moveThenToggle()
moveThenToggle()
turnAround()
twoMoveForward()
twoMoveForward()
moveThenToggle()
moveThenToggle()

For 循环

使用循环

1
2
3
4
5
6
for i in 1 ... 5 {
moveForward()
moveForward()
collectGem()
moveForward()
}

循环每一侧

1
2
3
4
5
6
7
8
for i in 1 ... 4 {
moveForward()
collectGem()
moveForward()
moveForward()
moveForward()
turnRight()
}

行至边缘再返回

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
for i in 1 ... 2 {
moveForward()
}
toggleSwitch()
turnLeft()
for i in 1 ... 2 {
moveForward()
}
turnLeft()
for i in 1 ... 2 {
moveForward()
}
toggleSwitch()
for i in 1 ... 2 {
moveForward()
}
turnLeft()
for i in 1 ... 2 {
moveForward()
}
toggleSwitch()
for i in 1 ... 2 {
moveForward()
}
turnLeft()
for i in 1 ... 2 {
moveForward()
}
toggleSwitch()

循环跳跃者

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
moveForward()
turnLeft()
for i in 1 ... 2 {
moveForward()
}
collectGem()
turnRight()
moveForward()
turnLeft()
for i in 1 ... 2 {
moveForward()
}
collectGem()
turnRight()
moveForward()
turnLeft()
for i in 1 ... 2 {
moveForward()
}
collectGem()
turnRight()
moveForward()
turnLeft()
for i in 1 ... 2 {
moveForward()
}
collectGem()
turnRight()
moveForward()
turnLeft()
moveForward()
moveForward()
collectGem()

拓展技能

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
func turnDirection(){
for i in 1 ... 2 {
moveForward()
}
turnRight()
}
func turnAround() {
for i in 1 ... 2 {
turnLeft()
}

}
func stepTask(){
for i in 1 ... 7 {
moveForward()
}
toggleSwitch()
turnAround()
for i in 1 ... 7 {
moveForward()
}
}

for i in 1 ... 3 {
turnDirection()
stepTask()
turnRight()
}

宝石农场

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
func taskProcess()
{
turnRight()
for i in 1 ... 2 {
moveForward()
collectGem()
}
func turnAround() {
turnLeft()
turnLeft()
}
turnAround()
for i in 1 ... 2 {
moveForward()
}
for i in 1 ... 2 {
moveForward()
toggleSwitch()
}
turnAround()
for i in 1 ... 2 {
moveForward()
}
func turnDirection() {
turnDirection()
moveForward()
}
turnLeft()
moveForward()
}
for i in 1 ... 3 {
taskProcess()
}

席卷四处

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
for i in 1 ... 2 {
moveForward()
collectGem()
}
func turnAround() {
for i in 1 ... 2 {
turnLeft()
}
}
turnAround()
moveForward()
turnRight()
moveForward()
collectGem()
turnAround()
for i in 1 ... 2 {
moveForward()
}
collectGem()
moveForward()
for i in 1 ... 2 {
moveForward()
collectGem()
}
turnAround()
moveForward()
turnRight()
moveForward()
collectGem()
turnAround()
for i in 1 ... 2 {
moveForward()
}
collectGem()
moveForward()
for i in 1 ... 2 {
moveForward()
collectGem()
}
turnAround()
moveForward()
turnRight()
moveForward()
collectGem()
turnAround()
for i in 1 ... 2 {
moveForward()
}
collectGem()
moveForward()
for i in 1 ... 2 {
moveForward()
collectGem()
}
turnAround()
moveForward()
turnRight()
moveForward()
collectGem()
turnAround()
for i in 1 ... 2 {
moveForward()
}
collectGem()

条件代码

检查开关

1
2
3
4
5
6
7
moveForward()
for i in 1 ... 3 {
moveForward()
if isOnClosedSwitch {
toggleSwitch()
}
}

使用 else if

1
2
3
4
5
6
7
8
9
10
11
for i in 1 ... 2 {
moveForward()
if isOnClosedSwitch
{
toggleSwitch()
}
else if isOnGem
{
collectGem()
}
}

循环条件代码

1
2
3
4
5
6
7
8
9
for i in 1 ... 2 {
moveForward()
if isOnGem{
collectGem()
}
else if isOnClosedSwitch{
toggleSwitch()
}
}

满足条件时向上爬

1
2
3
4
5
6
7
8
9
10
11
12
13
func taskGem(){
for i in 1 ... 4 {
if isOnGem {
collectGem()
} else {
moveForward()
}
}
turnLeft()
}
for i in 1 ... 4 {
taskGem()
}

定义更巧妙的函数

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
func collectOrToggle() {
if isOnClosedSwitch{
toggleSwitch()
}
else if isOnGem{
collectGem()
}
}
func twoSteps() {
for i in 1 ... 2 {
moveForward()
}
}
for i in 1 ... 2 {
twoSteps()
collectOrToggle()
}
for i in 1 ... 2 {
turnLeft()
twoSteps()
}
collectGem()
twoSteps()
collectGem()
turnRight()
moveForward()
turnRight()
twoSteps()
collectOrToggle()
twoSteps()
collectOrToggle()

围困其中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
func isOnClosedSwitchOrGem(){
if isOnClosedSwitch{
toggleSwitch()
}
else if isOnGem{
collectGem()
}
}
moveForward()
isOnClosedSwitchOrGem()
turnRight()
moveForward()
isOnClosedSwitchOrGem()
turnRight()
for i in 1 ... 3 {
for i in 1 ... 2 {
moveForward()
isOnClosedSwitchOrGem()
}
turnRight()
}

决策树

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
for i in 1 ... 2 {
moveForward()
collectGem()
func GemOrSwitch()
{
if isOnGem{
collectGem()
}
else if isOnClosedSwitch{
toggleSwitch()
}
}
func turnAround(){
for i in 1 ... 2 {
turnLeft()
}

}
func longBridge() {
turnRight()
for i in 1 ... 3 {
moveForward()
}
turnLeft()
moveForward()
GemOrSwitch()
turnAround()
moveForward()
turnRight()
for i in 1 ... 3 {
moveForward()
}
turnRight()
}
longBridge()
moveForward()
GemOrSwitch()
turnLeft()
moveForward()
GemOrSwitch()
turnAround()
moveForward()
turnLeft()
moveForward()
}

逻辑运算符

使用“非”运算符

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
func turnAround()
{
for i in 1 ... 2 {
turnLeft()
}

}
for i in 1 ... 4 {
moveForward()
if !isOnGem{
turnLeft()
for i in 1 ... 2 {
moveForward()
}
collectGem()
turnAround()
for i in 1 ... 2 {
moveForward()
}
turnLeft()
}
else{
collectGem()
}
}

非之螺旋

1
2
3
4
5
6
7
for i in 1 ... 13 {
moveForward()
if isBlocked{
turnLeft()
}
}
toggleSwitch()

检查这个与那个

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
for i in 1 ... 7 {
moveForward()
if isOnGem && !isBlockedLeft{
collectGem()
}
else if isOnGem && isBlockedLeft{
turnRight()
for i in 1 ... 2 {
moveForward()
}
func backLine(){
func turnAround(){
for i in 1 ... 2 {
turnLeft()
}
}
turnAround()
for i in 1 ... 2 {
moveForward()
}
}
if isOnClosedSwitch{
toggleSwitch()
}
backLine()
if isOnGem {
collectGem()
}
turnRight()
}
}

检查这个或那个

1
2
3
4
5
6
7
for i in 1 ... 12 {
moveForward()
if isBlocked || isBlockedLeft{
turnRight()
}
}
collectGem()

逻辑迷宫

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
for i in 1 ... 8 {
moveForward()
if isOnGem && isOnClosedSwitch{
collectGem()
toggleSwitch()
turnRight()
for i in 1 ... 2 {
moveForward()
}
collectGem()
func turnAround(){
for i in 1 ... 2 {
turnRight()
}
}
turnAround()
for i in 1 ... 2 {
moveForward()
}
turnRight()
}
else if isBlocked && isOnClosedSwitch{
toggleSwitch()
turnLeft()
}
else if isOnGem || isOnClosedSwitch{
if isOnGem{
collectGem()
}
else if isOnClosedSwitch{
turnLeft()
toggleSwitch()
}
}
}

While 循环

当…时运行代码

1
2
3
4
while isOnCloseSwitch() {
toggleSwitch()
moveForward()
}

创建更巧妙的 While 循环

1
2
3
4
5
6
7
8
9
10
moveForward()
while !isBlocked {
if isOnOpenSwitch {
moveForward()
}
else{
toggleSwitch()
moveForward()
}
}

正确选取工具

1
2
3
4
5
6
7
8
9
10
func turnAndCollectGem() {
moveForward()
turnLeft()
moveForward()
collectGem()
turnRight()
}
while !isBlocked {
turnAndCollectGem()
}

四乘以四

1
2
3
4
5
6
7
8
9
while !isBlocked {
moveForward()
if isOnClosedSwitch {
toggleSwitch()
}
if (isBlockedLeft || isBlocked) && isOnOpenSwitch{
turnRight()
}
}

转身

1
2
3
4
5
6
7
8
9
10
while !isBlocked {
moveForward()
if isOnGem && isBlocked{
collectGem()
turnLeft()
}
else if !isOnGem{
turnRight()
}
}

富饶之地

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
while !isBlocked {
moveForward()
if isOnGem || isOnClosedSwitch{
if isOnGem{
collectGem()
}
else if isOnClosedSwitch{
toggleSwitch()
}
}
else if isBlocked{
if isBlockedLeft {
turnRight()
moveForward()
turnRight()
}

else if isBlockedRight {
turnRight()
}
else{
turnLeft()
moveForward()
turnLeft()
}
}
}

嵌套循环

1
2
3
4
5
6
7
while isOnGem || isBlockedLeft {
while isOnGemm {
turnLeft()
collectGem()
}
moveForward()
}

随机矩形

1
2
3
4
5
6
7
while !isBlocked{
moveForward()
if isBlocked {
turnRight()
}
}
toggleSwitch()

始终右转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while !isBlocked{
moveForward()
if isOnClosedSwitch || isOnGem{
if isOnClosedSwitch{
toggleSwitch()
}
else{
collectGem()
}
}
else if isBlocked && isBlockedLeft{
turnRight()
}
}

算法

右手定则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
func navigateAroundWall() {
if isBlockedRight{
moveForward()
} else {
turnRight()
moveForward()
}
}
while !isOnClosedSwitch{
navigateAroundWall()
if isOnGem{
collectGem()
func turnAround() {
for i in 1 ... 2 {
turnLeft()
}
}
turnAround()
}
}
toggleSwitch()

调整算法

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
func navigateAroundWall() {
if !isBlocked{
if isBlockedRight{
moveForward()
}
else{
turnRight()
moveForward()
}
}
else if isBlocked{
if isBlockedRight {
turnLeft()
moveForward()
}
else{
turnRight()
moveForward()
}

}
}

while !isOnClosedSwitch {
navigateAroundWall()
if isOnGem {
collectGem()
turnLeft()
turnLeft()
}
}
toggleSwitch()

征服迷宫

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while !isOnGem{
if !isBlocked{
moveForward()
if isBlockedRight{
isBlockedLeft
}
else if !isBlockedRight{
turnRight()
}
}
else if isBlocked{
if isBlockedRight {
turnLeft()
}
else{
turnRight()
}
}
}
collectGem()

左转还是右转?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while !isOnGem{
moveForward()
if !isOnOpenSwitch{
if isOnClosedSwitch{
if !isBlocked{
toggleSwitch()
turnRight()
}
else{
toggleSwitch()
turnLeft()
}
}
}
}
collectGem()

向左走,向右走

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
while !isOnOpenSwitch{
if isOnGem || isOnClosedSwitch{
if isOnGem{
collectGem()
}
else{
toggleSwitch()
}
}
if isBlocked{
if isBlockedRight{
turnLeft()
moveForward()
}
else if isOnOpenSwitch && isBlocked{
turnLeft()
moveForward()
}
else{
turnRight()
moveForward()
}
}
else{
moveForward()
}
}