-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleBot.py
More file actions
335 lines (270 loc) · 10.4 KB
/
ModuleBot.py
File metadata and controls
335 lines (270 loc) · 10.4 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
"""
The module for the participant
"""
from typing import List, Literal
from random import choice, randint
from math import sqrt
from TankLib import Bullet, Tank, TankAction, StageData
# TODO : Change bots for lvl 9 and 10 (same as lvl 8) and improve others
LAST_MOVE: List[Literal["UP", "DOWN", "LEFT", "RIGHT"]] = []
COUNTER: List[int] = []
def choose_rand_direction(tank: Tank, stage: StageData) -> Literal["UP", "DOWN", "LEFT", "RIGHT"]:
"""
The function for the direction
"""
ind: int = stage.enemies.index(tank)
if len(LAST_MOVE) <= ind:
LAST_MOVE.append("UP")
COUNTER.append(0)
COUNTER[ind] += 1
if COUNTER[ind] == 20:
LAST_MOVE[ind] = choice(["UP", "DOWN", "LEFT", "RIGHT"])
COUNTER[ind] = 0
return LAST_MOVE[ind]
def get_fastest(tank: Tank, stage: StageData) -> Literal["UP", "DOWN", "LEFT", "RIGHT"]:
"""
The function to get the fastest way to go to player
"""
max_dist: tuple[int, int] = (abs(tank.pos_x - stage.player.pos_x), abs(tank.pos_y - stage.player.pos_y))
if max_dist[0] > max_dist[1]:
if tank.pos_x < stage.player.pos_x:
return "RIGHT"
return "LEFT"
if tank.pos_y < stage.player.pos_y:
return "DOWN"
return "UP"
def get_walls_colide(bullet: Bullet, final: tuple[int, int], stage: StageData) -> bool:
"""
Return if the bullet colide with a wall
"""
x: float = bullet.pos_x
y: float = bullet.pos_y
divider: float = sqrt(bullet.direction[0]**2 + bullet.direction[1]**2)
unit_vector: tuple[float, float] = (bullet.direction[0] / divider, bullet.direction[1] / divider)
checker_x: bool = final[0] > x if unit_vector[0] > 0 else final[0] < x
checker_y: bool = final[1] > y if unit_vector[1] > 0 else final[1] < y
while checker_x or checker_y:
if any(a_wall.is_in_wall(x, y, bullet.size, bullet.size) for a_wall in stage.walls):
return True
if x < 0 or x > stage.width or y < 0 or y > stage.height:
return True
x += unit_vector[0]
y += unit_vector[1]
checker_x = final[0] > x if unit_vector[0] > 0 else final[0] < x
checker_y = final[1] > y if unit_vector[1] > 0 else final[1] < y
return False
def choose_shooting_direction(tank: Tank, stage: StageData) -> tuple[int, int]:
"""
The function for the shooting direction
"""
direction: tuple[int, int] = (stage.player.pos_x - tank.pos_x, stage.player.pos_y - tank.pos_y)
test: bool = get_walls_colide(Bullet(tank.pos_x, tank.pos_y, direction, stage.bullet_size), (stage.player.pos_x, stage.player.pos_y), stage)
if not test:
return direction
return (0, 0)
def get_tank_collision(bullet: Bullet, tank: Tank, stage: StageData) -> bool:
"""
Return if the bullet colide with the tank
"""
x: float = bullet.pos_x
y: float = bullet.pos_y
divider: float = sqrt(bullet.direction[0]**2 + bullet.direction[1]**2)
unit_vector: tuple[float, float] = (bullet.direction[0] / divider, bullet.direction[1] / divider)
while True:
if any(a_wall.is_in_wall(x, y, bullet.size, bullet.size) for a_wall in stage.walls):
return False
if x < 0 or x > stage.width or y < 0 or y > stage.height:
return False
if tank.is_in_tank(x, y, bullet.size, bullet.size):
return True
x += unit_vector[0]
y += unit_vector[1]
def destroy_player_bullet(tank: Tank, stage: StageData) -> tuple[int, int]:
"""
The function to destroy the player's bullet that are near the tank
"""
player: Tank = stage.player
shoot_direction: tuple[int, int] = (0, 0)
direction: tuple[int, int]
dist: int
min_dist: int = 100
for bullet in player.bullets:
dist = (bullet.pos_x - tank.pos_x)**2 + (bullet.pos_y - tank.pos_y)**2
if dist < min_dist:
direction = (bullet.pos_x - tank.pos_x, bullet.pos_y - tank.pos_y)
if get_tank_collision(bullet, tank, stage):
min_dist = dist
shoot_direction = direction
return shoot_direction
def bot_move_1(stage: StageData, tank: Tank) -> TankAction:
"""
Level 1 Bot
"""
action: TankAction = TankAction()
action.direction = choose_rand_direction(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets
if action.shoot:
action.shoot_direction = (choice([randint(-10, -1), randint(1, 10)]), choice([randint(-10, -1), randint(1, 10)]))
return action
def bot_move_2(stage: StageData, tank: Tank) -> TankAction:
"""
Level 2 Bot
"""
action: TankAction = TankAction()
action.direction = get_fastest(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets
if action.shoot:
action.shoot_direction = (choice([randint(-10, -1), randint(1, 10)]), choice([randint(-10, -1), randint(1, 10)]))
return action
def bot_move_3(stage: StageData, tank: Tank) -> TankAction:
"""
Level 3 Bot
"""
action: TankAction = TankAction()
action.direction = choose_rand_direction(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets
if action.shoot:
action.shoot_direction = choose_shooting_direction(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot = False
return action
def bot_move_4(stage: StageData, tank: Tank) -> TankAction:
"""
Level 4 Bot
"""
action: TankAction = TankAction()
action.direction = get_fastest(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets
if action.shoot:
action.shoot_direction = choose_shooting_direction(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot = False
return action
def bot_move_5(stage: StageData, tank: Tank) -> TankAction:
"""
Level 5 Bot
"""
action: TankAction = TankAction()
action.direction = choose_rand_direction(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets
if action.shoot:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot_direction = choose_shooting_direction(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot = False
return action
def bot_move_6(stage: StageData, tank: Tank) -> TankAction:
"""
Level 6 Bot
"""
action: TankAction = TankAction()
action.direction = get_fastest(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets
if action.shoot:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot_direction = choose_shooting_direction(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot = False
return action
def bot_move_7(stage: StageData, tank: Tank) -> TankAction:
"""
Level 7 Bot
"""
action: TankAction = TankAction()
action.direction = choose_rand_direction(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets - 1
if action.shoot:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot_direction = choose_shooting_direction(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot = False
else:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction != (0, 0):
action.shoot = True
return action
def bot_move_8(stage: StageData, tank: Tank) -> TankAction:
"""
Level 8 Bot
"""
action: TankAction = TankAction()
action.direction = get_fastest(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets - 1
if action.shoot:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot_direction = choose_shooting_direction(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot = False
else:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction != (0, 0):
action.shoot = True
return action
def bot_move_9(stage: StageData, tank: Tank) -> TankAction:
"""
Level 9 Bot
"""
action: TankAction = TankAction()
action.direction = get_fastest(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets - 1
if action.shoot:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot_direction = choose_shooting_direction(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot = False
else:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction != (0, 0):
action.shoot = True
return action
def bot_move_10(stage: StageData, tank: Tank) -> TankAction:
"""
Level 10 Bot
"""
action: TankAction = TankAction()
action.direction = get_fastest(tank, stage)
action.shoot = len(tank.bullets) < tank.max_bullets - 1
if action.shoot:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot_direction = choose_shooting_direction(tank, stage)
if action.shoot_direction == (0, 0):
action.shoot = False
else:
action.shoot_direction = destroy_player_bullet(tank, stage)
if action.shoot_direction != (0, 0):
action.shoot = True
return action
def bot_move(stage: StageData, tank: Tank, bot: int) -> TankAction:
"""
The function for the movement
Need to return a tuple who specify action to do for player's tank
"""
player_action: TankAction = TankAction()
match bot:
case 1:
player_action = bot_move_1(stage, tank)
case 2:
player_action = bot_move_2(stage, tank)
case 3:
player_action = bot_move_3(stage, tank)
case 4:
player_action = bot_move_4(stage, tank)
case 5:
player_action = bot_move_5(stage, tank)
case 6:
player_action = bot_move_6(stage, tank)
case 7:
player_action = bot_move_7(stage, tank)
case 8:
player_action = bot_move_8(stage, tank)
case 9:
player_action = bot_move_9(stage, tank)
case 10:
player_action = bot_move_10(stage, tank)
return player_action