127 lines
4.1 KiB
Python
127 lines
4.1 KiB
Python
from asteracer import *
|
|
|
|
|
|
def load_asteroid_graph(path: str):
|
|
with open(path) as file:
|
|
lines = [line.strip() for line in file.readlines()]
|
|
|
|
# Filter out comments and empty lines
|
|
contents = [line for line in lines if line and not line.startswith('#')]
|
|
|
|
iter_lines = iter(contents)
|
|
first_line = list(map(int, next(iter_lines).split()))
|
|
n_racer, n_asteroid, n_goal, m = first_line
|
|
|
|
vertices = []
|
|
edges = []
|
|
vertex_objects = []
|
|
|
|
# Load vertices
|
|
for i in range(n_racer + n_asteroid + n_goal):
|
|
line = list(map(int, next(iter_lines).split()))
|
|
vertices.append((line[0], line[1]))
|
|
|
|
if i < n_racer:
|
|
vertex_objects.append(('S', i))
|
|
elif i < (n_racer + n_asteroid):
|
|
vertex_objects.append(('A', line[2]))
|
|
else:
|
|
vertex_objects.append(('G', line[2]))
|
|
|
|
# Load edges
|
|
for _ in range(m):
|
|
line = list(map(int, next(iter_lines).split()))
|
|
edges.append((line[0], line[1]))
|
|
|
|
return vertices, edges, vertex_objects
|
|
|
|
|
|
if __name__ == "__main__":
|
|
simulation = Simulation.load(f"../mapy/test.txt")
|
|
|
|
print(f"Startovní pozice: {simulation.racer.x} {simulation.racer.y}")
|
|
print(f"Počet asteroidů: {len(simulation.asteroids)}")
|
|
print(f"Počet cílů: {len(simulation.goals)}")
|
|
print()
|
|
|
|
tick = 0
|
|
print("Letím doprava...")
|
|
while True:
|
|
result = simulation.tick(Instruction(127, 0))
|
|
|
|
if result & TickFlag.COLLIDED:
|
|
print(f"Narazili jsme po {tick} instrukcích! Au...")
|
|
print(f"Aktuální pozice: {simulation.racer.x} {simulation.racer.y}")
|
|
print()
|
|
break
|
|
|
|
tick += 1
|
|
|
|
print("Letím dolů (je tam gól)...")
|
|
while True:
|
|
result = simulation.tick(Instruction(0, 127))
|
|
|
|
if result & TickFlag.GOAL_REACHED:
|
|
print(f"Sebrali jsme gól po {tick} instrucích!")
|
|
print(f"Góly sesbírány: {simulation.reached_goals}")
|
|
print(f"Aktuální pozice: {simulation.racer.x} {simulation.racer.y}")
|
|
print()
|
|
break
|
|
|
|
tick += 1
|
|
|
|
# posbíráme zbývající cíle tak, že k nim poletíme přímou čarou
|
|
for _ in range(simulation.reached_goals.count(False)):
|
|
nearest_goal = None
|
|
nearest_goal_distance = float('inf')
|
|
|
|
for i, reached in enumerate(simulation.reached_goals):
|
|
if not reached:
|
|
goal = simulation.goals[i]
|
|
distance = euclidean_distance(goal.x, goal.y, simulation.racer.x, simulation.racer.y)
|
|
|
|
if distance < nearest_goal_distance:
|
|
nearest_goal_distance = distance
|
|
nearest_goal = goal
|
|
|
|
print("Letíme k nejbližšímu cíli přímou čarou...")
|
|
collided_count = 0
|
|
while True:
|
|
instruction = Instruction(
|
|
nearest_goal.x - simulation.racer.x,
|
|
nearest_goal.y - simulation.racer.y,
|
|
)
|
|
|
|
result = simulation.tick(instruction)
|
|
|
|
if result & TickFlag.COLLIDED:
|
|
collided_count += 1
|
|
|
|
if result & TickFlag.GOAL_REACHED:
|
|
print(f"Sebrali jsme další gól po {tick} instrucích!")
|
|
print(f"Počet nárazů po cestě: {collided_count}")
|
|
print(f"Góly sesbírány: {simulation.reached_goals}")
|
|
print()
|
|
break
|
|
|
|
tick += 1
|
|
|
|
print(f"Hotovo!")
|
|
print()
|
|
assert simulation.finished()
|
|
|
|
print("Načítám graf...")
|
|
# - vertices jsou (x, y) pozice vrcholů
|
|
# - edges jsou (u, v) dvojice vrcholů tvořících hranu
|
|
# - vertex_objects jsou dvojice (typ, index), kde
|
|
# typ je typ objektu vrcholu ('A' asteroid, 'G' gól a 'S' loď)
|
|
# a index je kolikátý je to objekt (t.j. kolikátý asteroid, gól, apod.)
|
|
vertices, edges, vertex_objects = load_asteroid_graph("../grafy/test.txt")
|
|
|
|
print(f"Má {len(vertices)} vrcholů a {len(edges)} hran, vypisuji prvních 5:")
|
|
print(f"-> vrcholy: {vertices[:5]}")
|
|
print(f"-> hrany: {edges[:5]}")
|
|
print(f"-> objekty: {vertex_objects[:5]}")
|
|
|
|
|
|
|