cp2025 計算機程式

  • Home
    • SMap
    • reveal
    • blog
  • About
    • cs101
    • Computer
      • llama
      • nginx
    • AI
      • QandA
      • Teams
    • Homework
  • Topics
    • Git
      • Git_ex1
      • Git_ex2
    • Python
      • SE
      • CL
      • Loops
      • Recursion
      • SA
      • SST
      • Maze
      • Collect
      • GT
      • Python_ex1
    • Javascript
      • HTML and CSS
    • Project
      • Waitress
      • API
  • Brython
    • Brython_ex
    • Robot_ex
  • Ref
    • Reeborg
      • ex1
      • Otto_ninja
    • tkinter
    • Pyodide
    • Pyodide_ex
    • Pyodide2
      • robot.py
      • Example2
    • Pyodide3
      • png_files
      • Harvest
Git << Previous Next >> Git_ex2

Git_ex1

Reeborg 機器人 Python 開發流程與 Git/GitHub 協作範例

階段一:基礎 Python 與 Reeborg 指令

初學者可直接用 Python 撰寫簡單的 Reeborg 機器人程式,例如讓機器人向前走數步、左轉、撿物件:

move()
move()
turn_left()
move()
if object_here():
    take()

階段二:加入函式與三人協同開發

協作情境:三位組員(A、B、C)各自開發一段行走任務,然後將重複動作寫成函式,合併到專案。
  1. 組員 A: 讓機器人前進 3 格
    for i in range(3):
        move()
    
  2. 組員 B: 機器人轉 2 次左
    for i in range(2):
        turn_left()
    
  3. 組員 C: 機器人檢查物件並撿取
    if object_here():
        take()
    

協作流程:三人各自建立分支(feature/A, feature/B, feature/C),在 GitHub 上合併 Pull Request,最後由主分支整合。

Git 基本指令與說明

  • Step 1:初始化本地倉庫
    git init
        
    在專案資料夾裡執行,建立 Git 管理的本地倉庫。
  • Step 2:加入遠端 GitHub 倉庫
    git remote add origin https://github.com/你的帳號或組織/專案名稱.git
        
    將本地倉庫連結到 GitHub 上的遠端 repository。
  • Step 3:建立並切換到功能分支
    git checkout -b feature/A        # 組員A
    git checkout -b feature/B        # 組員B
    git checkout -b feature/C        # 組員C
        
    每位組員建立自己的功能分支,分工開發。
  • Step 4:修改程式並加入暫存區
    git add reeborg.py
        
    將變更過的檔案加入暫存區,準備提交。
  • Step 5:提交更動
    git commit -m "描述這次開發的內容"
        
    將暫存區的變更提交到本地倉庫。
  • Step 6:推送分支到 GitHub
    git push -u origin feature/A        # 組員A
    git push -u origin feature/B        # 組員B
    git push -u origin feature/C        # 組員C
        
    把每個人的分支推到遠端 GitHub。
  • Step 7:建立 Pull Request 並審查合併
    到 GitHub 網站上建立 Pull Request(PR),請其他組員 code review,審查無誤後合併到主分支(main)。
  • Step 8:將最新主分支同步回本地
    git checkout main
    git pull
        
    確保本地 main 跟遠端同步。

加入共用函式

def turn(times):
    for i in range(times):
        turn_left()

def new_move(steps):
    for i in range(steps):
        move()
階段三:重構與加入迴圈、條件與函式應用

將重複採收動作寫成 harvest_one_row() 函式,結合前進與轉彎,流程更清晰。

def harvest_one_row():
    while object_here():
        take()
    else:
        move()

階段四:導入 class,建立泛用 Reeborg 機器人控制架構

class ReeborgBot:
    def turn(self, times):
        for i in range(times):
            turn_left()
    def move_steps(self, steps):
        for i in range(steps):
            move()
    def harvest_one_row(self):
        while object_here():
            take()
        else:
            move()
    def harvest_field(self, rows, row_length):
        for i in range(rows):
            for j in range(row_length):
                self.harvest_one_row()
            if i != rows - 1:
                self.turn(3)
                self.move_steps(1)
                self.turn(3)
                self.move_steps(1)

# 使用範例
bot = ReeborgBot()
bot.move_steps(2)
turn_left()
bot.move_steps(2)
bot.harvest_field(3, 6)
協作開發流程與 Git/GitHub 應用摘要
  1. git init:建立本地專案。
  2. git branch feature/A、git branch feature/B、git branch feature/C:三位組員各自建立分支(或用 git checkout -b 一次完成)。
  3. git add、git commit、git push:將各自更動上傳到 GitHub。
  4. Pull Request:在 GitHub 建立 PR,進行 code review 與合併。
  5. 遇重複邏輯,抽象為函式,用新分支推送與合併。
  6. 最後導入 class,建立泛用控制架構,提升可維護性與擴充性。

結語

本例展示 Python 基礎語法、函式、類別應用,以及如何善用 Git/GitHub 進行協同開發,讓團隊程式碼更有架構、易於管理與擴展。


Git << Previous Next >> Git_ex2

Copyright © All rights reserved | This template is made with by Colorlib