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

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

972 views

サンプルプログラム

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

# coding:UTF-8
import sys
from MazeSimulator.MouseClient import Mouse, MouseError

# 左手法で迷路を探索する
def left_run(mouse):
    sensor = mouse.sensor()
    left = sensor[0]
    front = sensor[1]
    right = sensor[2]
    if left == 0 and front == 0 and right == 0:
        # 壁なし
        ret = mouse.turn_left()
        print(ret)
        ret = mouse.go_straight()
        print(ret)

    elif left == 0 and front == 0 and right == 1:
        # 左と前がOK
        ret = mouse.turn_left()
        print(ret)
        ret = mouse.go_straight()
        print(ret)

    elif left == 0 and front == 1 and right == 0:
        # 前だけNG
        ret = mouse.turn_left()
        print(ret)
        ret = mouse.go_straight()
        print(ret)

    elif left == 0 and front == 1 and right == 1:
        #  左だけOK
        ret = mouse.turn_left()
        print(ret)
        ret = mouse.go_straight()
        print(ret)

    elif left == 1 and front == 0 and right == 0:
        # 前と右がOK
        ret = mouse.go_straight()
        print(ret)

    elif left == 1 and front == 0 and right == 1:
        # 前だけOK
        ret = mouse.go_straight()
        print(ret)


    elif left == 1 and front == 1 and right == 0:
        # 右だけOK
        ret = mouse.turn_right()
        print(ret)
        ret = mouse.go_straight()
        print(ret)
    else:
        # 全部壁
        ret = mouse.turn_right()
        print(ret)
        ret = mouse.turn_right()
        print(ret)
        ret = mouse.go_straight()
        print(ret)

    if mouse.is_goal() == 1:
        print("ゴールしました")


if __name__ == '__main__': 
    # 本サイト
    mouse = Mouse("APIキー")
    map_id = 1
    try:
        mouse.start(map_id)
    except MouseError as e:
        print(e)
        sys.exit(1)

    for i in range(mouse.max_turn):
        while True:
            try:
                left_run(mouse)
            except MouseError as e:
                print(e)
                break
    print("ターンオーバー")

Page 6 of 7.

前のページ 次のページ



[添付ファイル]


お問い合わせ

プロフィール

すぺぺぺ

自己紹介

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

サイト/ブログ

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

ツイッター

@darkimpact0626