Created
October 14, 2022 22:01
-
-
Save crisleo94/8379ba6da1afb089f899dcc44a8f5414 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Via: | |
def __init__(self, tipo, numero, sentido='NA'): | |
self.tipo = tipo | |
self.numero = numero | |
self.sentido = sentido | |
def show(self): | |
print({ | |
'tipo': self.tipo, | |
'numero': self.numero, | |
'sentido': self.sentido | |
}) | |
class Propietario: | |
def __init__(self, nombre, apellido): | |
self.nombre = nombre | |
self.apellido = apellido | |
def show(self): | |
print({ | |
'nombre': self.nombre, | |
'apellido': self.apellido | |
}) | |
class Vehiculo: | |
def __init__(self, placa, puestos, tipo, propietario='NA'): | |
self.placa = placa | |
self.puestos = puestos | |
self.tipo = tipo | |
self.propietario = propietario | |
def show(self): | |
print({ | |
'placa': self.placa, | |
'puestos': self.puestos, | |
'tipo': self.tipo, | |
'propietario': self.propietario | |
}) | |
class Semaforo: | |
def __init__(self, coordenada, sentido_luces, estado): | |
self.coordenada = coordenada | |
self.sentido_luces = sentido_luces | |
self.estado = estado | |
def show(self): | |
print({ | |
'coordenada': self.coordenada, | |
'sentido_luces': self.sentido_luces, | |
'estado': self.estado | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from modelos import Via, Propietario, Vehiculo, Semaforo | |
def generar_mallado_vial(calles, carreras): | |
malla = [] | |
direccion = { | |
'tipo': ['CALLE', 'CARRERA', 'AV CALLE', 'AV CARRERA'], | |
'sentido': ['NORTE', 'SUR', 'ORIENTE', 'OCCIDENTE', 'DOBLE SENTIDO'] | |
} | |
for calle in range(calles): | |
for carrera in range(carreras): | |
tipo = random.choice(direccion["tipo"]) | |
numero = [random.randrange(0, calles + 1), random.randrange(0, carreras + 1)] | |
nueva_direccion = Via(tipo, numero) | |
numero_total = int(f'{random.randrange(0, calles + 1)}{random.randrange(0, carreras + 1)}') | |
sentido = generar_sentido(tipo, numero_total, direccion['sentido']) | |
nueva_direccion.sentido = sentido | |
if tipo == 'AV CALLE': | |
numero[0] += 11 | |
if tipo == 'AV CARRERA': | |
numero[1] += 11 | |
malla.append(nueva_direccion) | |
return malla | |
def generar_sentido(tipo_direccion, numero_direccion, sentidos): | |
numero = numero_direccion % 2 | |
if tipo_direccion == 'CARRERA' and numero == 0: | |
return sentidos[0] | |
if tipo_direccion == 'CARRERA' and numero != 0: | |
return sentidos[1] | |
if tipo_direccion == 'CALLE' and numero == 0: | |
return sentidos[2] | |
if tipo_direccion == 'CALLE' and numero != 0: | |
return sentidos[3] | |
if tipo_direccion == 'AV CALLE' or tipo_direccion == 'AV CARRERA': | |
return sentidos[4] | |
def generar_habitantes(cantidad): | |
nombres = ['marcos', 'mia', 'carlos', 'kevin'] | |
apellidos = ['suaza', 'arias', 'mora'] | |
habitantes = [] | |
for _ in range(cantidad): | |
ran = random.randrange(0, 3) | |
ran2 = random.randrange(0, 3) | |
if len(nombres[ran]) >= 3 and len(apellidos[ran2]) <= 5: | |
habitante = Propietario(nombres[ran].capitalize(), apellidos[ran2].capitalize()) | |
habitantes.append(habitante) | |
return habitantes | |
def generar_vehiculos(propietarios, cantidad_vehiculos): | |
maximo_vehiculos = int(len(propietarios) * 0.2) + 1 | |
letras = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] | |
numeros = [0,1,2,3,4,5,6,7,8,9] | |
tipos = ['CAMIONETA', 'AUTOMOVIL', 'BUS', 'CAMION', 'MOTO'] | |
vehiculos = [] | |
iterador = 0 | |
if cantidad_vehiculos < maximo_vehiculos: | |
iterador = cantidad_vehiculos | |
else: iterador = maximo_vehiculos | |
for _ in range(iterador): | |
placa = f'{random.choice(letras)}{random.choice(letras)}{random.choice(letras)}{random.choice(numeros)}{random.choice(numeros)}{random.choice(numeros)}', | |
puestos = random.randrange(2, 8), | |
tipo = random.choice(tipos) | |
vehiculo = Vehiculo(placa, puestos, tipo) | |
propietario = random.choice(propietarios) | |
vehiculo.propietario = propietario | |
vehiculos.append(vehiculo) | |
return vehiculos | |
def generar_semaforos(calles, carreras): | |
estados = ['R', 'A', 'V', 'FS'] | |
sentidos = ['NORTE', 'SUR', 'ORIENTE', 'OCCIDENTE'] | |
semaforos = [] | |
for _ in range(calles): | |
for _ in range(carreras): | |
coordenada = [random.randrange(0, calles + 1) + 5, random.randrange(0, carreras + 1) + 5] | |
sentido_luces = random.choice(sentidos) | |
estado = random.choice(estados) | |
semaforo = Semaforo(coordenada, sentido_luces, estado) | |
semaforos.append(semaforo) | |
return semaforos | |
def generar_cuidad(cantidad_calles, cantidad_carreras, cantidad_personas, cantidad_vehiculos): | |
mallado_vial = generar_mallado_vial(cantidad_calles, cantidad_carreras) | |
propietarios = generar_habitantes(cantidad_personas) | |
vehiculos = generar_vehiculos(propietarios, cantidad_vehiculos) | |
semaforos = generar_semaforos(cantidad_calles, cantidad_carreras) | |
print('----------------- MALLADO VIAL -----------------') | |
for direccion in mallado_vial: | |
direccion.show() | |
print('----------------- PROPIETARIOS -----------------') | |
for prop in propietarios: | |
prop.show() | |
print('----------------- VEHICULOS -----------------') | |
for vehiculo in vehiculos: | |
vehiculo.show() | |
print('----------------- SEMAFOROS -----------------') | |
for semaforo in semaforos: | |
semaforo.show() | |
# try: | |
calles = int(input('Ingrese la cantidad de calles: ')) | |
carreras = int(input('Ingrese la cantidad de carreras: ')) | |
personas = int(input('Ingrese la cantidad de personas: ')) | |
vehiculos = int(input('Ingrese la cantidad de vehiculos: ')) | |
generar_cuidad(calles, carreras, personas, vehiculos) | |
# except: | |
# print('Solo se pueden ingresar numeros!!') | |
#generar_mallado_vial(2, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment