Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- exercise10_3_receive (「rc_receive1」プログラムを改造しました)
- -- タートルラジコン(タートル側)
- -- 左手側にツルハシ、右手側にモデムを装着しているタートルを想定
- -- Functions
- function strafeRight()
- turtle.turnRight()
- turtle.forward()
- turtle.turnLeft()
- end
- function strafeLeft()
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- end
- -- Condition Table(Luaテーブルに関数を入れることで条件分岐を実現)
- local cond = {}
- cond["strafe_left"] = strafeLeft -- "strafe_left"メッセージが来たらstrafeLeft関数実行
- cond["strafe_right"] = strafeRight
- cond["go_forward"] = turtle.forward
- cond["go_back"] = turtle.back
- cond["dig"] = turtle.dig
- cond["place"] = turtle.place
- -- Main
- rednet.open("right") -- 右側に設置したモデムを使うという宣言
- while true do
- local id, msg = rednet.receive() -- メッセージを待ち受ける
- print(id, msg)
- if cond[msg] then -- cond[msg]で関数を取り出すことができたら、
- cond[msg]() -- その関数を実行
- else -- cond[msg]で関数を取り出せなかったら、
- print("wrong")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement