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)