Software Odyssey
Friday, April 21, 2023

Playgrounds: Your First Step into the World of Coding

Author:
Yuwei Yang
文章封面圖片

Playgrounds is an interactive learning app designed by Apple for beginners who want to learn the Swift programming language. "Learn to Code" is one of its main topics, available on both Mac and iPad with a Traditional Chinese interface. Although some explanations may not be completely thorough, by following the lessons step-by-step or jumping right in to try things out, learners can more easily grasp the basics of Swift through a gamified and visual approach. Below is my problem-solving log from working through the "Learn to Code" module.

In the 'Start Coding' section, there are nine major learning goals. Each goal includes several implementation challenges designed from the player's perspective. Players must use the concepts learned in each unit to control the game character and complete specific objectives—such as 'collect a gem' or 'toggle a switch'—in order to pass the level.

Before beginning any challenge, each 'introduction' provides a visual, real-world, and beginner-friendly example to help users understand the concept and how to use it. While the activity resembles a game, there is no single correct solution. There are many ways to achieve the goal, and the system encourages players to experiment with different approaches.

Commands

(1) Introduction

(2) Issuing Commands

段落圖片
1moveForward()
2moveForward()
3moveForward()
4collectGem()

(3) Toggling a Switch

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

(4) Finding and Fixing Errors

段落圖片
1moveForward()
2moveForward()
3turnLeft()
4moveForward()
5collectGem()
6moveForward()
7toggleSwitch()

For Loops

(1) Introduction

(2) Using Loops

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

(3) One Side of the Loop

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

Conditionals

(1) Introduction

(2) Checking a Switch

段落圖片
1moveForward()
2moveForward()
3    if isOnClosedSwitch{
4        toggleSwitch()
5    }  
6    moveForward()
7if isOnClosedSwitch{
8    toggleSwitch() 
9}  
10moveForward()
11if isOnClosedSwitch{
12    toggleSwitch() 
13}  

(3) Using else if

段落圖片
1moveForward()
2if isOnGem{
3    collectGem()    
4} else if isOnClosedSwitch{
5    toggleSwitch()    
6}
7moveForward()
8if isOnGem{
9    collectGem()    
10} else if isOnClosedSwitch{
11    toggleSwitch()    
12}

(4) Move Upward When Conditions Are Met

段落圖片
1for i in 1 ... 3 {
2    if isOnGem {
3        collectGem()
4    } else {
5        moveForward()
6    }
7}
8turnLeft()
9for i in 1 ... 5 {
10    if isOnGem {
11        collectGem()
12    } else {
13        moveForward()
14    }
15}
16turnLeft()
17for i in 1 ... 3 {
18    if isOnGem {
19        collectGem()
20    } else {
21        moveForward()
22    }
23}
24turnLeft()
25for i in 1 ... 5 {
26    if isOnGem {
27        collectGem()
28    } else {
29        moveForward()
30    }
31}

Logical Operators

(1) Introduction

(2) Using the NOT Operator

段落圖片

The first time I solved this puzzle, I didn't use the NOT operator as the task suggested. Instead, I used other methods to achieve the same result. The system is smart enough to detect whether you've used the intended learning method. If you use a different one, it will respond: 'Congratulations! You found a creative solution!'

