> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swift-playgrounds/learn-to-code-2/pages/types/deactivating-a-portal.md).

# 關閉傳送門

Deactivating a Portal

這個關卡要求我們用 `portal.isActive = false` 的方式來關閉一個開關。這種用：

* `object.property`
* `object.method()`

的方式來使用物件的**屬性**，或呼叫物件的**方法**，稱為 **dot notation**。

![](https://1830103165-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M5-JmwCZMKh_d7RfBaN%2F-M5UeRfBE-Livrh6AZKR%2F-M5UefMmY630DHer6ycR%2Fdeactive%20portal.PNG?alt=media\&token=b4543900-01cd-4d5d-95ba-52cc5a3d65c7)

{% tabs %}
{% tab title="main" %}

```swift
// 關閉傳送門
greenPortal.close()

// 打開的開關數量
var switches = 0

// 如果打開的開關不到 3 個，就繼續走。
while switches < 3 {
    walk(along: .left) { 
        if isOnClosedSwitch { 
            toggleSwitch()
            switches += 1
        }
    }
}
```

{% endtab %}

{% tab title="Portal" %}

```swift
extension Portal {
    func close() { isActive = false }
}
```

{% endtab %}

{% tab title="navigation" %}

```swift
// 貼著哪邊走的選項
enum Side {
    case left, right
}

// 將「左右邊被擋住」的狀況放到同一個函數中處理。
func isBlockedOn(_ side: Side) -> Bool {
    side == .left ? isBlockedLeft : isBlockedRight
}

// 將「向左右走」放到同一個函數中處理。
func turn(_ side: Side) {
    if side == .left { turnLeft() }
    else { turnRight() }
}

// 走反方向
func turnOtherSide(from side: Side) {
    if side == .left { turnRight() }
    else { turnLeft() }
}

// ⭐️ 沿著某一邊走的策略，
//    將來可以寫入 Actor 的 extension 中。
func walk(along side: Side = .left, handle:()->Void = {}) {
    if !isBlockedOn(side) { // 如果指定邊沒擋住，就走這一邊
        turn(side)
        moveForward()
        handle()
    } else if !isBlocked {  // 不然如果前面沒擋住，就走前面
        moveForward()
        handle()
    } else {                // 否則就轉向另一邊
        turnOtherSide(from: side)
    }
}
```

{% endtab %}
{% endtabs %}
