Advertisement
hevohevo

CC: exercise10_3_receive

May 21st, 2016
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.20 KB | None | 0 0
  1. -- exercise10_3_receive (「rc_receive1」プログラムを改造しました)
  2. -- タートルラジコン(タートル側)
  3. -- 左手側にツルハシ、右手側にモデムを装着しているタートルを想定
  4.  
  5. -- Functions
  6. function strafeRight()
  7.   turtle.turnRight()
  8.   turtle.forward()
  9.   turtle.turnLeft()
  10. end
  11.  
  12. function strafeLeft()
  13.   turtle.turnLeft()
  14.   turtle.forward()
  15.   turtle.turnRight()
  16. end
  17.  
  18. -- Condition Table(Luaテーブルに関数を入れることで条件分岐を実現)
  19. local cond = {}
  20. cond["strafe_left"] = strafeLeft  -- "strafe_left"メッセージが来たらstrafeLeft関数実行
  21. cond["strafe_right"] = strafeRight
  22. cond["go_forward"] = turtle.forward
  23. cond["go_back"] = turtle.back
  24. cond["dig"] = turtle.dig
  25. cond["place"] = turtle.place
  26.  
  27. -- Main
  28. rednet.open("right")  -- 右側に設置したモデムを使うという宣言
  29.  
  30. while true do
  31.   local id, msg = rednet.receive()  -- メッセージを待ち受ける
  32.   print(id, msg)
  33.  
  34.   if cond[msg] then  -- cond[msg]で関数を取り出すことができたら、
  35.     cond[msg]()  -- その関数を実行
  36.   else  -- cond[msg]で関数を取り出せなかったら、
  37.     print("wrong")
  38.   end
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement