Ziel ist ein Programm, welches ein Labyrinth erstellt und es dem Spieler erlaubt, sich im Labyrinth zu bewegen. Er gewinnt, wenn er den Ausgang erreicht.

Vorüberlegung_Maze1.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import random
  4. class Maze(object):
  5. def __init__(self, width=21, height=21, exit_cell=(1, 1)):
  6. self.width = width
  7. self.height = height
  8. self.exit_cell = exit_cell
  9. self.create()
  10. def create(self):
  11. self.maze = [[1] * self.width for _ in range(self.height)]
  12. self.start_cell = None
  13. self.steps = None
  14. self.recursion_depth = None
  15. self._visited_cells = []
  16. self._visit_cell(self.exit_cell)
  17. def _visit_cell(self, cell, depth=0):
  18. x, y = cell
  19. self.maze[y][x] = 0 # Wand entfernen
  20. self._visited_cells.append(cell)
  21. neighbors = self._get_neighbors(cell)
  22. random.shuffle(neighbors)
  23. for neighbor in neighbors:
  24. if not neighbor in self._visited_cells:
  25. self._remove_wall(cell, neighbor)
  26. self._visit_cell(neighbor, depth+1)
  27. self._update_start_cell(cell, depth)
  28. def _get_neighbors(self, cell):
  29. """
  30. Die benachbarten Zellen nehmen.
  31. Beispiel:
  32. b sind die benachbarten Zellen von a
  33. # # # # # # # # # # # # # #
  34. # # # b # # # # a # b # # #
  35. # # # # # # # # # # # # # #
  36. # b # a # b # # b # # # # #
  37. # # # # # # # # # # # # # #
  38. # # # b # # # # # # # # # #
  39. # # # # # # # # # # # # # #
  40. """
  41. x, y = cell
  42. neighbors = []
  43. # Links
  44. if x - 2 > 0:
  45. neighbors.append((x-2, y))
  46. # Rechts
  47. if x + 2 < self.width:
  48. neighbors.append((x+2, y))
  49. # Hoch
  50. if y - 2 > 0:
  51. neighbors.append((x, y-2))
  52. # Runter
  53. if y + 2 < self.height:
  54. neighbors.append((x, y+2))
  55. return neighbors
  56. def _remove_wall(self, cell, neighbor):
  57. """
  58. Entfernen der Wand zwischen zwei Zellen
  59. Example:
  60. gegeben sind die Zellen a und b
  61. Die Wand dazwischen ist w
  62. # # # # #
  63. # # # # #
  64. # a w b #
  65. # # # # #
  66. # # # # #
  67. """
  68. x0, y0 = cell
  69. x1, y1 = neighbor
  70. # Vertikal
  71. if x0 == x1:
  72. x = x0
  73. y = (y0 + y1) / 2
  74. # Horizontal
  75. if y0 == y1:
  76. x = (x0 + x1) / 2
  77. y = y0
  78. self.maze[y][x] = 0 # Wand entfernen
  79. def _update_start_cell(self, cell, depth):
  80. if depth > self.recursion_depth:
  81. self.recursion_depth = depth
  82. self.start_cell = cell
  83. self.steps = depth * 2 # Wand + Zelle
  84. def show(self, verbose=False):
  85. MAP = {0: ' ', # Durchgang
  86. 1: '#', # Wand
  87. 2: 'B', # Ausgang
  88. 3: 'A', # Start
  89. }
  90. x0, y0 = self.exit_cell
  91. self.maze[y0][x0] = 2
  92. x1, y1 = self.start_cell
  93. self.maze[y1][x1] = 3
  94. for row in self.maze:
  95. print ' '.join([MAP[col] for col in row])
  96. if verbose:
  97. print "Schritte von A nach B:", self.steps
  98. if __name__ == '__main__':
  99. from optparse import OptionParser
  100. parser = OptionParser(description="Maze random generator")
  101. parser.add_option('-W', '--width', type=int, default=21,
  102. help="maze width (muss ungerade sein)")
  103. parser.add_option('-H', '--height', type=int, default=21,
  104. help="maze height (muss ungerade sein)")
  105. parser.add_option('-v', '--verbose', action='store_true',
  106. help="zeigt Schritte von Start bis Ziel")
  107. args, _ = parser.parse_args()
  108. for arg in ('width', 'height'):
  109. if getattr(args, arg) % 2 == 0:
  110. setattr(args, arg, getattr(args, arg) + 1)
  111. print "Warnung: %s muss ungerade sein, benutze %d stattdessen" % (arg, getattr(args, arg))
  112. exit_cell = (args.width-2, args.height-2)
  113. maze = Maze(args.width, args.height, exit_cell)
  114. maze.show(args.verbose)