|
|
|
@ -45,118 +45,6 @@ Para acabar el código, se esconde la pluma (tortuga) que pinta y se finaliza.
|
|
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|