python迷宫小游戏代码_python迷宫游戏,迷宫生成,解决与可视化
时间: 2023-09-27 13:05:46 浏览: 61
以下是一个简单的Python迷宫小游戏代码,包括迷宫生成、解决和可视化。
```python
import random
# 定义迷宫类
class Maze:
def __init__(self, width=10, height=10):
self.width = width
self.height = height
self.grid = self.prepare_grid()
self.configure_cells()
def prepare_grid(self):
return [[Cell(x, y) for y in range(self.height)] for x in range(self.width)]
def configure_cells(self):
for row in self.grid:
for cell in row:
x, y = cell.x, cell.y
cell.north = self.get_cell(x, y-1)
cell.east = self.get_cell(x+1, y)
cell.south = self.get_cell(x, y+1)
cell.west = self.get_cell(x-1, y)
def get_cell(self, x, y):
if x < 0 or y < 0 or x >= self.width or y >= self.height:
return None
return self.grid[x][y]
def __str__(self):
output = "+" + "---+" * self.width + "\n"
for row in self.grid:
top = "|"
bottom = "+"
for cell in row:
if cell is None:
cell = Cell(-1, -1)
body = " "
east_boundary = " " if cell.linked(cell.east) else "|"
top += body + east_boundary
south_boundary = " " if cell.linked(cell.south) else "---"
corner = "+"
bottom += south_boundary + corner
output += top + "\n"
output += bottom + "\n"
return output
# 定义细胞类
class Cell:
def __init__(self, x, y):
self.x = x
self.y = y
self.north = None
self.east = None
self.south = None
self.west = None
self.links = {}
def link(self, cell, bidi=True):
self.links[cell] = True
if bidi:
cell.link(self, False)
return self
def unlink(self, cell, bidi=True):
del self.links[cell]
if bidi:
cell.unlink(self, False)
return self
def linked(self, cell):
return cell is not None and cell in self.links
def neighbors(self):
neighbors = []
if self.north:
neighbors.append(self.north)
if self.east:
neighbors.append(self.east)
if self.south:
neighbors.append(self.south)
if self.west:
neighbors.append(self.west)
return neighbors
# 定义深度优先搜索算法
def recursive_backtracker(maze, current=None):
if current is None:
current = maze.grid[0][0]
stack = [current]
while stack:
current = stack[-1]
neighbors = [n for n in current.neighbors() if not current.linked(n)]
if not neighbors:
stack.pop()
else:
next_cell = random.choice(neighbors)
current.link(next_cell)
stack.append(next_cell)
# 定义广度优先搜索算法
def bfs(maze, start=None, end=None):
if start is None:
start = maze.grid[0][0]
if end is None:
end = maze.grid[maze.width-1][maze.height-1]
queue = [(start, [start])]
visited = set()
while queue:
(vertex, path) = queue.pop(0)
if vertex == end:
return path
if vertex not in visited:
visited.add(vertex)
for neighbor in [n for n in vertex.neighbors() if vertex.linked(n)]:
queue.append((neighbor, path + [neighbor]))
return None
# 定义主函数
def main():
maze = Maze(10, 10)
recursive_backtracker(maze)
print(maze)
path = bfs(maze)
if path:
for cell in path:
maze.grid[cell.x][cell.y] = "P"
print(maze)
if __name__ == "__main__":
main()
```
运行上面的代码会生成一个10x10的迷宫,然后使用深度优先搜索算法生成迷宫,并使用广度优先搜索算法找到从左上角到右下角的最短路径,并将路径用字母"P"标记在迷宫中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)