-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot_TNT.py
More file actions
310 lines (210 loc) · 8.65 KB
/
bot_TNT.py
File metadata and controls
310 lines (210 loc) · 8.65 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
from init_game import general
import logging
from generals_io_client import generals
import math
enermy_capital_value=1000
enermy_city_value=100
empty_city_value=50
our_city_value=10
empty_tile_value=4
enerymy_tile_value=2
logging.basicConfig(level=logging.DEBUG)
# first_update=general.get_updates()[0]
# rows=first_update['rows']
# cols=first_update['cols']
# our_flag=first_update['player_index']
# general_y, general_x =first_update['generals'][our_flag]
turn=0
rows=20
cols=20
our_flag=0
general_y, general_x =0,0
general_position=(general_y,general_x)
tiles=[]
armies=[]
cities=[]
generals_list=[]
def position_plus(a,b):
return (a[0]+b[0],a[1]+b[1])
def grid_to_index(y,x):
return y*cols+x
def index_to_grid(index):
return index/cols, index%cols
# def get_distance(y1,x1,y2,x2):
# return math.sqrt(
# math.pow(y1-y2,2)-math.pow(x1-x2,2)
# )
def get_distance(position1,position2):
return math.fabs(position1[0]-position2[0])+math.fabs(position1[1]-position2[1])
def how_far_from_general(position):
return (position, general_position)
# def is_inland(y,x):
#
# if y+1<cols and y-1>=0 and x-1>=0 and x+1<rows \
# and tiles[y][x+1] ==our_flag and tiles[y][x-1]==our_flag \
# and tiles[y-1][x]==our_flag and tiles[y+1][x]==our_flag:
# return True;
def get_tiles_with_priority():
destinations_with_priority={}
tiles_we_see=[]
for y in range(0,len(tiles)):
for x in range(0, len(tiles[y])):
is_fog=tiles[y][x] ==generals.FOG
is_obstacle=tiles[y][x]==generals.OBSTACLE
# what tiles we can see
if (is_fog==False and is_obstacle==False ):
tiles_we_see.append((y,x)) #get what we see in the map
# these two lines of codes will make the generals and city be remebered
is_a_capital = (y, x) in generals_list
is_a_city = (y, x) in cities
is_ours = tiles[y][x] == our_flag
is_empty = tiles[y][x] == generals.EMPTY
have_soldiers = armies[y][x] >0
p = get_priority_of_destination(is_a_capital, is_a_city, is_ours, is_empty, have_soldiers)
if p > 0:
destinations_with_priority[(y, x)] = p
# is_in_fog= tiles[y][x] ==generals.FOG
# is_obstacle = tiles[y][x] == generals.OBSTACLE
# how_many_armies=armies[y][x] ## only visible
# how_far=how_far_from_general((y,x))
# return priorities_of_destinations
return destinations_with_priority, rank_all_we_see(destinations_with_priority,tiles_we_see)
def get_priority_of_destination(is_a_capital, is_a_city, is_ours, is_empty, have_soldiers):
p = 0
if is_a_capital and is_ours ==False : #enermies capital
p += enermy_capital_value
elif is_a_city:
if is_ours: #our city
p+=our_city_value
elif is_empty: # empty city
p += empty_city_value
else: # it belongs to enermies
p += enermy_city_value
elif is_empty: # empty tile
p += empty_tile_value
elif is_ours == False and have_soldiers: #enermy tile
p += enerymy_tile_value
return p
def rank_all_we_see(destinations, tiles_we_see):
# print tiles_ranked
tiles_we_rank={}
for destination_position in destinations:
priority = destinations[destination_position]
for tile in tiles_we_see:
distance = get_distance(destination_position, tile)
if (tile !=generals.MOUNTAIN):
p=get_priority_of_the_tile(priority,distance)
if tile in destinations:
p=priority
if tile in tiles_we_rank:
tiles_we_rank[tile]+=p
else:
tiles_we_rank[tile] = p
return tiles_we_rank
def get_priority_of_the_tile(priority, distance ):
# print priority
# print distance
# print cols+rows
# print cols
p=(-math.log(distance+1, cols+rows)+1)*priority # 0: +infinate, cols:0, 1:1
# print p
return float ("{0:.3f}".format(p))
# def what_tiles_we_see():
# tiles_we_see = []
# for y in range(0,len(tiles)):
# for x in range(0, len(tiles[y])):
# if ((tiles[y][x] !=generals.FOG and tiles[y][x]!=generals.OBSTACLE ) ):
# tiles_we_see.append((y,x)) #get what we see in the map
# return tiles_we_see
def what_tiles_we_have():
armies_we_have={}
tiles_we_own=[]
borders=[]
inlands=[]
for y in range(0,len(tiles)):
for x in range(0, len(tiles[y])):
if tiles[y][x] ==our_flag:
# we own this places
armies_we_have[(y,x)]=armies[y][x]
tiles_we_own.append((y,x))
# if is_inland(y,x):
# inlands.append((y,x))
# else:
# borders.append((y,x))
return armies_we_have, tiles_we_own,borders,inlands
for state in general.get_updates():
# get position of your general
our_flag = state['player_index']
try:
general_y, general_x = state['generals'][our_flag]
except KeyError:
break
rows, cols = state['rows'], state['cols']
turn = state['turn']
tiles = state['tile_grid']
armies = state['army_grid']
cities = state['cities']
generals_list = state['generals']
# move_to units from general to arbitrary square
# for dy, dx in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
# if (0 <= general_y+dy < state['rows'] and 0 <= general_x+dx < state['cols']
# and state['tile_grid'][general_y+dy][general_x+dx] != generals.MOUNTAIN):
# general.move(general_y, general_x, general_y+dy, general_x+dx)
# break
# for k,v in state:
# print '%s : %s' %(k,v)
# print state
# explore(general_y, general_x, get_radius(rows, cols))
# print armies
# move_to(general_y, general_x, general_y + 0, general_x + 1)
# move_to(general_y, general_x, general_y - 0, general_x - 1)
# move_to(general_y, general_x, general_y + 1, general_x -0)
# move_to(general_y, general_x, general_y - 1, general_x - 0)
# if tiles[general_y+1][general_x] !=generals.MOUNTAIN and armies[general_y+1][general_x]==0:
# general.move(general_y,general_x,general_y+1,general_x)
# if tiles[general_y-1][general_x] !=generals.MOUNTAIN and armies[general_y-1][general_x]==0:
# general.move(general_y,general_x,general_y-1,general_x)
# if tiles[general_y][general_x+1] !=generals.MOUNTAIN and armies[general_y][general_x+1]==0:
# general.move(general_y,general_x,general_y,general_x+1)
# if tiles[general_y][general_x-1] !=generals.MOUNTAIN and armies[general_y][general_x-1]==0:
# general.move(general_y,general_x,general_y,general_x-1)
# print what_tiles_we_have(tiles, armies)
armies_we_have, tiles_we_own, borders, inlands = what_tiles_we_have()
# print(basic_turn_info)
# empties=empties_near(tiles_we_own, tiles)
# print empties
# empties_distances, destination_of_lowest_distance=get_empties_distances(empties, (general_y, general_x))
# print empties_distances
# print destination_of_lowest_distance
# corp=which_corp_near_there(tiles_we_own, destination_of_lowest_distance)
# print corp
#
#
# if armies_we_have[corp]>1:
# general.move(corp[0], corp[1], destination_of_lowest_distance[0], destination_of_lowest_distance[1])
destinations,tiles_ranked = get_tiles_with_priority()
starts = armies_we_have
we_can={}
best_priority=0
best_move=((general_position),general_position+(1,0))
for destination_position in tiles_ranked:
priority = tiles_ranked[destination_position]
for corp_position in starts:
number=starts[corp_position]
# print (destination_position,corp_position)
if tiles[destination_position[0]][destination_position[1]] !=generals.MOUNTAIN and \
get_distance(destination_position,corp_position) <=1 and number>1 and \
number>armies[destination_position[0]][destination_position[1]]:
# first and not necessary
we_can[corp_position, destination_position]=priority
if priority>best_priority:
best_priority=priority
best_move=(corp_position, destination_position)
from console_output import game_output
game_output(state, ranks=tiles_ranked)
print best_move, best_priority
print destinations
print tiles_ranked
des=best_move[1]
corp = best_move[0]
general.move(corp[0], corp[1], des[0], des[1])