task4 <<
Previous Next >> Challenge
Reeborg
假如利用相對路徑呼叫 URL 變數值:
https://mdewcm2025.github.io/hw-scrum-1/reeborg/?lang=en&mode=python&menu=worlds/menus/select_collection_en.json&name=Alone&url=worlds/tutorial_en/harvest1.json&editor=python/harvest1.py
表示:
lang=en
mode=python
menu=worlds/menus/select_collection_en.json
name=Alone
url=worlds/tutorial_en/harvest1.json
editor=python/harvest1.py
https://mdewcm2025.github.io/hw-scrum-1/reeborg
https://github.com/mdewcm2025/hw-scrum-1/blob/main/reeborg/rb/reeborg.js
function set_editor() {
"use strict";
if (localStorage.getItem("editor")){
editor.setValue(localStorage.getItem("editor"));
} else {
editor.setValue(RUR.translate("move") + "()");
}
}
決定 editor 中的內容
在 index.html 中則 editor 內容由 id="code" 變數決定
Reeborg's World 是一個教育平台,用於學習 Python 程式設計,特別是初學者,透過控制一個虛擬機器人來完成任務。以下是其程式架構的逐步解析:
1. Reeborg's World 的基本概念
Reeborg 是一個虛擬機器人,存在於一個由格子組成的世界中。這個世界可能包含牆壁、目標物(如星星或三角形)、障礙物等。你的任務是透過撰寫 Python 程式來控制 Reeborg,讓它完成特定目標,例如移動到某個位置、撿起物品或繞過障礙。
程式架構的核心是:
- 指令:Reeborg 能理解的基本動作(如移動、轉向)。
- 條件判斷:根據環境決定動作(如檢查前方是否有牆)。
- 迴圈與函數:用來重複動作或組織程式碼。
2. 基本指令
Reeborg 的程式依賴一組預定義的函數來控制其行為。以下是常用的基本指令:
move()
:讓 Reeborg 向前移動一格。
turn_left()
:讓 Reeborg 向左轉 90 度。
pick_object()
:撿起當前格子上的物品(如果有)。
put_object()
:放下 Reeborg 攜帶的物品。
front_is_clear()
:檢查前方是否沒有牆壁(返回 True 或 False)。
at_goal()
:檢查 Reeborg 是否到達目標位置(返回 True 或 False)。
這些指令是程式的基礎,透過組合它們可以完成簡單任務。例如:
move()
move()
turn_left()
move()
這段程式讓 Reeborg 向前走兩步,向左轉,再向前走一步。
3. 控制結構
為了讓 Reeborg 能應對更複雜的情況,需要使用 Python 的控制結構:
(1) 條件語句
使用 if
、elif
和 else
根據環境條件執行不同動作。例如:
if front_is_clear():
move()
else:
turn_left()
這段程式檢查前方是否暢通,如果是就前進,否則左轉。
(2) 迴圈
使用 while
或 for
來重複動作。例如:
while not at_goal():
move()
這段程式讓 Reeborg 一直向前走,直到到達目標。
(3) 自訂函數
為了讓程式更模組化,可以定義自己的函數。例如:
def turn_right():
turn_left()
turn_left()
turn_left()
move()
turn_right()
move()
這裡定義了一個
turn_right()` 函數,透過三次左轉實現右轉。
4. 典型程式架構
一個完整的 Reeborg 程式通常包含以下部分:
- 初始化:設定起始條件(通常由環境提供,無需額外程式碼)。
- 主邏輯:使用迴圈、條件和函數來解決問題。
- 結束條件:確保程式在達到目標時停止。
例如,假設任務是讓 Reeborg 沿著牆走並到達目標:
def turn_right():
turn_left()
turn_left()
turn_left()
while not at_goal():
if front_is_clear():
move()
else:
turn_left()
這個程式實現了「靠右走」策略:如果前方暢通就前進,否則左轉。
5. 進階功能
Reeborg 還支援一些進階功能,視任務需求而定:
- 感測器:如
right_is_clear()
(檢查右方是否暢通)、carries_object()
(檢查是否攜帶物品)。
- 多目標任務:需要結合多個條件,例如撿起所有物品並到達終點。
- 除錯工具:Reeborg's World 提供步進執行和視覺化介面,幫助理解程式如何運作。
6. 範例:繞過障礙物
假設 Reeborg 需要繞過一個方形障礙物到達目標:
def turn_right():
turn_left()
turn_left()
turn_left()
while not at_goal():
if front_is_clear():
move()
elif right_is_clear():
turn_right()
move()
else:
turn_left()
這個程式讓 Reeborg 在遇到障礙時嘗試右轉,否則左轉,形成一個簡單的路徑尋找策略。
7. 設計程式的思考流程
撰寫 Reeborg 程式時,可以遵循以下步驟:
- 理解目標:明確任務要求(如到達某點、收集物品)。
- 觀察環境:檢查世界中的牆壁、物品和起點終點。
- 分解問題:將任務拆成小步驟。
- 測試與調整:在 Reeborg's World 中運行程式,根據結果修改。
maze example:
def turn_right():
turn_left()
turn_left()
turn_left()
while True:
if at_goal() and wall_in_front():
turn_left()
turn_left()
break
if right_is_clear() and front_is_clear():
move()
if front_is_clear() and wall_on_right():
move()
if wall_in_front() and right_is_clear() and not at_goal():
turn_right()
move()
if wall_in_front() and not right_is_clear():
turn_left()
if right_is_clear() and not at_goal():
turn_right()
move()
or based upon:
while not at_goal():
if right_is_clear():
# do something; likely more than one line of code
elif front_is_clear():
# do something else; perhaps a single line of code
else:
# do something else; perhaps a single line of code
https://aroberge.gitbooks.io/reeborg-s-world-advanced-world-creation/content/
https://github.com/b-tomi/100DaysOfCode/
https://www.youtube.com/watch?v=9YDyVwoYJ8k
task4 <<
Previous Next >> Challenge