1for i in 1 ... 4 {
2    moveForward()    
3    if isOnGem {
4        collectGem()        
5        }else {
6        turnLeft()
7        moveForward()
8        moveForward()
9        collectGem()
10        turnLeft()
11        turnLeft()
12        moveForward()
13        moveForward()
14        turnLeft()  
15}
段落圖片

The image above shows the second solution: 'If you're not on a gem, do this instead.'

1for i in 1 ... 4 {
2    moveForward()    
3    if !isOnGem {
4        turnLeft()
5        moveForward()
6        moveForward()
7        collectGem()
8        turnLeft()
9        turnLeft()
10        moveForward()
11        moveForward()
12        turnLeft() 
13        }
14    else {
15        collectGem()      
16      }
17}

Variables

(1) Introduction

Variables are used to store information.

1var name = "Mia"
2var age = 19

To create a new variable, use the keyword `var`, followed by a new variable name and a value. Once created, the type of data the variable holds will never change.

The assignment operator (=) sets the value of a variable.

`name` stores a String (text within quotation marks).

`age` stores an Int (an integer or whole number).

(2) Keeping Track

段落圖片
1var gemCounter = 0
2moveForward()
3moveForward()
4collectGem()
5gemCounter = +1

(3) Increasing a Value

段落圖片
1var gemCounter = 0
2moveForward()
3for i in 1 ... 7 {
4    if isOnGem {
5        collectGem() 
6        gemCounter = +1
7        moveForward()        
8    }else {
9        moveForward()      
10      }
11}
12turnRight()
13for i in 1 ... 2 {
14    if isOnGem {
15        collectGem() 
16        gemCounter = +1
17        moveForward()        
18    }else {
19        moveForward()   
20         }
21}
22turnRight()
23for i in 1 ... 7 {
24    if isOnGem {
25        collectGem()  
26        gemCounter = +1
27        moveForward()    
28        }else {
29        moveForward()
30            }
31}

Types

(1) Introduction

A type is like a blueprint of a house—it defines the characteristics and behaviors. Within a type, functionalities are called 'properties' (variables defined inside the type), and actions are called 'methods' (functions defined inside the type).

(2) Deactivating a Portal

段落圖片
1greenPortal.isActive = true
2 
3moveForward()
4moveForward()
5while greenPortal.isActive {
6    greenPortal.isActive = false
7    moveForward()
8}
9for i in 1 ... 3 {
10    turnRight()
11    moveForward()
12    moveForward()
13    moveForward()
14    toggleSwitch()
15    turnRight()
16    turnRight()
17    moveForward()
18    moveForward()
19    moveForward() 
20}

(3) Setting the Correct Portal

段落圖片
1moveForward()
2 
3for i in 1 ... 6 {
4    moveForward() 
5    if  isOnGem {
6        collectGem()
7    }else {
8        while isBlocked {
9            turnRight()
10            turnRight()
11            while bluePortal.isActive {
12                bluePortal.isActive = false
13            }
14        }
15    }
16}
17bluePortal.isActive = true
18moveForward()
19moveForward()
20for i in 1 ... 2 {
21    moveForward() 
22    if  isOnGem {
23        collectGem()
24    }else {
25        while isBlocked {
26            turnRight()
27            turnRight()
28            moveForward()            
29            }
30        }
31    }
32bluePortal.isActive = false
33moveForward()
34turnRight()
35turnRight()
36pinkPortal.isActive = false
37moveForward()
38moveForward()
39collectGem()

Initialization

(1) Introduction

The portal is of type `Portal`, and our character is of type `Character`. Now we can control a new type called `Expert`, which has a new method (action) called `turnLockUp()`.

段落圖片段落圖片

(2) Initialize Your Expert

段落圖片
1let expert = Expert()
2for i in 1 ... 3 {
3    expert.moveForward()
4}
5expert.turnLockUp()
6while expert.isBlocked {
7    expert.turnRight()
8    expert.turnRight()
9    expert.moveForward()
10    expert.moveForward()
11    expert.moveForward()
12}
13expert.turnLeft()
14for i in 1 ... 3 {
15    expert.moveForward()
16    expert.moveForward()
17    expert.moveForward()
18    expert.moveForward()
19    expert.collectGem()
20    while expert.isBlocked {
21        expert.turnRight()
22        expert.turnRight()
23        expert.moveForward()
24        expert.moveForward()
25        expert.moveForward()
26    }
27    expert.turnLeft()    
28}

Functions

(1) Introduction

(2) Collect, Toggle, Repeat

段落圖片
1func doIt(){
2    collectGem()
3    moveForward()
4    toggleSwitch()
5    }
6    moveForward()
7    doIt()
8    moveForward()
9    turnLeft()
10    moveForward()    
11    doIt()
12    moveForward()
13    moveForward()    
14    turnLeft()
15    moveForward()    
16    doIt()
17    moveForward()
18    turnLeft()
19    moveForward()
20    doIt()

Parameters

(1) Introduction

Parameters = options you can define. A function can include multiple parameters.

段落圖片段落圖片

(2) Keep Moving Forward

段落圖片
1let expert = Expert()
2    func move(distance: Int){
3    for i in 1 ... distance{
4        expert.moveForward()
5    }
6    }
7    expert.move(distance:6)
8expert.turnRight()
9expert.move(distance:2)
10expert.turnRight()
11for i in 1 ... 2 {
12    expert.move(distance:5)
13    expert.turnLeft()
14}
15expert.turnLockUp()
16expert.turnLeft()
17for i in 1 ... 2 {
18    
19    expert.move(distance:3)
20    expert.turnRight()
21}
22expert.move(distance:4)
23expert.collectGem()

(3) Placing at Specific Positions

段落圖片
1let expert = Expert()
2world.place(expert, atColumn: 1, row: 1)
3expert.collectGem()
4world.place(expert, atColumn: 6, row: 1)
5expert.collectGem()
6world.place(expert, atColumn: 1, row: 6)
7expert.collectGem()
About Author