-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchBot.py
More file actions
103 lines (86 loc) · 2.9 KB
/
SearchBot.py
File metadata and controls
103 lines (86 loc) · 2.9 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
from hlt import *
from networking import *
import time
myID, gameMap = getInit()
sendInit("MyBot4")
def heuristic(in_site, in_loc):
#If neutral cell
if in_site.owner == 0 and in_site.strength > 0:
return in_site.production / in_site.strength
#If enemy cell
else:
total_damage = 0
#Calculate overkill damage
for d in CARDINALS:
neighbor_site = gameMap.getSite(in_loc, d)
if neighbor_site.owner != 0 and neighbor_site.owner != myID:
total_damage += neighbor_site.strength
return total_damage
#Check surrounding squares to find highest value move
def best_direction(location):
max_value = 0
direction = STILL
for d in CARDINALS:
neighbor_site = gameMap.getSite(location, d)
neighbor_loc = gameMap.getLocation(location, d)
if neighbor_site.owner != myID:
value = heuristic(neighbor_site, neighbor_loc)
if value >= max_value:
direction = d
max_value = value
return direction
#Find the direction of closest border
def closest_border(location):
direction = NORTH
max_distance = min(gameMap.width, gameMap.height) / 2
for d in CARDINALS:
distance = 0
current = location
next_site = gameMap.getSite(current, d)
while next_site.owner == myID and distance < max_distance:
distance += 1
current = gameMap.getLocation(current, d)
next_site = gameMap.getSite(current)
if distance < max_distance:
direction = d
max_distance = distance
return direction
def move(location):
site = gameMap.getSite(location)
border = False
if site.strength < site.production * 5:
return Move(location, STILL)
#Is border cell?
for d in CARDINALS:
neighbor_site = gameMap.getSite(location, d)
if neighbor_site.owner != myID:
border = True
#If border what is best move?
if border:
pos_direction = best_direction(location)
neighbor_site = gameMap.getSite(location, pos_direction)
if neighbor_site.owner != myID:
if neighbor_site.strength < site.strength:
return Move(location, pos_direction)
#If not border radiate outwards
if not border:
return Move(location, closest_border(location))
return Move(location, STILL)
#Move for each owned cell
def determine_moves():
for y in range(gameMap.height):
for x in range(gameMap.width):
location = Location(x, y)
if gameMap.getSite(location).owner == myID:
moves.append(move(location))
#If out of time, finalize moves
if time.time() - start_time > .98:
return
return
#Main
while True:
start_time = time.time()
moves = []
gameMap = getFrame()
determine_moves()
sendFrame(moves)