Advertisement
Guest User

mazeb.py

a guest
Jan 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. class Maze:
  2. """A 2D Maze"""
  3.  
  4. """The Maze constructor
  5. (none) -> none
  6. start by declaring attributes"""
  7.  
  8. def __init__(self):
  9. self.mazeIndex = 0
  10. self.mazes = []
  11. self.initMazes()
  12. self.currentMaze = self.mazes[0]
  13.  
  14. def toString(self):
  15. printme = ""
  16. """for i in range (0, len(self.currentMaze.tiles)):
  17. for j in self.currentMaze.tiles[i]:
  18. printme = printme + j"""
  19.  
  20. return printme
  21.  
  22. def initMazes(self):
  23. self.mazes.append({"tiles": [['#', '#', '#', '#', '#', '#', '#'],
  24. ['#', ' ', '#', ' ', ' ', ' ', '#'],
  25. ['#', ' ', '#', ' ', ' ', ' ', '#'],
  26. ['#', ' ', '#', ' ', '#', '#', '#'],
  27. ['#', ' ', '#', ' ', ' ', '@', '#'],
  28. ['#', ' ', ' ', ' ', ' ', ' ', '#'],
  29. ['#', '#', '#', '#', '#', '#', '#']],
  30. "sprouts": 1, "width": 7, "height": 7}
  31. )
  32. # Set the current maze to level 1
  33.  
  34. self.mazes.append({"tiles": [['#', '#', '#', '#', '#'],
  35. ['#', ' ', ' ', ' ', '#'],
  36. ['#', '@', ' ', ' ', '#'],
  37. ['#', ' ', ' ', '@', '#'],
  38. ['#', '#', '#', '#', '#']],
  39. "sprouts": 2, "width": 5, "height": 5}
  40. )
  41.  
  42.  
  43. def placeRat(self, rat_char, row, column):
  44. self.currentMaze.tiles[row][column] = rat_char
  45.  
  46. def clearAtPos(self, row, col):
  47. self.currentMaze.tiles[row][col] = " "
  48.  
  49. def eatSprouts(self):
  50. self.currentMaze.sprouts -= 1
  51. if self.currentMaze.sprouts == 0:
  52. self.mazeIndex += 1
  53. # Here we check if mazeIndex is equal to len(mazes), in which case there are no more mazes
  54. if self.mazeIndex == len(mazes):
  55. print("You win!")
  56. # Else, if there are more levels, set the next one as the current maze
  57. else:
  58. self.currentMaze = self.mazes[self.mazeIndex]
  59.  
  60. def getWidth(self):
  61. return self.currentMaze.width
  62.  
  63. def getHeight(self):
  64. return self.currentMaze.height
  65.  
  66. def getCharAtPos(self, row, col):
  67. return self.currentMaze.tiles[row][col]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement