Advertisement
hevohevo

CC: exercise10_4_receive

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