Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Mundo:
- def __init__(self, matrix):
- self._matrix = matrix
- self.filas = len(matrix)
- self.columnas = len(matrix[0])
- self.turnos = 0
- self.aspiradora = Aspiradora(self, 0, 0)
- print(self)
- self.aspiradora.despertar()
- def sucio(self, x, y):
- return self._matrix[x][y] == 1
- def quitar(self, x, y):
- self._matrix[x][y] = 0
- def enMundo(self, x, y):
- xEnMundo = (x >= 0) and (x < self.filas)
- yEnMundo = (y >= 0) and (y < self.columnas)
- return xEnMundo and yEnMundo
- def __str__(self):
- cadena = ""
- for fila in self._matrix:
- cadena += str(fila)
- return cadena
- class Aspiradora:
- maxTurnosSinLimpiar = 5
- def __init__(self, mundo, posX, posY):
- self.mundo = mundo
- self.posX = posX
- self.posY = posY
- self.turnosSinLimpiar = 0
- def derecha(self):
- if self.mundo.enMundo(self.posX, self.posY+1):
- self.posY += 1
- def izquierda(self):
- if self.mundo.enMundo(self.posX, self.posY-1):
- self.posY -= 1
- def mover(self):
- if not self.mundo.enMundo(self.posX, self.posY+1):
- self.izquierda()
- print("Moviendo a izquierda... pos: ({}, {})".format(self.posX, self.posY))
- else:
- self.derecha()
- print("Moviendo a derecha... pos: ({}, {})".format(self.posX, self.posY))
- self.mundo.turnos += 1
- def aspirar(self):
- if self.mundo.sucio(self.posX, self.posY):
- self.mundo.quitar(self.posX, self.posY)
- self.mundo.turnos += 1
- print("Aspirando...")
- def noOp(self):
- self.mundo.turnos += 1
- def despertar(self):
- while True:
- if self.mundo.sucio(self.posX, self.posY):
- self.aspirar()
- else:
- self.mover()
- self.turnosSinLimpiar += 1
- if self.turnosSinLimpiar > self.maxTurnosSinLimpiar:
- break
- def main():
- matrix = [[0, 1]]
- mundo = Mundo(matrix)
- print(mundo)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement