Oscar Arranz
Oscar Arranz

Reputation: 714

Python gets into an infinite loop that does nothing

I don't know why it's happening but basically after python should end a while, it does nothing (but the program doesn't finish) and I can see the program is using processor as if it was an infinite loop.

I have no clue why it happens, it wasn't happening before and I think I haven't changed anything on the function that's causing this issue.

Code:

import random


class Gato:

    movimiento = 0
    posicion = [0, 0]

    def spawn(self):
        self.posicion = [random.randint(0, 4), random.randint(0, 4)]

    def moverse(self, direccion):
        movido = False

        while not movido:
            if direccion == "sur":
                if self.posicion[1] == 4:
                    print("No puedes salir de los limites del mapa")
                    direccion = input("Vuelve a moverte en una dirección (norte, este, oeste)")
                else:
                    self.posicion[1] += 1
                    self.movimiento += 1
                    movido = True
            elif direccion == "norte":
                if self.posicion[1] == 0:
                    print("No puedes salir de los limites del mapa")
                    direccion = input("Vuelve a moverte en una dirección (sur, este, oeste)")
                else:
                    self.posicion[1] -= 1
                    self.movimiento += 1
                    movido = True
            elif direccion == "este":
                if self.posicion[0] == 4:
                    print("No puedes salir de los limites del mapa")
                    direccion = input("Vuelve a moverte en una dirección (norte, sur, oeste)")
                else:
                    self.posicion[0] += 1
                    self.movimiento += 1
                    movido = True
            elif direccion == "oeste":
                if self.posicion[0] == 0:
                    print("No puedes salir de los limites del mapa")
                    direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")
                else:
                    self.posicion[0] -= 1
                    self.movimiento += 1
                    movido = True
            else:
                print("Esa no es una direccion válida")
                direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")

    def detectarcolisionraton(self, raton):
        if self.posicion == raton.getposicion():
            return True
        else:
            return False

    def detectarcolisionqueso(self, queso):
        if self.posicion == queso.getposicion():
            return True
        else:
            return False

    def getmovimientos(self):
        return self.movimiento

    def checklimites(self, dir):
        if self.posicion[1] == 4 & dir == 0:
            print("No puedes salir de los limites del mapa")
        elif self.posicion[1] == 0 & dir == 1:
            print("No puedes salir de los limites del mapa")
        elif self.posicion[0] == 0 & dir == 2:
            print("No puedes salir de los limites del mapa")
        elif self.posicion[0] == 4 & dir == 3:
            print("No puedes salir de los limites del mapa")


class Raton:

    posicion = [0, 0]

    def spawn(self):
        self.posicion = [random.randint(0, 4), random.randint(0, 4)]

    def getposicion(self):
        return self.posicion

    def moverse(self, gato):
        xory = random.randint(0, 1)
        rand = random.randint(0, 1)
        movido = False

        while not movido:
            if gato.getmovimientos() % 3 == 0:
                if xory == 0:
                    if rand == 0:
                        if self.posicion[0] == 4:
                            xory = random.randint(0, 1)
                            rand = random.randint(0, 1)
                        else:
                            self.posicion[0] += 1
                            movido = True
                    else:
                        if self.posicion[0] == 0:
                            xory = random.randint(0, 1)
                            rand = random.randint(0, 1)
                        else:
                            self.posicion[0] -= 1
                            movido = True
                else:
                    if rand == 0:
                        if self.posicion[1] == 4:
                            xory = random.randint(0, 1)
                            rand = random.randint(0, 1)
                        else:
                            self.posicion[1] += 1
                            movido = True
                    else:
                        if self.posicion[1] == 0:
                            xory = random.randint(0, 1)
                            rand = random.randint(0, 1)
                        else:
                            self.posicion[1] -= 1
                            movido = True

    def pista(self, gato):
        if self.posicion[0] == gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
            print("El raton está al Sur")
        elif self.posicion[0] > gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
            print("El raton está al Sureste")
        elif self.posicion[0] > gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
            print("El raton está al Este")
        elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
            print("El raton está al Noreste")
        elif self.posicion[0] == gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
            print("El raton está al Norte")
        elif self.posicion[0] < gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
            print("El raton está al Noroeste")
        elif self.posicion[0] < gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
            print("El raton está al Oeste")
        elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
            print("El raton está al Suroeste")


class Queso:

    posicion = [0, 0]

    def spawn(self):
        self.posicion = [random.randint(0, 4), random.randint(0, 4)]

    def getposicion(self):
        return self.posicion


def imprimirmapa():
    for y in range(0, 5):
        for x in range(0, 5):
            print("[", end="", flush=True)
            if gato.posicion[0] == x and gato.posicion[1] == y:
                print("O", end="", flush=True)
            else:
                print("X", end="", flush=True)

            if x == 4:
                print("]")
            else:
                print("]", end="", flush=True)


gato = Gato()
raton = Raton()
queso = Queso()

gato.spawn()
raton.spawn()
queso.spawn()

while gato.posicion[0] == raton.posicion[0] and gato.posicion[1] == raton.posicion[1]:
    print("Spawn Failed!")
    raton.spawn()


ganado = False

while not ganado:
    raton.pista(gato)
    imprimirmapa()
    gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
    if gato.detectarcolisionraton(raton):
        print("Has ganado")
        ganado = True
    elif gato.detectarcolisionqueso(queso):
        queso.posicion = [-1, -1]
        print("Encontraste el QUESO")
        raton.pista(gato)
        gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
        raton.pista(gato)
        gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
        raton.pista(gato)
        queso.spawn()
        gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
    raton.moverse(gato)

Upvotes: 0

Views: 76

Answers (1)

F. Montes
F. Montes

Reputation: 36

In your Raton class, in moverse(), you have if gato.getmovimientos() % 3 == 0 is True, then this leads to changing movido from False to True, however you have no statement for when gato.getmovimientos() is False, which is why you have an infinite loop.

Upvotes: 1

Related Questions