Git_ex1 <<
Previous Next >> Python
Git_ex2
三人協同開發 Reeborg 程式:Git/GitHub 詳細流程與指令
專案設計演進與協作分工
- 階段一:初始設計(三人各自撰寫基本功能)
- Alice:負責機器人移動與左轉基本動作
- Bob:設計撿取物件的邏輯
- Carol:設計主程式流程與方向判斷
# Alice
move()
move()
turn_left()
move()
# Bob
if object_here():
take()
# Carol
while not is_facing_north():
turn_left()
git 指令:
git checkout -b feature/move # Alice
git checkout -b feature/take # Bob
git checkout -b feature/dir # Carol
git add reeborg.py
git commit -m "初步功能"
git push -u origin feature/move # Alice
git push -u origin feature/take # Bob
git push -u origin feature/dir # Carol
- 階段二:重複迴圈設計(逐步將動作包裝為迴圈)
- Alice:將移動與轉向包裝進 for 迴圈
- Bob:將撿取物件行為放入 while 迴圈
# Alice
for i in range(3):
move()
for i in range(2):
turn_left()
# Bob
while object_here():
take()
git 指令:
git add reeborg.py
git commit -m "加入重複迴圈"
git push
- 階段三:Function 封裝設計(將重複邏輯抽象為函式)
- Alice:設計
new_move
、turn
、turn_right
- Bob:設計
harvest_one_row
,用 global 記錄物件數
- Carol:設計
is_facing_east
與主流程
def turn(n):
for i in range(n):
turn_left()
def turn_right():
for i in range(3):
turn_left()
def new_move(n):
for i in range(n):
move()
def harvest_one_row():
global object_taken
while object_here() and object_taken < 36:
take()
object_taken += 1
if object_taken < 36:
move()
def is_facing_east():
count = 0
while not is_facing_north():
turn_left()
count += 1
if count == 4:
break
turn(3)
return True
git 指令:
git add reeborg.py
git commit -m "完成 function 封裝"
git push
- 階段四:導入 class-based 設計(最終彈性架構)
- Alice、Bob、Carol 協作將函式整合為 class,提升可維護性
class ReeborgBot:
def __init__(self):
self.object_taken = 0
def turn(self, n):
for i in range(n):
turn_left()
def turn_right(self):
for i in range(3):
turn_left()
def new_move(self, n):
for i in range(n):
move()
def harvest_one_row(self):
while object_here() and self.object_taken < 36:
take()
self.object_taken += 1
if self.object_taken < 36:
move()
def is_facing_east(self):
count = 0
while not is_facing_north():
turn_left()
count += 1
if count == 4:
break
self.turn(3)
return True
def harvest_field(self):
self.new_move(2)
self.turn(1)
self.new_move(2)
done = False
while self.is_facing_east() and not done:
for _ in range(2):
for _ in range(6):
self.harvest_one_row()
if self.object_taken >= 36:
done = True
break
if done: break
for _ in range(2):
self.turn(1)
move()
for _ in range(6):
self.harvest_one_row()
if self.object_taken >= 36:
done = True
break
if done: break
for _ in range(2):
self.turn_right()
move()
if self.object_taken >= 36:
print("task completed!")
break
# 使用範例
bot = ReeborgBot()
bot.harvest_field()
git 指令:
- 協作於 feature/class-based 分支整合 class 架構
git checkout -b feature/class-based
# 合併 Alice/Bob/Carol 分支或直接在此分支重構
git add reeborg.py
git commit -m "整合 class-based 架構"
git push -u origin feature/class-based
專案開發分工摘要表
組員 |
負責內容 |
Git 分支名稱 |
Alice |
基本移動與轉向(new_move 、turn 、turn_right ) |
feature/move-turn |
Bob |
收成邏輯(harvest_one_row 、object_taken ) |
feature/harvest |
Carol |
方向判斷與主流程(is_facing_east 、主執行流程) |
feature/main-loop |
GitHub 操作 Step by Step
- 由一位組員(如 Alice)新建 repo(如
reeborg-harvest
)
- 本地初始化:
git clone https://github.com/your-org/reeborg-harvest.git
cd reeborg-harvest
- 建立主分支 main:
git checkout -b main
git push -u origin main
- 三位組員各自分出功能分支:
git checkout -b feature/move-turn # Alice
git checkout -b feature/harvest # Bob
git checkout -b feature/main-loop # Carol
- 推送分支到遠端:
git push -u origin feature/move-turn # Alice
git push -u origin feature/harvest # Bob
git push -u origin feature/main-loop # Carol
- 組員於各自分支開發程式並提交:
git add reeborg.py
git commit -m "功能/重構/修正說明"
git push
- 建立 Pull Request(PR)並 Code Review:
- 於 GitHub 建立 PR,標明合併分支、審查人員、功能說明
- 進行 code review,提出建議與修正
- 審查通過後合併 PR(Merge pull request)
- main 分支同步至本地:
git checkout main
git pull
後續協作建議
- 每次新增功能或修正前,請先拉新分支,勿直接在 main 分支作業:
git checkout -b feature/新功能
git pull origin main
# 如有衝突,解決後再
git add .
git commit -m "解決衝突"
git push
- 在 Pull Request 詳細描述修改內容與測試方式,方便審查。
- 善用
git log
、git status
、git diff
檢查歷史與變更。
常用 Git 指令說明
git checkout -b 分支名
:從目前分支新建並切換到分支
git add 檔名
:加入暫存區,準備提交
git commit -m "訊息"
:提交更動,寫下本次修改說明
git push -u origin 分支名
:推送分支到遠端
git pull
:拉下遠端最新更動並合併
git merge 分支名
:將指定分支合併到目前分支
git status
:顯示目前狀態、尚未提交的變更
git log
:查詢提交歷史
git diff
:顯示檔案內容變更差異
結論
本範例展現 Reeborg 機器人從最簡單設計、逐步進化至 class-based 架構的過程,以及三人協同開發的 Git/GitHub 分支工作流。遵循此流程能大幅提升團隊協作、程式碼品質與可維護性。
Git_ex1 <<
Previous Next >> Python