-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm_bot.py
More file actions
151 lines (130 loc) · 5.16 KB
/
algorithm_bot.py
File metadata and controls
151 lines (130 loc) · 5.16 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
import rgkit.rg as rg
class Robot:
def act(self, game):
self.game = game
spawn_test = self._spawn_action()
# if game.turn == 1:
# for i in range(19):
# for j in range(19):
# spot = rg.loc_types((i,j))
# print "i:",i, "j:",j,"spot", spot
if spawn_test:
return spawn_test
next_step = self._near_spawn_action()
if next_step:
return next_step
return ["guard"]
def _near_spawn_action(self):
normal, spawn = Robot._surrounding(self.location)
if spawn:
s_occu, s_unoccu = Robot._occupied_find(spawn, self.game)
if s_occu:
for i in s_occu:
friend_test = Robot._test_friend_bot(self.player_id, i, self.game)
if friend_test == "friend":
return ["move", rg.toward(self.location, rg.CENTER_POINT)]
else:
return ["attack", i]
if normal:
o_occu, o_unoccu = Robot._occupied_find(normal, self.game)
if o_occu:
for i in o_occu:
friend_test = Robot._test_friend_bot(self.player_id, i, self.game)
if friend_test == "friend":
continue
return ["attack", i]
return ['guard']
def _spawn_action(self):
if Robot._spawn_test(self.location):
normal, spawn = Robot._surrounding(self.location)
if normal:
n_occu, n_unoccu = Robot._occupied_find(normal, self.game)
if n_unoccu:
return ["move", rg.toward(self.location, n_unoccu[0])]
else:
for i in n_occu:
friend_test = Robot._test_friend_bot(self.player_id, i, self.game)
if friend_test == "friend":
pass
else:
return ["attack", i]
if spawn:
s_occu, s_unoccu = Robot._occupied_find(spawn, self.game)
if s_unoccu:
return ["move", rg.toward(self.location, s_unoccu[0])]
else:
return None
@staticmethod
def _spawn_test(location):
"""test input location is at spawn place or not
Args:
location (tuple): location of the map
Returns:
bool: True if the place is a spawnplace
False if not
"""
spot = rg.loc_types(location)
if ("spawn") in spot:
return True
else:
return False
@staticmethod
def _surrounding(location):
"""test closest place location and property
Args:
location (tuple): location of the map
Returns:
tuple: return[0], list, a list of location of normal places
return[1], list, a list of location of spawn places
"""
normal = rg.locs_around(location, filter_out=('invalid', 'obstacle', 'spawn'))
spawn = rg.locs_around(location, filter_out=('invalid', 'obstacle'))
new_spawn = [val for val in spawn if val not in normal]
# intersection = [val for val in normal if val in new_spawn]
# print "intersection", intersection
return normal, new_spawn
@staticmethod
def _occupied_find(locs, game):
"""test given location is occupied by a bot or not
Args:
locs (tuple): location of a map
game (a information type of rg): information for present map
Returns:
tuple: return[0], list, a list of location been occupied
return[1], list, a list of location haven't been oocupied
"""
occupied = []
unoccupied = []
loc_len = len(locs)
condition = [None] * loc_len
for i in range(loc_len):
if locs[i] in game.robots:
condition[i] = "occupied"
occupied.append(locs[i])
else:
condition[i] = "unoccupied"
unoccupied.append(locs[i])
return occupied, unoccupied
@staticmethod
def _test_friend_bot(my_player_id, loc, game):
"""test a bot on a location is a friendly one or not
Args:
my_player_id (attribute of bot object): an id to tell which player
loc (tuple): location of the game
game (game info): a game info data structure
Returns:
string: 'friend' for a friend bot
'enemy' for a enemy bot
"""
bot_player_id = game.robots[loc]['player_id']
if bot_player_id == my_player_id:
return "friend"
else:
return "enemy"
# if self.location == rg.CENTER_POINT
# return ["guard"]
# for loc, bot in game.robots.iteritems():
# if bot.player_id != self.player_id:
# if rg.dist(loc, self.location) <= 1:
# return ["attack", loc]
# return ["move", rg.toward(self.location, rg.CENTER_POINT)]