Make day 15 viz prettier

This commit is contained in:
Sijmen 2018-12-18 15:06:26 +01:00
parent 6acee8422d
commit 1c227f6906
1 changed files with 12 additions and 16 deletions

View File

@ -14,7 +14,7 @@ class Elf:
self.targeted = False self.targeted = False
def __repr__(self): def __repr__(self):
return "E" return "\033[32mE\033[0m"
class Goblin: class Goblin:
@ -28,7 +28,7 @@ class Goblin:
self.targeted = False self.targeted = False
def __repr__(self): def __repr__(self):
return "G" return "\033[31mG\033[0m"
Elf.enemy = Goblin Elf.enemy = Goblin
@ -41,7 +41,7 @@ class Wall:
self.targeted = False self.targeted = False
def __repr__(self): def __repr__(self):
return "#" return "\u2588"
class Empty: class Empty:
@ -51,7 +51,7 @@ class Empty:
self.targeted = False self.targeted = False
def __repr__(self): def __repr__(self):
return "." if not self.targeted else "\033[33m?\033[0m" return " " if not self.targeted else "\033[33m?\033[0m"
NEIGHBORS = [(0, -1), (-1, 0), (1, 0), (0, 1)] NEIGHBORS = [(0, -1), (-1, 0), (1, 0), (0, 1)]
@ -121,10 +121,6 @@ def find_closest(field, pos, targets):
def move(field, x, y, value): def move(field, x, y, value):
for row in field:
for col in row:
col.targeted = False
targets = [] targets = []
for tx, ty in value.enemy.locations: for tx, ty in value.enemy.locations:
targets.extend([(tx + dx, ty + dy) for dx, dy in NEIGHBORS]) targets.extend([(tx + dx, ty + dy) for dx, dy in NEIGHBORS])
@ -132,9 +128,6 @@ def move(field, x, y, value):
if not targets: if not targets:
raise StopIteration raise StopIteration
for tx, ty in targets:
field[ty][tx].targeted = True
closest = find_closest(field, (x, y), targets) closest = find_closest(field, (x, y), targets)
# print_field(field, highlight=(x, y), path=closest) # print_field(field, highlight=(x, y), path=closest)
# time.sleep(0.3) # time.sleep(0.3)
@ -176,8 +169,9 @@ def attack(field, x, y, value):
def print_field(field, steps=None, highlight=None, path=None): def print_field(field, steps=None, highlight=None, path=None):
if steps is not None: print("\033[2J\033[;HElf attack power:", Elf.power)
print(f"round={steps}") # if steps is not None:
# print(f"round={steps}")
for y, row in enumerate(field): for y, row in enumerate(field):
units = [] units = []
for x, value in enumerate(row): for x, value in enumerate(row):
@ -194,10 +188,11 @@ def print_field(field, steps=None, highlight=None, path=None):
etc = sorted(units, key=lambda unit: (str(unit), unit.health)) etc = sorted(units, key=lambda unit: (str(unit), unit.health))
print(" ", ", ".join(f"{x}({x.health})" for x in etc)) print(" ", ", ".join(f"{x}({x.health})" for x in etc))
print() print()
time.sleep(0.05)
def step(field, steps): def step(field, steps):
# print_field(field, steps) print_field(field, steps)
seen_units = set() seen_units = set()
for y, row in enumerate(field): for y, row in enumerate(field):
for x, value in enumerate(row): for x, value in enumerate(row):
@ -235,8 +230,9 @@ def main():
while True: while True:
main() main()
if Elf.deaths == 0: if Elf.deaths == 0:
print("yay") print("No elves died! :D")
break break
Elf.power += 1 Elf.power += 1
print("oh no :(, trying", Elf.power) print(f"{Elf.deaths} elves died, trying power={Elf.power}")
time.sleep(4)