Ejemplo n.º 1
0
def abrirventana2(img, img2, root, valor_reina, img3):

    # Se grafica el tablero de juego dada una matriz, la cual proviene del
    # algoritmo de las vegas:
    def GraficarTablero(matriz):
        for n, i in enumerate(matriz):
            posy = n * 40
            for k, j in enumerate(i):
                posx = k * 40
                # Se grafica el tablero con las imágenes que se definen en la función
                # "ventana_principal"
                if n % 2 == 0 and k % 2 == 0 and j == 0:
                    tk.Label(frame2, image=img).place(x=posx, y=posy)
                if n % 2 == 0 and k % 2 == 0 and j != 0:
                    tk.Label(frame2, image=img2).place(x=posx, y=posy)
                if n % 2 == 0 and k % 2 != 0 and j != 0:
                    tk.Label(frame2, image=img3).place(x=posx, y=posy)
                if n % 2 != 0 and k % 2 == 0 and j != 0:
                    tk.Label(frame2, image=img3).place(x=posx, y=posy)
                if n % 2 != 0 and k % 2 != 0 and j == 0:
                    tk.Label(frame2, image=img).place(x=posx, y=posy)
                if n % 2 != 0 and k % 2 != 0 and j != 0:
                    tk.Label(frame2, image=img2).place(x=posx, y=posy)

    reinas = nreinas.algoritmo(int(valor_reina.get()))

    # El anterior "reinas", se convierte en matriz para poder graficar:
    matriz_tablero = nreinas.matriz(reinas)
    print(reinas)

    # Hacer que la ventana_raiz principal 'root' se cierre:
    root.withdraw()
    # Crear la segunda ventana_raiz 'ventana_secundaria', mediante el método toplevel, el cual permite crear una ventana_raiz hija
    # de la ventana_raiz principal que hereda sus atributos:
    ventana_secundaria = tk.Toplevel()
    ventana_secundaria.title('Tablero')
    frame2 = tk.Frame(ventana_secundaria,
                      bg="#F4F6F7",
                      width=len(matriz_tablero) * 40,
                      height=len(matriz_tablero) * 40)
    frame2.pack()
    # Se grafica la matriz:
    GraficarTablero(matriz_tablero)
    def __salida(self, nombre):
        # El robot llega y se va cuando se le atiende o acaba el juego:
        llegada = self.__env.now
        print(f'Al minuto{self.__env.now:7.2f} llega el robot {nombre}')

        # Variables globales:
        global COLA
        global MAX_COLA
        global ESPERA_ROBOT
        global UTILIDAD
        global UTILIDAD_PROFESOR
        global PIERDE_PROFESOR

        #Atendemos a los robots (retorno del yield)
        # El robot ocupa al mesero:
        with self.__mesero.request() as req:

            # Se aumenta el tamaño de la cola al llegar un robot:
            COLA += 1
            if COLA > MAX_COLA:
                MAX_COLA = COLA

            print(f'Tamaño de la cola: {COLA}')

            # Si el profesor está ocupado jugando con otro robot, el robot actual espera
            # a que se desocupe y sea atendido:
            yield req
            # Una vez desocupado el profesor y empiece a atender el robot en cola, se disminuye la cola:
            COLA = COLA - 1
            # Se calcula el tiempo que esperó el robot:
            espera = self.__env.now - llegada
            # El tiempo de espera se añade a una lista para poder calcular el tiempo promedio de espera:
            ESPERA_ROBOT = np.append(ESPERA_ROBOT, espera)
            print(
                f'Al minuto{self.__env.now:7.2f} el robot {nombre} es atendido, esperando{espera:7.2f} minutos en cola'
            )

            # Se selecciona un n para el tamaño del tablero
            n_tablero = random.choice([4, 5, 6, 8, 10, 12, 15])
            print(f'Tamaño del tablero {n_tablero}')

            # Se calcula el tiempo de ejecución del algoritmo Las Vegas:
            valor1 = time.time()
            vegas.algoritmo(n_tablero)
            valor2 = time.time()
            tiempo_vegas = valor2 - valor1
            print(f'Las vegas demoró {tiempo_vegas}')

            # Se calcula el tiempo de ejecución del algoritmo deterministico:
            valor3 = time.time()
            determinista.solver(n_tablero)
            valor4 = time.time()
            tiempo_determinista = valor4 - valor3
            print(f'Determinista demoró {tiempo_determinista}')

            # El menor tiempo de ejecución indica quién ganó el juego:
            if tiempo_vegas < tiempo_determinista:
                print("			Gana el robot")
                UTILIDAD -= 10
                PIERDE_PROFESOR += 1
                # Se realiza el tiempo de servicio:
                yield self.__env.timeout(tiempo_vegas)
            else:
                print("			Gana el profesor")
                UTILIDAD += 15
                UTILIDAD_PROFESOR += 15
                # Se realiza el tiempo de servicio:
                yield self.__env.timeout(tiempo_determinista)

            print(f'Al minuto{self.__env.now:7.2f} sale el robot {nombre}')