Home | Posts RSS | Comments RSS | Login

breed

2010-01-25
因為種種原因(多數是因天資不夠聰穎 orz)
所以跳槽選了個相似的語言:LOGO語言來做。

LOGO語言有不少軟體可以做程式設計的工作,其中最有名的就是:MSWLogo。只是這款軟體只限於windows平台上執行,又源於種種原因,去找了Netlogo來用。

Netlogo是採用LGPL的條款,詳見


以下、則是為了搞清楚breed的作用所寫的小程式:)







使用Netlogo寫程式,不同功用的語法會標上不同的顏色,
因為這邊偷懶,所以就不加上顏色區分了:)
唯一加上顏色的是註解這樣。

而這個程式,是設定三種不同的breed,撞上藍色的牆之後會執行不同的動作。
butterfly撞上左右兩邊的牆會變褐色 (brown),撞上上下兩邊的牆會變綠色 (turquoise)
cow撞上牆之後,會從四個角落重新出發
sheep撞上左右兩邊的牆之後,會畫出所走的路徑,撞上上下兩邊的牆則不會畫出路徑


breed [butterflies butterfly] ; This is a built-in turtle and link variable.
breed [cows cow]
breed [sheeps sheep]

to setup ; 設定初始值
 ca ; clear-all
 draw-walls ; 繪製藍色的牆
 set-default-shape butterflies "butterfly" ; 設定breed的圖形
 set-default-shape cows "cow"
 set-default-shape sheeps "sheep"

 create-butterflies 3 [ ; 建立三個butterfly
  set color pink ; 設定butterfly的初始顏色
  set heading random 360 ; 設定butterfly方向以亂數決定
  randomize ; 設定butterfly初始位置以亂數決定
 ]
 create-cows 3 [ ; 建立三個cow
  set color gray
  set heading random 360
  randomize
 ]
 create-sheeps 3 [ ; 建立三個sheep
  set color white
  set heading random 360
  randomize
 ]
end

to draw-walls ; 繪製藍色的牆
 ask patches with [abs pxcor >= max-pxcor] ; 繪製左右兩邊的牆、abs是絕對值
  [ set pcolor blue ]
 ask patches with [abs pycor >= max-pycor] ; 繪製上下兩邊的牆
  [ set pcolor blue ]
end

to randomize ; 讓breed的初始位置可以亂數決定
 setxy random-xcor random-ycor ; 以亂數的方式設立breed的初始位置
 if any? patches in-radius 1 with [pcolor = blue] ; 如果breed的位置是牆,則重新設立初始位置,直到breed跟牆沒有重疊為止
  [ randomize ]
end

to go ; 設定三種breed的動作
 ask butterflies [
  fd 0.02 ; forward
  if abs [pxcor] of patch-ahead 0.1 >= max-pxcor [ ; 如果撞上左右兩邊的牆,則往反方向彈起,並變成褐色 (brown)
   set heading (- heading)
   set color brown
  ]
  if abs [pycor] of patch-ahead 0.1 >= max-pycor [ ; 如果撞上上下兩邊的牆,則往反方向彈起,並變成褐色 (turquoise)
   set heading (180 - heading)
   set color turquoise
  ]
 ]

 ask cows [ ; 設定撞上四邊的牆,就會由四個角落重新開始走
  fd 0.005 ; forward
  if abs [pxcor] of patch-ahead 0.1 >= max-pxcor [
   set heading (- heading)
   ifelse pycor < 0
    [ set ycor ((- (xcor ^ 2)) / max-pycor) ]
    [ set ycor ((xcor ^ 2) / max-pycor) ]   ]
  if abs [pycor] of patch-ahead 0.1 >= max-pycor [
   set heading (180 - heading)
   ifelse pxcor < 0
    [ set xcor ((- (ycor ^ 2)) / max-pxcor) ]
    [ set xcor ((ycor ^ 2) / max-pxcor) ]
  ]
 ]

 ask sheeps [
  fd 0.01 ; forward
  if abs [pxcor] of patch-ahead 0.1 = max-pxcor [ ; 如果撞上左右兩邊的牆,則往反方向彈起,並畫下路徑
   set heading (- heading)
   pd ; pen-down
  ]
  if abs [pycor] of patch-ahead 0.1 = max-pycor [ ; 如果撞上上下兩邊的牆,則往反方向彈起,且不畫下路徑
   set heading (180 - heading)
   pu ; pen-up
  ]
 ]

 tick ; Advances the tick counter by one.
end



簡單來說,breed是一種可以一次創造出很多turtle、又可以執行不同動作的東西。

0 意見 to breed:

張貼留言