|
| 1 | +import time |
| 2 | +from turtle import * |
| 3 | + |
| 4 | +from maze import MazeGen |
| 5 | + |
| 6 | +# ENTER = 2 |
| 7 | +# EXIT = 5 |
| 8 | +PART_OF_PATH = 0 |
| 9 | +OBSTACLE = 1 |
| 10 | +TRIED = 3 |
| 11 | +DEAD_END = 4 |
| 12 | + |
| 13 | +class Maze: |
| 14 | + def __init__(self, mazedata, enter, exit) -> None: |
| 15 | + rowsInMaze = len(mazedata) |
| 16 | + columnsInMaze = len(mazedata[0]) |
| 17 | + self.enter = enter |
| 18 | + self.exit = exit |
| 19 | + self.startRow = enter[0] |
| 20 | + self.startCol = enter[1] |
| 21 | + self.mazelist = mazedata |
| 22 | + |
| 23 | + self.rowsInMaze = rowsInMaze |
| 24 | + self.columnsInMaze = columnsInMaze |
| 25 | + self.xTranslate = -columnsInMaze/2 |
| 26 | + self.yTranslate = rowsInMaze/2 |
| 27 | + self.t = Turtle(shape='turtle') |
| 28 | + setup(width=800, height=650) |
| 29 | + setworldcoordinates(-(columnsInMaze-1)/2 - 0.5, -(rowsInMaze-1)/2 - 0.5, |
| 30 | + (columnsInMaze-1)/2 + 0.5, (rowsInMaze-1)/2 + 0.5) |
| 31 | + pass |
| 32 | + |
| 33 | + def drawMaze(self): |
| 34 | + tracer(0) |
| 35 | + for y in range(self.rowsInMaze): |
| 36 | + for x in range(self.columnsInMaze): |
| 37 | + if self.mazelist[y][x] == OBSTACLE: |
| 38 | + self.drawCenteredBox(x + self.xTranslate, -y + self.yTranslate, 'tan') |
| 39 | + |
| 40 | + self.t.color('black', 'blue') |
| 41 | + self.updatePosition(self.startRow, self.startCol) |
| 42 | + tracer(1) |
| 43 | + |
| 44 | + def drawCenteredBox(self, x, y, color): |
| 45 | + self.t.up() |
| 46 | + self.t.goto(x - 0.5, y - 0.5) |
| 47 | + self.t.color('black', color) |
| 48 | + self.t.setheading(90) |
| 49 | + self.t.down() |
| 50 | + self.t.begin_fill() |
| 51 | + for _ in range(4): |
| 52 | + self.t.forward(1) |
| 53 | + self.t.right(90) |
| 54 | + self.t.end_fill() |
| 55 | + update() |
| 56 | + |
| 57 | + |
| 58 | + def moveTurtle(self, x, y): |
| 59 | + self.t.up() |
| 60 | + self.t.setheading(self.t.towards(x+self.xTranslate, -y+self.yTranslate)) |
| 61 | + self.t.goto(x+self.xTranslate, -y+self.yTranslate) |
| 62 | + |
| 63 | + def dropBreadcrumb(self, color): |
| 64 | + self.t.dot(color) |
| 65 | + |
| 66 | + def updatePosition(self, row, col, val=None): |
| 67 | + if val: |
| 68 | + self.mazelist[row][col] = val |
| 69 | + self.moveTurtle(col, row) |
| 70 | + |
| 71 | + if val == PART_OF_PATH: |
| 72 | + color = 'green' |
| 73 | + elif val == OBSTACLE: |
| 74 | + color = 'red' |
| 75 | + elif val == TRIED: |
| 76 | + color = 'black' |
| 77 | + elif val == DEAD_END: |
| 78 | + color = 'red' |
| 79 | + else: |
| 80 | + color = None |
| 81 | + |
| 82 | + if color: |
| 83 | + self.dropBreadcrumb(color) |
| 84 | + |
| 85 | + def isExit(self, row, col): |
| 86 | + return (row, col) == self.exit |
| 87 | + # return (row == 0 or row == self.rowsInMaze-1 or |
| 88 | + # col == 0 or col == self.columnsInMaze-1) |
| 89 | + |
| 90 | + def __getitem__(self, idx): |
| 91 | + try: |
| 92 | + return self.mazelist[idx] |
| 93 | + except: |
| 94 | + return [int(i) for i in '1'*self.columnsInMaze] |
| 95 | + |
| 96 | +def find(maze, startRow, startColumn, searchType): |
| 97 | + if searchType == 'es' or searchType == 'e': |
| 98 | + return east(maze, startRow, startColumn, searchType) or south(maze, startRow, startColumn, searchType) or \ |
| 99 | + west(maze, startRow, startColumn, searchType) or north(maze, startRow, startColumn, searchType) |
| 100 | + elif searchType == 'en': |
| 101 | + return east(maze, startRow, startColumn, searchType) or north(maze, startRow, startColumn, searchType) or \ |
| 102 | + west(maze, startRow, startColumn, searchType) or south(maze, startRow, startColumn, searchType) |
| 103 | + elif searchType == 'wn' or searchType == 'w': |
| 104 | + return west(maze, startRow, startColumn, searchType) or north(maze, startRow, startColumn, searchType) or \ |
| 105 | + east(maze, startRow, startColumn, searchType) or south(maze, startRow, startColumn, searchType) |
| 106 | + elif searchType == 'ws': |
| 107 | + return west(maze, startRow, startColumn, searchType) or south(maze, startRow, startColumn, searchType) or \ |
| 108 | + east(maze, startRow, startColumn, searchType) or north(maze, startRow, startColumn, searchType) |
| 109 | + elif searchType == 'n': |
| 110 | + return north(maze, startRow, startColumn, searchType) or east(maze, startRow, startColumn, searchType) or \ |
| 111 | + west(maze, startRow, startColumn, searchType) or south(maze, startRow, startColumn, searchType) |
| 112 | + elif searchType == 's': |
| 113 | + return south(maze, startRow, startColumn, searchType) or east(maze, startRow, startColumn, searchType) or \ |
| 114 | + west(maze, startRow, startColumn, searchType) or north(maze, startRow, startColumn, searchType) |
| 115 | + pass |
| 116 | + |
| 117 | +def east(maze, startRow, startColumn, searchType): |
| 118 | + return search(maze, startRow, startColumn+1, searchType) |
| 119 | + |
| 120 | +def south(maze, startRow, startColumn, searchType): |
| 121 | + return search(maze, startRow+1, startColumn, searchType) |
| 122 | + |
| 123 | +def west(maze, startRow, startColumn, searchType): |
| 124 | + return search(maze, startRow, startColumn-1, searchType) |
| 125 | + |
| 126 | +def north(maze, startRow, startColumn, searchType): |
| 127 | + return search(maze, startRow-1, startColumn, searchType) |
| 128 | + |
| 129 | + |
| 130 | +def search(maze, startRow, startColumn, searchType): # 从指定的点开始搜索 |
| 131 | + if maze[startRow][startColumn] == OBSTACLE: |
| 132 | + return False |
| 133 | + if maze[startRow][startColumn] == TRIED: |
| 134 | + return False |
| 135 | + if maze.isExit(startRow, startColumn): |
| 136 | + maze.updatePosition(startRow, startColumn, PART_OF_PATH) |
| 137 | + return True |
| 138 | + |
| 139 | + maze.updatePosition(startRow, startColumn, TRIED) |
| 140 | + |
| 141 | + found = find(maze, startRow, startColumn, searchType) |
| 142 | + # found = search(maze, startRow, startColumn+1) or \ |
| 143 | + # search(maze, startRow+1, startColumn) or \ |
| 144 | + # search(maze, startRow-1, startColumn) or \ |
| 145 | + # search(maze, startRow, startColumn-1) |
| 146 | + |
| 147 | + |
| 148 | + if found: |
| 149 | + maze.updatePosition(startRow, startColumn, PART_OF_PATH) |
| 150 | + else: |
| 151 | + maze.updatePosition(startRow, startColumn, DEAD_END) |
| 152 | + |
| 153 | + return found |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == '__main__': |
| 157 | + mg = MazeGen(31, 21) |
| 158 | + mg.generate() |
| 159 | + mazedata = mg.map |
| 160 | + m = Maze(mg.map, mg.entrance, mg.exit) |
| 161 | + myWin = m.t.getscreen() |
| 162 | + m.drawMaze() |
| 163 | + |
| 164 | + # 计算最近探索方向 |
| 165 | + searchType = 'es' |
| 166 | + if mg.entrance[0]<mg.exit[0] and mg.entrance[1] < mg.exit[1]: |
| 167 | + searchType = 'es' |
| 168 | + elif mg.entrance[0]<mg.exit[0] and mg.entrance[1] > mg.exit[1]: |
| 169 | + searchType = 'ws' |
| 170 | + elif mg.entrance[0]>mg.exit[0] and mg.entrance[1] > mg.exit[1]: |
| 171 | + searchType = 'wn' |
| 172 | + elif mg.entrance[0]>mg.exit[0] and mg.entrance[1] < mg.exit[1]: |
| 173 | + searchType = 'en' |
| 174 | + elif mg.entrance[0] == mg.exit[0]: |
| 175 | + searchType = 'n' |
| 176 | + elif mg.entrance[1] == mg.exit[1]: |
| 177 | + searchType = 's' |
| 178 | + |
| 179 | + search(m, m.startRow, m.startCol, searchType) |
| 180 | + |
| 181 | + myWin.exitonclick() |
0 commit comments