Advertisement
ganryu

Untitled

Apr 1st, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. class Mundo:
  2.     def __init__(self, matrix):
  3.         self._matrix = matrix
  4.         self.filas = len(matrix)
  5.         self.columnas = len(matrix[0])
  6.         self.turnos = 0
  7.         self.aspiradora = Aspiradora(self, 0, 0)
  8.         print(self)
  9.         self.aspiradora.despertar()
  10.  
  11.     def sucio(self, x, y):
  12.         return self._matrix[x][y] == 1
  13.  
  14.     def quitar(self, x, y):
  15.         self._matrix[x][y] = 0
  16.  
  17.     def enMundo(self, x, y):
  18.         xEnMundo = (x >= 0) and (x < self.filas)
  19.         yEnMundo = (y >= 0) and (y < self.columnas)
  20.         return xEnMundo and yEnMundo
  21.  
  22.     def __str__(self):
  23.         cadena = ""
  24.         for fila in self._matrix:
  25.             cadena += str(fila)
  26.         return cadena
  27.  
  28. class Aspiradora:
  29.     maxTurnosSinLimpiar = 5
  30.    
  31.     def __init__(self, mundo, posX, posY):
  32.         self.mundo = mundo
  33.         self.posX = posX
  34.         self.posY = posY
  35.         self.turnosSinLimpiar = 0
  36.  
  37.     def derecha(self):
  38.         if self.mundo.enMundo(self.posX, self.posY+1):
  39.             self.posY += 1
  40.  
  41.     def izquierda(self):
  42.         if self.mundo.enMundo(self.posX, self.posY-1):
  43.             self.posY -= 1
  44.  
  45.     def mover(self):
  46.         if not self.mundo.enMundo(self.posX, self.posY+1):
  47.             self.izquierda()
  48.             print("Moviendo a izquierda... pos: ({}, {})".format(self.posX, self.posY))
  49.         else:
  50.             self.derecha()
  51.             print("Moviendo a derecha... pos: ({}, {})".format(self.posX, self.posY))
  52.         self.mundo.turnos += 1
  53.    
  54.     def aspirar(self):
  55.         if self.mundo.sucio(self.posX, self.posY):
  56.             self.mundo.quitar(self.posX, self.posY)
  57.             self.mundo.turnos += 1
  58.             print("Aspirando...")
  59.    
  60.     def noOp(self):
  61.         self.mundo.turnos += 1
  62.  
  63.     def despertar(self):
  64.         while True:
  65.             if self.mundo.sucio(self.posX, self.posY):
  66.                 self.aspirar()
  67.             else:
  68.                 self.mover()
  69.                 self.turnosSinLimpiar += 1
  70.                 if self.turnosSinLimpiar > self.maxTurnosSinLimpiar:
  71.                     break
  72.  
  73. def main():
  74.     matrix = [[0, 1]]
  75.     mundo = Mundo(matrix)
  76.     print(mundo)
  77.  
  78. if __name__ == "__main__":
  79.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement