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

编程入门 行业动态 更新时间:2024-10-12 05:51:16

python实现简单n*n<a href=https://www.elefans.com/category/jswz/34/1769354.html style=迷宫的右手法则"/>

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

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

话不多说 直接上代码

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

dirct = {0:"上",1:"右",2:"下",3:"左",
}
#定义迷宫中的人物类
class Person():
# 构造函数  参数为人物在迷宫中的初始位置,前进方向,方向表示在字典中有说明def __init__(self,x,y,d):self.pos_x = xself.pos_y = yself.pos_lastx = x #lastx为移动之前的位置,初始化为起始位置self.pos_lasty = yself.direction = d # 0 U  1 R  2 D  3 Ldef 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] = 0maze[self.pos_x][self.pos_y] = "Y"#更新地图以及人物位置    print("----"*10)for i in range(len(maze)):  # 控制行,0~2for 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_xself.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 = 3else:self.direction -= 1#右转逻辑def TurnRight(self): #右转if (self.direction + 1) > 3 :self.direction = 0else: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)

更多推荐

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

本文发布于:2024-03-12 23:30:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1732674.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:迷宫   法则   右手   简单   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!