86 lines
2.4 kB
1"""The interface every view of a run implements."""
2
3from abc import ABC, abstractmethod
4
5from ..scenario import Scenario
6from ..schedule import Move
7
8
9class Renderer(ABC):
10 """Shows a run as it is replayed, one turn at a time.
11
12 The simulator drives these calls while it walks the schedule, so a
13 view never has to replay the run itself. A graphical view animates
14 inside show_turn; the text view simply prints.
15 """
16
17 @abstractmethod
18 def begin(self, scenario: Scenario) -> None:
19 """Prepare to show a run.
20
21 Args:
22 scenario: The map and drone count about to be replayed.
23 """
24
25 @abstractmethod
26 def show_turn(self, index: int, moves: list[Move]) -> None:
27 """Show one turn of the run.
28
29 Args:
30 index: 1-based number of the turn.
31 moves: The drones that moved; others held their position.
32 """
33
34 @abstractmethod
35 def finish(self, total_turns: int) -> None:
36 """Close the view once every drone has been delivered.
37
38 Args:
39 total_turns: Length of the run, which is its score.
40 """
41
42
43class Broadcast(Renderer):
44 """Shows one run on several views at once.
45
46 It exists so src.simulate.Simulation.run keeps taking a
47 single renderer while the graphical view runs alongside the text
48 one. Widening the simulator to accept a list would move this loop
49 into the part of the program that enforces the rules.
50 """
51
52 def __init__(self, *views: Renderer) -> None:
53 """Group several views behind one.
54
55 Args:
56 *views: The views to show the run on, in order.
57 """
58 self._views = views
59
60 def begin(self, scenario: Scenario) -> None:
61 """Open every view.
62
63 Args:
64 scenario: The map and drone count about to be replayed.
65 """
66 for view in self._views:
67 view.begin(scenario)
68
69 def show_turn(self, index: int, moves: list[Move]) -> None:
70 """Show one turn on every view.
71
72 Args:
73 index: 1-based number of the turn.
74 moves: The drones that moved.
75 """
76 for view in self._views:
77 view.show_turn(index, moves)
78
79 def finish(self, total_turns: int) -> None:
80 """Close every view.
81
82 Args:
83 total_turns: Length of the run.
84 """
85 for view in self._views:
86 view.finish(total_turns)