2022年2月18日 星期五

學習程式設計1_10_收集、切換、重複


Suggest_Code_01

moveForward()
collectGem()
moveForward()
toggleSwitch()
moveForward()
turnLeft()
moveForward()
collectGem()
moveForward()
toggleSwitch()
moveForward()
moveForward()
turnLeft()
moveForward()
collectGem()
moveForward()
toggleSwitch()
moveForward()
turnLeft()
moveForward()
collectGem()
moveForward()
toggleSwitch()

Result: OK

上述步驟是一步一步走完的程式碼,相當冗長,透過設定函數可以節省版面和編輯時間。觀察下來,可以發現其中4個步驟可以湊成一個循環,這個重複的步驟就可以設定成一個函數。


Suggest_Code_02

func mFcGmFtS(){
    moveForward()
    collectGem()
    moveForward()
    toggleSwitch()
}
mFcGmFtS()
moveForward()
turnLeft()
mFcGmFtS()
moveForward()
moveForward()
turnLeft()
mFcGmFtS()
moveForward()
turnLeft()
mFcGmFtS()

Result: OK

我將 moveForward()、collectGem()、moveForward()、toggleSwitch() 這4個指令組合成一個函數,並命名成 func mFcGmFtS(){...},在最初逐步執行的程式碼中,將重複的部分以 func mFcGmFtS() 取代,可發現明顯節省了程式碼一些篇幅。




學習程式設計1_09_建立新的函數

Suggest_Code_01

func turnRight(){
turnLeft()
turnLeft()
turnLeft()
}
moveForward()
turnLeft()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnLeft()
moveForward()
toggleSwitch()

Result: OK


Suggest_Code_02

Func turnRight(){
turnLeft()
turnLeft()
turnLeft()
}
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
moveForward()
turnRight()
moveForward()
moveForward()
moveForward()
moveForward()
turnRight()
moveForward()
toggleSwitch()

Result: OK





Hint:
關卡內有3塊開關,只要將沒有發亮的開關打開即可,本來就亮著的開關不用再去做 toggleSwitch 的動作。

學習程式設計1_08_組合新的動作

Suggest_Code_01

func turnRight(){
    turnLeft()
    turnLeft()
    turnLeft()
}
moveForward()
moveForward()
moveForward()
turnRight()
moveForward()
moveForward()
moveForward()
collectGem()

Result: OK

學習程式設計1_函數


將現有的一些指令做組合並命名,使它成為一個新的動作(通常是連續一貫的指令),以方便呼叫並且執行。

func_Name(){
A()
B()
C()
}

在上面範例裡的大括弧內部加入一些指令,來定義此函數的行為。

Example:

func_右轉(){
左轉()
左轉()
左轉()
}

簡單來說,在一開始右轉這個函數透過3次左轉來定義,之後就可以直接呼叫右轉,而不用在程式還要輸入3次左轉

2022年2月15日 星期二

學習程式設計1_07_最短路線

Suggest_Code_01

moveForward()
moveForward()
moveForward()
collectGem()
turnLeft()
moveForward()
moveForward()
moveForward()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
toggleSwitch()

Result: It takes 10 steps.


Suggest_Code_02

moveForward()
moveForward()
moveForward()
collectGem()
moveForward()
moveForward()
moveForward()
moveForward()
toggleSwitch()

Result: It takes 7 steps, it is the shortest path.


Suggest_Code_03

turnLeft()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
moveForward()
moveForward()
moveForward()
toggleSwitch()
moveForward()
moveForward()
moveForward()
moveForward()
collectGem()

Result: It takes 11 steps.

學習程式設計1_06_除錯練習

Original code as follow:

moveForward()
moveForward()
moveForward()
turnLeft()
toggleSwitch()
moveForward()
moveForward()
moveForward()
collectGem()
moveForward()

Suggest code 01:

moveForward()
turnLeft()
moveForward()
moveForward()
toggleSwitch()
moveForward()
moveForward()
moveForward()
moveForward()
collectGem()

Result: OK

學習程式設計1_05_尋找並修正錯誤

Original code as follow:

moveForward()
turnLeft()
moveForward()
moveForward()
collectGem()
moveForward()
toggleSwitch()

Suggest code 01:

moveForward()
moveForward()
turnLeft()
moveForward()
collectGem()
moveForward()
toggleSwitch()

Result: OK

學習程式設計1_04_傳送門練習

Suggest code 01

moveForward()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
toggleSwitch()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
collectGem()

Result: OK

學習程式設計1_03_切換開關

Suggest_Code_01

moveForward()
moveForward()
turnLeft()
moveForward()
collectGem()
moveForward()
turnLeft()
moveForward()
moveForward()
toggleSwitch()

Result: OK

Hint: 在這一關卡裡,除了收集寶石以外,開關也要切換成發亮的狀態才算完成。

學習程式設計1_02_加入新指令

Suggest_Code_01

moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
collectGem()

Result: OK

學習程式設計1_01_發出指令

Suggest_Code_01

moveForward()
moveForward()
moveForward()
collectGem()

Result: OK

學習程式設計1_10_收集、切換、重複

Suggest_Code_01 moveForward() collectGem() moveForward() toggleSwitch() moveForward() turnLeft() moveForward() collectGem() moveForward() to...