-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.py
120 lines (103 loc) · 4.76 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""This is an example of a bot participating in the HackArena 2.0.
This bot will randomly move around the map,
use abilities and print the map to the console.
"""
import os
import random
from hackathon_bot import *
class ExampleBot(HackathonBot):
def on_lobby_data_received(self, lobby_data: LobbyData) -> None:
print(f"Lobby data received: {lobby_data}")
def next_move(self, game_state: GameState) -> ResponseAction:
self._print_map(game_state.map)
# Check if the agent is dead
if game_state.my_agent.is_dead:
# Return pass to avoid warnings from the server
# when the bot tries to make an action with a dead tank
return Pass()
return self._get_random_action()
def on_game_ended(self, game_result: GameResult) -> None:
print(f"Game ended: {game_result}")
def on_warning_received(self, warning: WarningType, message: str | None) -> None:
print(f"Warning received: {warning} - {message}")
def _get_random_action(self):
return random.choice(
[
Movement(MovementDirection.FORWARD),
Movement(MovementDirection.BACKWARD),
Rotation(RotationDirection.LEFT, RotationDirection.LEFT),
Rotation(RotationDirection.LEFT, RotationDirection.RIGHT),
Rotation(RotationDirection.LEFT, None),
Rotation(RotationDirection.RIGHT, RotationDirection.LEFT),
Rotation(RotationDirection.RIGHT, RotationDirection.RIGHT),
Rotation(RotationDirection.RIGHT, None),
Rotation(None, RotationDirection.LEFT),
Rotation(None, RotationDirection.RIGHT),
Rotation(None, None), # Useless, better use Pass()
AbilityUse(Ability.FIRE_BULLET),
AbilityUse(Ability.FIRE_DOUBLE_BULLET),
AbilityUse(Ability.USE_LASER),
AbilityUse(Ability.USE_RADAR),
AbilityUse(Ability.DROP_MINE),
Pass(),
]
)
def _print_map(self, game_map: Map):
os.system("cls" if os.name == "nt" else "clear")
end = " "
for row in game_map.tiles:
for tile in row:
entity = tile.entities[0] if tile.entities else None
if isinstance(entity, Wall):
print("#", end=end)
elif isinstance(entity, Laser):
if entity.orientation is Orientation.HORIZONTAL:
print("|", end=end)
elif entity.orientation is Orientation.VERTICAL:
print("-", end=end)
elif isinstance(entity, DoubleBullet):
if entity.direction == Direction.UP:
print("⇈", end=end)
elif entity.direction == Direction.RIGHT:
print("⇉", end=end)
elif entity.direction == Direction.DOWN:
print("⇊", end=end)
elif entity.direction == Direction.LEFT:
print("⇇", end=end)
elif isinstance(entity, Bullet):
if entity.direction is Direction.UP:
print("↑", end=end)
elif entity.direction is Direction.RIGHT:
print("→", end=end)
elif entity.direction is Direction.DOWN:
print("↓", end=end)
elif entity.direction is Direction.LEFT:
print("←", end=end)
elif isinstance(entity, AgentTank):
print("A", end=end)
elif isinstance(entity, PlayerTank):
print("P", end=end)
elif isinstance(entity, Mine):
print("x" if entity.exploded else "X", end=end)
elif isinstance(entity, Item):
match (entity.type):
case SecondaryItemType.DOUBLE_BULLET:
print("D", end=end)
case SecondaryItemType.LASER:
print("L", end=end)
case SecondaryItemType.MINE:
print("M", end=end)
case SecondaryItemType.RADAR:
print("R", end=end)
elif tile.zone:
index = chr(tile.zone.index)
index = index.upper() if tile.is_visible else index.lower()
print(index, end=end)
elif tile.is_visible:
print(".", end=end)
else:
print(" ", end=end)
print()
if __name__ == "__main__":
bot = ExampleBot()
bot.run()