21-05-2026
82 lines
2.1 kB
1from ex0 import (
2 FlameFactory,
3 AquaFactory,
4 CreatureFactory
5)
6from ex1 import (
7 HealingCreatureFactory,
8 TransformCreatureFactory
9)
10from ex2 import (
11 BattleStrategy,
12 NormalStrategy,
13 AggressiveStrategy,
14 DefensiveStrategy,
15 InvalidStrategyError
16)
17
18
19def battle(opponents: list[tuple[CreatureFactory, BattleStrategy]]) -> None:
20 print("*** Tournament ***")
21 print(f"{len(opponents)} opponents involved")
22 fighters = [(factory.create_base(), strat) for factory, strat in opponents]
23 for i in range(len(fighters)):
24 for j in range(i + 1, len(fighters)):
25 creature1, strategy1 = fighters[i]
26 creature2, strategy2 = fighters[j]
27 print("\n* Battle *")
28 print(creature1.describe())
29 print(" vs.")
30 print(creature2.describe())
31 print(" now fight!")
32 strategy1.act(creature1)
33 strategy2.act(creature2)
34
35
36def run_tournament(
37 opponents: list[tuple[CreatureFactory, BattleStrategy]]
38) -> None:
39 try:
40 battle(opponents)
41 except InvalidStrategyError as e:
42 print(
43 f"Battle error, aborting tournament: {e}"
44 )
45
46
47def main() -> None:
48 normal = NormalStrategy()
49 aggressive = AggressiveStrategy()
50 defensive = DefensiveStrategy()
51
52 print("Tournament 0 (basic)")
53 print(
54 " [ (Flameling+Normal), (Healing+Defensive) ]"
55 )
56 run_tournament(
57 [(FlameFactory(), normal), (HealingCreatureFactory(), defensive)]
58 )
59 print()
60
61 print("Tournament 1 (error)")
62 print(
63 " [ (Flameling+Aggressive), (Healing+Defensive) ]"
64 )
65 run_tournament(
66 [(FlameFactory(), aggressive), (HealingCreatureFactory(), defensive)]
67 )
68 print()
69
70 print("Tournament 2 (multiple)")
71 print(
72 " [ (Aquabub+Normal), (Healing+Defensive), (Transform+Aggressive) ]"
73 )
74 run_tournament([
75 (AquaFactory(), normal),
76 (HealingCreatureFactory(), defensive),
77 (TransformCreatureFactory(), aggressive)
78 ])
79
80
81if __name__ == "__main__":
82 main()