python实现简单n*n迷宫的右手法则

python实现简单n*n迷宫的右手法则

话不多说 直接上代码

代码中注释我感觉是够清楚的了

dirct = {
    0:"上",
    1:"右",
    2:"下",
    3:"左",
}
#定义迷宫中的人物类
class Person():
# 构造函数  参数为人物在迷宫中的初始位置,前进方向,方向表示在字典中有说明
    def __init__(self,x,y,d):
        self.pos_x = x
        self.pos_y = y
        self.pos_lastx = x #lastx为移动之前的位置,初始化为起始位置
        self.pos_lasty = y
        self.direction = d # 0 U  1 R  2 D  3 L
    def posF(self): #人物前方的位置
        if self.direction == 0:   #上
            return [self.pos_x - 1,self.pos_y]
        elif  self.direction == 1:#右
            return [self.pos_x, self.pos_y + 1]
        elif  self.direction == 2:#下
            return [self.pos_x + 1, self.pos_y ]
        elif  self.direction == 3:#左
            return [self.pos_x , self.pos_y - 1]
        else:
            print("Position is wrong!")

    def posR(self):  #人物前方的位置
        if self.direction == 0:#上
            return [self.pos_x,self.pos_y + 1]
        elif  self.direction == 1:#右
            return [self.pos_x + 1 , self.pos_y]
        elif  self.direction == 2:#下
            return [self.pos_x , self.pos_y - 1]
        elif  self.direction == 3:#左
            return [self.pos_x  - 1, self.pos_y ]
        else:
            print("Position is wrong!")

    def PersoninMaze(self): # 人物在地图中的位置
        global maze  #使用全局地图
        #地图中人物用Y表示
        if (self.pos_x == self.pos_lastx)&(self.pos_y == self.pos_lasty):  #如果初始位置和移动之前位置重合,则此位置为人物初始位置,表示在地图中
            maze[self.pos_x][self.pos_y] = "Y"  
        else: #否则更新人物的位置,并将人物移动之前位置清零
            maze[self.pos_lastx][self.pos_lasty] = 0
            maze[self.pos_x][self.pos_y] = "Y"
        #更新地图以及人物位置    
        print("----"*10)
        for i in range(len(maze)):  # 控制行,0~2
            for j in range(len(maze[i])):  # 控制列
                print(maze[i][j], end='\t')
            print()
        print("----" * 10)
        #为了方便调试观察,打印每次移动之后人物的朝向
        print("the direc is {}".format(dirct[self.direction]))
    def Move(self):  #向前走 同时更新地图位置
    	#记录人物此时位置
        self.pos_lastx = self.pos_x
        self.pos_lasty = self.pos_y
        #往前走 下一刻人物的位置就是此时人物前方的位置,
        self.pos_x = self.posF()[0]
        self.pos_y = self.posF()[1]
        #移动之后,更新地图显示
        self.PersoninMaze()
        #左转逻辑
    def TurnLeft(self): #左转
        if (self.direction - 1) < 0 :
            self.direction = 3
        else:
            self.direction -= 1
            #右转逻辑
    def TurnRight(self): #右转
        if (self.direction + 1) > 3 :
            self.direction = 0
        else:
            self.direction += 1
	# 可以使用此函数查看信息
    def printninf(self):
        #print("the last pos is ({},{})".format(self.pos_lastx, self.pos_lasty))
        print("the pos after move is ({},{})".format(self.pos_x, self.pos_y))
        print("the direc is {}".format(dirct[self.direction]))
        print("the pos front is {}".format(self.posF()))
        print("the pos right is {}".format(self.posR()))
   

 #移动逻辑 右手法则 递归
def isWall(Person):
    global maze
    # 判断如果人物前边或者右边超出了maze的index范围,说明检测到了边界出口,退出运行
    if (Person.posF()[0] < 0 ) | (Person.posF()[0] > 9 ) | (Person.posF()[1] < 0 ) | (Person.posF()[1] > 9 ):  #index的数值也可以通过np.shape获取
        print("out")
    else:
    # 
        if maze[Person.posR()[0]][Person.posR()[1]] == 0:  # 右边是路
            Person.TurnRight()  #若右手不为墙壁,就原地右转在直走
            Person.Move()
            #Person.printninf()
            isWall(Person)
        elif maze[Person.posR()[0]][Person.posR()[1]] == 1:  # 右边是墙壁
            if (maze[Person.posF()[0]][Person.posF()[1]] == 0):  # 人物正前方的是路  右边是墙
                Person.Move() #往前走
                #Person.printninf()
                isWall(Person)
            elif (maze[Person.posF()[0]][Person.posF()[1]] == 1):#人物正前方的是墙
                Person.TurnLeft() #左转两次
                Person.TurnLeft()
                #Person.printninf()
                isWall(Person) #检测右手情况

if __name__ == '__main__':
    maze=[[1,0,1,1,1,1,1,1,1,1],
          [1,0,0,1,1,0,1,1,1,1],
          [1,1,0,0,0,0,1,1,1,1],
          [1,0,1,0,1,0,0,0,1,1],
          [1,0,1,0,1,0,1,1,0,1],
          [1,0,0,0,0,1,0,0,0,1],
          [1,1,0,1,0,0,0,1,0,1],
          [1,1,1,0,1,1,1,1,0,1],
          [1,1,1,0,0,0,0,0,0,1],
          [1,1,1,1,0,1,1,1,1,1]]
    p = Person(0,1,2)
    isWall(p)
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值