-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchingBevster
384 lines (310 loc) · 11.6 KB
/
searchingBevster
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#from pandas.hashtable import na_sentinel
import hlt
from hlt import NORTH, EAST, SOUTH, WEST, STILL, Move, Square
import random
import logging
import csv
from collections import namedtuple
# prodWeight= random.randint(1,10)
# squareWeight= random.randint(1,10)
# stayStill = random.randint(3,7)
# gameStyle = random.randint(0,1)
stayStill = 5
gameStyle = 1
beingAttacked = False
myID, game_map, targetCoord,totalStrength,totalProd,areaStrength,ts,tp,gameStyle = hlt.get_init(1,2)
hlt.send_init("PrecisionBevster")
logging.basicConfig(filename=str("game_in") + '.log', level=logging.DEBUG)
def get_adjas(direction):
if direction==0:
adjas = [1,3]
elif direction==1:
adjas = [0,2]
elif direction==2:
adjas = [1,3]
elif direction==3:
adjas = [0,2]
else:
adjas = [4,4]
return adjas
def find_best_direction(square):
direction = NORTH
direction2 = NORTH
max_distance = min(game_map.width, game_map.height) / 2
max_unoccup = min(game_map.width, game_map.height) / 2
max_prod = 0
for d in (NORTH, EAST, SOUTH, WEST):
distance = 0
current = square
unoccup_count = 0
prod_sum = 0
while (current.owner==myID or current.owner==0) and distance < max_distance:
distance += 1
if current.owner==0:
unoccup_count+=1
prod_sum+=(square.production)/tp - (square.strength)/ts
current = game_map.get_target(current, d)
if prod_sum>max_prod:
max_prod = prod_sum
direction = d
if distance < max_distance:
direction2 = d
max_distance = distance
if unoccup_count < max_unoccup:
max_unoccup=unoccup_count
#if unoccup_count<1000:
return direction,direction2,max_distance
def distProdRatio():
None
def heuristic(square):
return square.production
def heuristic2(square):
if square.owner == 0 and square.strength > 0:
return (square.production)/tp - (square.strength)/ts
#return (square.production*square.production) / (square.strength)
else:
# return total potential damage caused by overkill when attacking this square
return sum(neighbor.strength for neighbor in game_map.neighbors(square) if neighbor.owner not in (0, myID))
# def find_high_prod(square):
# max_prod = square.production
# direction = 0
# if square.strength>1:
# for d in (NORTH, EAST, SOUTH, WEST):
# target = game_map.get_target(square, d)
# if target.production > max_prod:
# max_prod = target.production
# direction = d
# return direction
#
# def get_edge(square):
# target, direction = max(((neighbor, direction) for direction, neighbor in enumerate(game_map.neighbors(square))
# if neighbor.owner != myID),
# default=(None, None),
# key=lambda t: heuristic2(t[0]))
# return target,direction
# def get_move(square):
#
# target, direction = get_edge(square)
#
# if square.strength < square.production * stayStill: #and square.strength<100
# return Move(square, STILL)
#
# #border = any(neighbor.owner != myID for neighbor in game_map.neighbors(square))
#
# #if target is not None and target.strength < square.strength:
#
# # if square.strength < square.production * stayStill:
# # direction = find_high_prod(square)
# # return Move(square, direction)
# # else:
# direction, direction2, distance = find_best_direction(square)
#
# if distance < 5:
# #None
# global beingAttacked
# beingAttacked=True
#
# #logging.debug(str(beingAttacked) + " "+ str(distance))
# if beingAttacked == True:
# dir = direction2
# else:
# dir = direction
#
# target = game_map.get_target(square, dir)
# if target != myID and target.strength > square.strength:
# return Move(square, STILL)
# elif square.strength < square.production * stayStill: # and square.strength<100
# return Move(square, STILL)
#
# return Move(square, dir)
def get_move(square,mySquareCount):
target, direction = max(((neighbor, direction) for direction, neighbor in enumerate(game_map.neighbors(square))
if neighbor.owner != myID),
default=(None, None),
key=lambda t: heuristic2(t[0]))
adjas = get_adjas(direction)
target1 = game_map.get_target(square, adjas[0])
target2 = game_map.get_target(square, adjas[1])
if mySquareCount>10:#beingAttacked==False:
minTP = 0
else:
minTP = tp-ts
if target is not None and target.strength < square.strength and target.production/tp-target.strength/ts >= minTP:
return Move(square, direction)
elif square.strength < square.production * stayStill: # and square.strength<100
return Move(square, STILL)
elif target is not None:
adjas = get_adjas(direction)
target1 = game_map.get_target(square, adjas[0])
target2 = game_map.get_target(square, adjas[1])
prefDirection, prefDirection2, distance = find_best_direction(square)
#logging.debug(str(beingAttacked) + " "+ str(distance))
if beingAttacked == True:
dir = prefDirection2
else:
dir = prefDirection
#logging.debug(str(dir) + " " + str(adjas[0])+ " " + str(adjas[1]))
target3 = game_map.get_target(square, dir)
if target1.owner==myID :
return Move(square, adjas[0])
# elif target2.owner==myID:
# return Move(square, adjas[1])
else:
# if target is not None and target.strength < square.strength and target.production > minTP:
# return Move(square, direction)
if target1.strength < square.strength:
return Move(square, adjas[0])
# elif target.strength < square.strength and square.strength>100:
# return Move(square, direction)
else:
return Move(square, STILL)
#return Move(square, direction)
border = any(neighbor.owner != myID for neighbor in game_map.neighbors(square))
if not border:
# if square.strength < square.production * stayStill:
# direction = find_high_prod(square)
# return Move(square, direction)
# else:
direction, direction2, distance = find_best_direction(square)
if distance < 5:
#None
global beingAttacked
beingAttacked=True
#logging.debug(str(beingAttacked) + " "+ str(distance))
if beingAttacked == True:
dir = direction2
else:
dir = direction
return Move(square,dir)
else:
# wait until we are strong enough to attack
return Move(square, STILL)
def get_xydistance(self, sq1, sq2):
"Returns Manhattan distance between two squares."
dx = min(abs(sq1.x - sq2.x), sq1.x + self.width - sq2.x, sq2.x + self.width - sq1.x)
dy = min(abs(sq1.y - sq2.y), sq1.y + self.height - sq2.y, sq2.y + self.height - sq1.y)
return dx,dy
def find_nearest_dense(square,targetCoord):
direc = NORTH
lrDist = targetCoord.x- square.x
lrFlip = False
if abs(lrDist)>game_map.width/2:
lrFlip = True
if lrDist !=0:
if lrFlip:
if (lrDist) > 0:
neighbour = game_map.get_target(square, WEST)
direc = WEST
else:
neighbour = game_map.get_target(square, EAST)
direc = EAST
else:
if (lrDist)>0:
neighbour = game_map.get_target(square, EAST)
direc = EAST
else:
neighbour = game_map.get_target(square, WEST)
direc = WEST
maxStrength = neighbour.strength
else:
maxStrength = 10000
udDist = targetCoord.y - square.y
udFlip = False
if abs(udDist)>game_map.height/2:
udFlip = True
if udDist != 0:
if udFlip:
if (udDist) < 0:
neighbour = game_map.get_target(square, SOUTH)
if neighbour.strength<maxStrength:
direc = SOUTH
else:
neighbour = game_map.get_target(square, NORTH)
if neighbour.strength<maxStrength:
direc = NORTH
else:
if (udDist) < 0:
neighbour = game_map.get_target(square, NORTH)
if neighbour.strength<maxStrength:
direc = NORTH
else:
neighbour = game_map.get_target(square, SOUTH)
if neighbour.strength<maxStrength:
direc = SOUTH
#logging.debug(str(direc) + " " + str(square) + " " + str(targetCoord))
neighbour = game_map.get_target(square, direc)
if neighbour.strength < square.strength and neighbour.owner==0:
return Move(square, direc)
elif neighbour.owner == myID and square.strength > square.production * 5:
return Move(square, direc)
else:# square.strength < square.production * 5: #and square.strength<100
return Move(square, STILL)
# logging.debug(str(prodWeight) + " " + str(squareWeight) + " " + str(stayStill) + " " +str(game_map.height) + " " + str(gameStyle)
# + " " + str(totalStrength) + " " + str(totalProd) + " " + str(areaStrength) + " " + str(game_map.starting_player_count)
# )
# csvFile = csv.writer(open('\\\\ldnvnascti0037\\FXT_Grail$\\bevan\\halite.csv', "w", newline=''))
# csvFile2 = csv.writer(open('\\\\ldnvnascti0037\\FXT_Grail$\\bevan\\halite2.csv', "w", newline=''))
# count = 0
# row = []
# row2 = []
# for square in game_map:
#
# row.append(square.production)
# row2.append(square.strength)
#
# count += 1
# if count==29:
# csvFile.writerow(row)
# csvFile2.writerow(row2)
# row = []
# row2 = []
# count = 0
#logging.debug(str(square.x) + "," + str(square.y) + ","+ str(square.production) + "," +str(square.strength))
# num_players = game_map.starting_player_count
# area =game_map.width*game_map.height
# area_count = []
# turn_count = 0
#
# if square.owner != 0:
# area_count[square.owner] += 1
#
# for i in num_players:
# area_count.append(0)
gameStyle = 1
if gameStyle == 1:
target = False
else:
target = True
# direction = NORTH
# logging.debug(direction)
# direction = SOUTH
# logging.debug(direction)
# direction = EAST
# logging.debug(direction)
mySquareCount = 0
while True:
game_map.get_frame()
if target==False:
mySquareCount = 0
#moves = [find_nearest_dense(square,targetCoord) for square in game_map if square.owner == myID]
for square in game_map:
if square.owner==myID:
mySquareCount+=1
#target = True
#logging.debug(target)
#hlt.send_frame(moves)
if mySquareCount > 10:
target=True
moves = [get_move(square,mySquareCount) for square in game_map if square.owner == myID]
hlt.send_frame(moves)
else:
moves = [get_move(square,mySquareCount) for square in game_map if square.owner == myID]
hlt.send_frame(moves)
# if target.production*4 >= square.production*5:
# direction = direction
# elif square.strength>square.production*5:
# direction = direction
# else:
# direction = 0
# if square.strength + target.strength > 255:
# direction = 0