-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai_player.py
82 lines (66 loc) · 2.43 KB
/
ai_player.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
import time
import dots_player
import bfs
import random
class AI_player(dots_player.Player):
def __init__(self, level, number, name):
dots_player.Player.__init__(self, number, name)
self.level = level
def get_step(self, player_step, player_dots, map, used_dots, caught_dots):
time.sleep(0.2)
if self.level:
return get_smart_step(player_dots, map, used_dots, caught_dots)
return get_prime_step(player_step, map, used_dots)
def get_prime_step(player_step, map, used_dots):
steps = list(bfs.get_neighbours(player_step))
n = random.randint(0, 8)
for i in range(n, n + 8):
step = steps[i % len(steps)]
if step == player_step:
continue
if step not in used_dots and map.in_bounds(step):
return step
return get_default_step(map, used_dots)
def get_default_step(map, used_dots):
for dot in map.get_all_dots():
if dot not in used_dots and map.in_bounds(dot):
return dot
def get_smart_step(enemy_dots, map, used_dots, caught_dots):
max_points_around = -1
best_dot = None
for dot in enemy_dots:
if dot in caught_dots or not _has_empty_space(dot, used_dots, map):
continue
neighbours = list(bfs.get_neighbours(dot))
near_friendlies = 0
for neighbour in neighbours:
if neighbour in used_dots and neighbour not in enemy_dots:
near_friendlies += 1
if near_friendlies >= max_points_around:
max_points_around = near_friendlies
best_dot = dot
steps = _get_directions_in_priority(best_dot, map)
for step in steps:
if step == best_dot:
continue
if step not in used_dots and map.in_bounds(step):
return step
print("default_step")
return get_default_step(map, used_dots)
def _get_directions_in_priority(dot, map):
res = []
pos_dir = list(bfs.get_possible_directions(dot))
for dir in pos_dir:
if map.in_bounds(dir):
res.append(dir)
neighbors = bfs.get_neighbours(dot)
for neighbor in neighbors:
if neighbor not in res and map.in_bounds(neighbor):
res.append(neighbor)
return res
def _has_empty_space(dot, used_dots, map):
neighbours = list(bfs.get_neighbours(dot))
for neighbour in neighbours:
if neighbour not in used_dots and map.in_bounds(neighbour):
return True
return False