python迷宫问题模拟程序_python3练习,做一个迷宫生成程序

1 #!/usr/bin/python3

2 #coding=utf-8

3 importrandom4 importtkinter as tk5

6 classCell():7 TOP =(0)8 RIGHT = (1)9 BOTTOM = (2)10 LEFT = (3)11 def __init__(self, x, y):12 self.index =013 self.x =x14 self.y =y15 self.walls =[True, True, True, True]16 self.visited =False17 def __str__(self):18 return ‘x:{}-y:{}, walls:{}‘.format(self.x, self.y, self.walls)19 defWalls(self):20 returnself.walls21 defdelTop(self):22 self.walls[0] =False23 defdelRight(self):24 self.walls[1] =False25 defdelBottom(self):26 self.walls[2] =False27 defdelLeft(self):28 self.walls[3] =False29 defsetWall(self, isWall, index):30 if index not in [0, 1, 2, 3]:31 return

32 self.walls[index] =isWall33 defXY(self):34 return[self.x, self.y]35 defX(self):36 returnself.x37 defY(self):38 returnself.y39 defisVisited(self):40 returnself.visited41 defsetVisited(self):42 self.visited =True43

44 classMaze():45 SIZE = (18)46 START =(0, 0)47 def __init__(self):48 self.size =self.SIZE49 self.bfsBuffer =[]50 self.cells =51 [[Cell(y, x) for x inrange(self.SIZE)]52 for y inrange(self.SIZE)]53

54 self.current = self.cells[self.START[0]][self.START[1]]55

56

57 def __str__(self):58 info = ‘‘

59 for rows inself.cells:60 for cell inrows:61 info +=str(cell)62 returninfo63 deftrblWalls(self, x, y):64 returnself.cells[x][y].Walls()65 defvisited(self, x, y):66 returnself.cells[x][y].isVisited()67 defCurrentCell(self):68 returnself.current69 defsetCurrent(self, cell):70 self.current =cell71 deftopCell(self):72 if 0 ==self.current.Y():73 returnNone74 return self.cells[self.current.X()][self.current.Y() - 1]75 defrightCell(self):76 if self.current.X() == (self.SIZE - 1):77 returnNone78 return self.cells[self.current.X() + 1][self.current.Y()]79 defbottomCell(self):80 if self.current.Y() == (self.SIZE - 1):81 returnNone82 return self.cells[self.current.X()][self.current.Y() + 1]83 defleftCell(self):84 if 0 ==self.current.X():85 returnNone86 return self.cells[self.current.X() - 1][self.current.Y()]87 defdelWall(self, current, neighbor):88 x =current.X()89 y =current.Y()90 x2 =neighbor.X()91 y2 =neighbor.Y()92 #print("({}x{}) and ({}x{})".format(x, y, x2, y2))

93

94 if (1 == (x -x2)):95 current.delLeft()96 neighbor.delRight()97 elif (-1 == (x -x2)):98 current.delRight()99 neighbor.delLeft()100 if (1 == (y -y2)):101 current.delTop()102 neighbor.delBottom()103 elif (-1 == (y -y2)):104 current.delBottom()105 neighbor.delTop()106

107 defcheckNeighbor(self):108 neighbor =[]109 top =self.topCell()110 right =self.rightCell()111 bottom =self.bottomCell()112 left =self.leftCell()113 if (None != top and nottop.isVisited()):114 neighbor.append(self.topCell())115 if (None != right and notright.isVisited()):116 neighbor.append(self.rightCell())117 if (None != bottom and notbottom.isVisited()):118 neighbor.append(self.bottomCell())119 if (None != left and notleft.isVisited()):120 neighbor.append(self.leftCell())121 count =len(neighbor)122 if 0 ==count:123

124 if (len(self.bfsBuffer) ==0):125 return

126 self.current =self.bfsBuffer.pop()127 self.checkNeighbor()128

129 return

130

131 old =self.current132

133 self.current = neighbor[random.randint(0, count - 1)]134

135 self.delWall(old, self.current)136 #print(‘neighbor count:{} ->{}‘.format(count, str(self.current)))

137 self.setUp()138

139 defsetUp(self):140 self.current.setVisited()141 self.bfsBuffer.append(self.current)142 self.checkNeighbor()143

144 classMazeUI():145 winWidth = (600)146 winHeight = (600)147 def __init__(self):148 self.maze =Maze()149 self.ui =tk.Tk()150 self.centeredDisplay()151

152 self.createMaze()153

154 self.ui.mainloop()155 defcenteredDisplay(self):156 self.ui.title(‘Maze by jianc‘)157 self.ui.geometry(‘{}x{}+{}+{}‘.format(158 self.winWidth, self.winHeight,159 int((self.ui.winfo_screenwidth() - self.winWidth)/2),160 int((self.ui.winfo_screenheight() - self.winHeight)/2)))161 self.ui.resizable(False, False)162 defcreateMaze(self):163 self.cs = tk.Canvas(self.ui, bg = ‘#5f3c23‘)164

165 self.cs.pack(side = tk.TOP, fill = ‘both‘, expand=1,166 padx=0, ipadx=0, pady=0, ipady=0)167

168 self.maze.setUp()169

170 #print(self.maze)

171 self.drawCells()172

173 defdrawCells(self):174 w = float(self.winWidth /self.maze.SIZE)175 h = float(self.winHeight /self.maze.SIZE)176 current =self.maze.CurrentCell()177 y =current.X()178 x =current.Y()179 self.cs.create_rectangle(y * w + 4, x * h + 4, (y + 1) * w - 4, (x + 1) * h - 4,180 fill=‘#ff0000‘, width =0)181

182

183

184 for rows inrange(self.maze.SIZE):185 for cols inrange(self.maze.SIZE):186 top, right, bottom, left =self.maze.trblWalls(cols, rows)187 #print("top:{} right:{} bottom:{} left:{}".format(top, right, bottom, left))

188 bVisited =self.maze.visited(rows, cols)189

190 """if bVisited:191 self.cs.create_rectangle(rows * w + 10, cols * h + 10,192 (rows + 1) * w - 10, (cols+ 1) * h - 10, fill=‘#00ff00‘, width = 0)193 """

194 iftop:195 self.cs.create_line(cols * w, rows *h,196 (cols + 1) * w, rows * h, width=5)197 ifright:198 self.cs.create_line((cols + 1) * w, rows *h,199 (cols + 1) * w, (rows + 1) * h, width=5)200 ifbottom:201 self.cs.create_line((cols + 1) * w, (rows + 1) *h,202 cols * w, (rows + 1) * h, width=5)203 ifleft:204 self.cs.create_line(cols * w, (rows + 1) *h,205 cols * w, rows * h, width=5)206

207 current =self.maze.CurrentCell()208 y =current.X()209 x =current.Y()210 self.cs.create_rectangle(y * w + 5, x * h + 5, (y + 1) * w - 5, (x + 1) * h - 5,211 fill=‘#ff0000‘, width =0)212

213

214

215 maze = MazeUI()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值