マイクロマウスシミュレーター

マイクロマウスシミュレーションの使い方について。

948 views

サンプルプログラム

左手法で迷路を走行するサンプルプログラムです。

  1. # coding:UTF-8
  2. import sys
  3. from MazeSimulator.MouseClient import Mouse, MouseError
  4. # 左手法で迷路を探索する
  5. def left_run(mouse):
  6. sensor = mouse.sensor()
  7. left = sensor[0]
  8. front = sensor[1]
  9. right = sensor[2]
  10. if left == 0 and front == 0 and right == 0:
  11. # 壁なし
  12. ret = mouse.turn_left()
  13. print(ret)
  14. ret = mouse.go_straight()
  15. print(ret)
  16. elif left == 0 and front == 0 and right == 1:
  17. # 左と前がOK
  18. ret = mouse.turn_left()
  19. print(ret)
  20. ret = mouse.go_straight()
  21. print(ret)
  22. elif left == 0 and front == 1 and right == 0:
  23. # 前だけNG
  24. ret = mouse.turn_left()
  25. print(ret)
  26. ret = mouse.go_straight()
  27. print(ret)
  28. elif left == 0 and front == 1 and right == 1:
  29. # 左だけOK
  30. ret = mouse.turn_left()
  31. print(ret)
  32. ret = mouse.go_straight()
  33. print(ret)
  34. elif left == 1 and front == 0 and right == 0:
  35. # 前と右がOK
  36. ret = mouse.go_straight()
  37. print(ret)
  38. elif left == 1 and front == 0 and right == 1:
  39. # 前だけOK
  40. ret = mouse.go_straight()
  41. print(ret)
  42. elif left == 1 and front == 1 and right == 0:
  43. # 右だけOK
  44. ret = mouse.turn_right()
  45. print(ret)
  46. ret = mouse.go_straight()
  47. print(ret)
  48. else:
  49. # 全部壁
  50. ret = mouse.turn_right()
  51. print(ret)
  52. ret = mouse.turn_right()
  53. print(ret)
  54. ret = mouse.go_straight()
  55. print(ret)
  56. if mouse.is_goal() == 1:
  57. print("ゴールしました")
  58. if __name__ == '__main__':
  59. # 本サイト
  60. mouse = Mouse("APIキー")
  61. map_id = 1
  62. try:
  63. mouse.start(map_id)
  64. except MouseError as e:
  65. print(e)
  66. sys.exit(1)
  67. for i in range(mouse.max_turn):
  68. while True:
  69. try:
  70. left_run(mouse)
  71. except MouseError as e:
  72. print(e)
  73. break
  74. print("ターンオーバー")

Page 6 of 7.

前のページ 次のページ



[添付ファイル]


お問い合わせ

プロフィール

すぺぺぺ

自己紹介

本サイトの作成者。
プログラムは趣味と勉強を兼ねて、のんびり本サイトを作っています。
フレームワークはdjango。
ChatGPTで自動プログラム作成に取り組み中。

サイト/ブログ

https://www.osumoi-stdio.com/novel/

ツイッター

@darkimpact0626