迷宫的右手法则"/>
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迷宫的右手法则
发布评论