-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleBot.py
executable file
·107 lines (84 loc) · 3.8 KB
/
simpleBot.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
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
from energium.game_constants import GAME_CONSTANTS, DIRECTIONS
ALL_DIRECTIONS = [DIRECTIONS.EAST, DIRECTIONS.NORTH, DIRECTIONS.WEST, DIRECTIONS.SOUTH]
from energium.kit import Agent
import sys
import math
import random
from energium.position import Position
# Create new agent
agent = Agent()
# initialize agent
agent.initialize()
# A simple BFS on every unit to find the max empty spot.
# Once initialized, we enter an infinite loop
while True:
# wait for update from match engine
agent.update()
commands = []
mymap = agent.map
# player is your player object, opponent is the opponent's
player = agent.players[agent.id]
opponent = agent.players[(agent.id + 1) % 2]
# use print("msg", file=sys.stderr) to print messages to the terminal or your error log.
# normal prints are reserved for the match engine. Uncomment the lines below to log something
# print('agent.turn, player.team, len(my_bases), len(my_units), player.energium)
random_base = random.randint(0,len(player.bases)-1)
totalCost = len(opponent.units)*50 + opponent.energium
if (len(opponent.units)*50 + opponent.energium) > (len(player.units)*50 + player.energium):
commands.append(player.bases[random_base].spawn_unit())
### AI Code goes here ###
free_real_estate = []
tiles = []
for y in range(agent.mapHeight):
for x in range(agent.mapWidth):
if mymap.get_tile(x,y).energium >= 0:
flag = True
for unit in player.units:
if unit.pos.equals(Position(x,y)):
flag = False
for unit in opponent.units:
if unit.pos.equals(Position(x,y)):
flag = False
if flag:
free_real_estate.append(mymap.get_tile(x,y).energium)
tiles.append((x,y))
for unit in player.units:
max_energium = max(free_real_estate)
if mymap.get_tile_by_pos(unit.pos).energium >= max_energium:
continue
current_energium = mymap.get_tile_by_pos(unit.pos).energium
q = [(unit.pos.x,unit.pos.y)]
parents = {q[0]: q[0]}
visited = []
answer = None
while current_energium < max_energium and q:
# print(parents,file=sys.stderr)
current = q[0]
q = q[1:]
answer = current
visited.append(current)
current_energium = mymap.get_tile(current[0],current[1]).energium
positionsToAdd = [(current[0]+1,current[1]),(current[0],current[1]+1),(current[0]-1,current[1]),(current[0],current[1]-1)]
for add in positionsToAdd:
if add[0] >= 0 and add[0] < agent.mapWidth and add[1] >= 0 and add[1] < agent.mapHeight:
if (add[0],add[1]) not in q and (add[0],add[1]) not in visited and (add[0],add[1]) in tiles:
parents[(add[0],add[1])] = (current[0],current[1])
q.append((add[0],add[1]))
# print(parents[answer], current_energium,file=sys.stderr)
pos_to_go = (unit.pos.x,unit.pos.y)
while parents[answer] != (unit.pos.x,unit.pos.y):
answer = parents[answer]
# print(answer, "---", end="", file=sys.stderr)
# print("\n", answer, "===", pos_to_go, file=sys.stderr)
direction = unit.pos.direction_to(Position(answer[0],answer[1]))
# print(pos_to_go,file=sys.stderr)
if direction:
free_real_estate.remove(max_energium)
tiles.remove(answer)
commands.append(unit.move(direction))
### AI Code ends here ###
#print("The current commands are ", commands, file=sys.stderr)
# submit commands to the engine
print(','.join(commands))
# now we end our turn
agent.end_turn()