SimulaciĆ³n de una estaciĆ³n de servicio

import threading
import random
import time
import turtle

class EstacionDeServicio:
    def __init__(self):
        self.surtidor1 = 80
        self.surtidor2 = 60
        self.lock = threading.Lock()

    def repostar_gasolina(self, coche, cantidad):
        with self.lock:
            if coche.gasolina_actual < coche.capacidad_deposito and cantidad > 0:
                cantidad_a_repostar = min(cantidad, coche.capacidad_deposito - coche.gasolina_actual)
                if cantidad_a_repostar <= self.surtidor1:
                    coche.gasolina_actual += cantidad_a_repostar
                    self.surtidor1 -= cantidad_a_repostar
                    return True
                elif cantidad_a_repostar <= self.surtidor2:
                    coche.gasolina_actual += cantidad_a_repostar
                    self.surtidor2 -= cantidad_a_repostar
                    return True
            return False

class Coche:
    def __init__(self, nombre, capacidad_deposito, color):
        self.nombre = nombre
        self.capacidad_deposito = capacidad_deposito
        self.gasolina_actual = 0
        self.color = color

    def dibujar_coche(self, x, y):
        turtle.penup()
        turtle.goto(x, y)
        turtle.pendown()
        turtle.color(self.color)
        turtle.begin_fill()
        for _ in range(2):
            turtle.forward(60)
            turtle.right(90)
            turtle.forward(20)
            turtle.right(90)
        turtle.end_fill()

        for i in range(2):
            turtle.penup()
            turtle.goto(x + i * 55, y - 30) 
            turtle.pendown()
            turtle.color("black")
            turtle.begin_fill()
            turtle.circle(10)
            turtle.end_fill()

        turtle.penup()
        turtle.goto(x + 10, y + 20)
        turtle.pendown()
        turtle.color(self.color)
        turtle.begin_fill()
        for _ in range(2):
            turtle.forward(40)
            turtle.right(90)
            turtle.forward(20)
            turtle.right(90)
        turtle.end_fill()

        turtle.penup()
        turtle.goto(x + 30, y + 5)
        turtle.pendown()
        turtle.color("black")
        turtle.write(f"{self.gasolina_actual}L", align="center", font=("Arial", 8, "normal"))

        turtle.penup()
        turtle.goto(x + 30, y + 40)
        turtle.pendown()
        turtle.color("black")
        turtle.write(self.nombre, align="center", font=("Arial", 8, "normal"))

    def dibujar_surtidor_gasolina(self, x, y):

        turtle.penup()
        turtle.goto(x, y)
        turtle.pendown()
        turtle.color("red")
        turtle.begin_fill()
        for _ in range(2):
            turtle.forward(20)
            turtle.right(90)
            turtle.forward(40)
            turtle.right(90)
        turtle.end_fill()

 
        turtle.penup()
        turtle.goto(x + 10, y - 15)
        turtle.pendown()
        turtle.color("black")
        turtle.write(f"{self.gasolina_actual}L", align="center", font=("Arial", 8, "normal"))


        turtle.penup()
        turtle.goto(x + 10, y + 20)
        turtle.pendown()
        turtle.color("black")
        turtle.write("Surtidor", align="center", font=("Arial", 8, "normal"))

def conductor(estacion, coche):
    while estacion.repostar_gasolina(coche, random.randint(2, 20)):
        time.sleep(1)
    print(f"{coche.nombre} ha terminado de repostar. Gasolina actual: {coche.gasolina_actual}")


estacion = EstacionDeServicio()

capacidades_deposito = random.sample(range(10, 61), 6)
colores = ["green", "blue", "orange", "pink", "yellow", "grey"]
coches = [Coche(f"Coche {i + 1}", capacidad, colores[i]) for i, capacidad in enumerate(capacidades_deposito)]
hilos_conductores = [threading.Thread(target=conductor, args=(estacion, coche)) for coche in coches]

print("Estado inicial de los coches:")
for coche in coches:
    print(f"{coche.nombre}: Gasolina actual = {coche.gasolina_actual}/{coche.capacidad_deposito}")

for hilo_conductor in hilos_conductores:
    hilo_conductor.start()


fila1_y = 100
fila2_y = -100
for i, coche in enumerate(coches):
    if i < 3:
        coche.dibujar_coche(-150 + i * 150, fila1_y)
    else:
        coche.dibujar_coche(-150 + (i - 3) * 150, fila2_y)


for hilo_conductor in hilos_conductores:
    hilo_conductor.join()

coche_surtidor1 = Coche("Surtidor 1", 0, "")
coche_surtidor2 = Coche("Surtidor 2", 0, "")
coche_surtidor1.surtidor1 = estacion.surtidor1
coche_surtidor2.surtidor2 = estacion.surtidor2
coche_surtidor1.dibujar_surtidor_gasolina(-75, -30)
coche_surtidor2.dibujar_surtidor_gasolina(75, -30)


print(f"\nEstado final de los surtidores:")
print(f"Surtidor 1: {estacion.surtidor1} litros")
print(f"Surtidor 2: {estacion.surtidor2} litros")

print("\nEstado final de los coches:")
for coche in coches:
    print(f"{coche.nombre}: Gasolina actual = {coche.gasolina_actual}/{coche.capacidad_deposito}")

turtle.hideturtle()

turtle.done()

![](image/captura1_enproceso.png) ![](image/captura2_procesofinalizado.png)