Visually displays the Wa-Tor planet using an ascii code in terminal to clear and re-print the Wa-Tor planet at intervals. Uses ascii colour codes to colourfully display the predators and prey: * (0x60f197) Prey = ``#`` * (0xfffff) Predator = ``x`` >>> wt = WaTo
(wt: WaTor, iter_number: int, *, colour: bool = True)
| 479 | |
| 480 | |
| 481 | def visualise(wt: WaTor, iter_number: int, *, colour: bool = True) -> None: |
| 482 | """ |
| 483 | Visually displays the Wa-Tor planet using |
| 484 | an ascii code in terminal to clear and re-print |
| 485 | the Wa-Tor planet at intervals. |
| 486 | |
| 487 | Uses ascii colour codes to colourfully display the predators and prey: |
| 488 | * (0x60f197) Prey = ``#`` |
| 489 | * (0xfffff) Predator = ``x`` |
| 490 | |
| 491 | >>> wt = WaTor(30, 30) |
| 492 | >>> wt.set_planet([ |
| 493 | ... [Entity(True, coords=(0, 0)), Entity(False, coords=(0, 1)), None], |
| 494 | ... [Entity(False, coords=(1, 0)), None, Entity(False, coords=(1, 2))], |
| 495 | ... [None, Entity(True, coords=(2, 1)), None] |
| 496 | ... ]) |
| 497 | >>> visualise(wt, 0, colour=False) # doctest: +NORMALIZE_WHITESPACE |
| 498 | # x . |
| 499 | x . x |
| 500 | . # . |
| 501 | <BLANKLINE> |
| 502 | Iteration: 0 | Prey count: 2 | Predator count: 3 | |
| 503 | """ |
| 504 | if colour: |
| 505 | __import__("os").system("") |
| 506 | print("\x1b[0;0H\x1b[2J\x1b[?25l") |
| 507 | |
| 508 | reprint = "\x1b[0;0H" if colour else "" |
| 509 | ansi_colour_end = "\x1b[0m " if colour else " " |
| 510 | |
| 511 | planet = wt.planet |
| 512 | output = "" |
| 513 | |
| 514 | # Iterate over every entity in the planet |
| 515 | for row in planet: |
| 516 | for entity in row: |
| 517 | if entity is None: |
| 518 | output += " . " |
| 519 | else: |
| 520 | if colour is True: |
| 521 | output += ( |
| 522 | "\x1b[38;2;96;241;151m" |
| 523 | if entity.prey |
| 524 | else "\x1b[38;2;255;255;15m" |
| 525 | ) |
| 526 | output += f" {'#' if entity.prey else 'x'}{ansi_colour_end}" |
| 527 | |
| 528 | output += "\n" |
| 529 | |
| 530 | entities = wt.get_entities() |
| 531 | prey_count = sum(entity.prey for entity in entities) |
| 532 | |
| 533 | print( |
| 534 | f"{output}\n Iteration: {iter_number} | Prey count: {prey_count} | " |
| 535 | f"Predator count: {len(entities) - prey_count} | {reprint}" |
| 536 | ) |
| 537 | # Block the thread to be able to visualise seeing the algorithm |
| 538 | sleep(0.05) |
nothing calls this directly
no test coverage detected