-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
executable file
·187 lines (148 loc) · 5.19 KB
/
example.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
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
import generals
import heapq
# 1v1
#g = generals.Generals('ry0FVyx_g', 'frank', '1v1')
# ffa
# g = generals.Generals('your userid', 'your username', 'ffa')
# private game
roomID = raw_input("Please enter the room id: ")
#print(roomID)
g = generals.Generals('ry0FVyx_g', 'frank', 'private', roomID)
# 2v2 game
# g = generals.Generals('your userid', 'your username', 'team')
class Tile:
def __init__(self):
self.state = generals.FOG # what is on the tile
self.visited = False # while traversing, marks if visited
self.coords = [] # coordinates in grid
self.up = None # Node above it
self.down = None # Node below it
self.left = None # Node to left of it
self.right = None # Node to right of it
self.army = 0 # Number of troops on the tile (- means good)
self.searchDist = 0 #
self.searchPrev = None
class Bot:
def __init__(self):
self.mapGrid = [] # a representation of the map as a 2x2 matrix
self.tiles = [] # a list of all tiles
self.roomID = "" # the id of current game
self.nCols = 0 # number of columns in the map
self.nRows = 0 # number of rows in the map
def startGame(self,game="private"):
if game == "1v1":
g = generals.Generals('ry0FVyx_g', 'frank', '1v1')
elif game == "ffa":
g = generals.Generals('ry0FVyx_g', 'frank', 'ffa')
elif game == "private":
self.roomID = raw_input("Please enter the room id: ")
g = generals.Generals('ry0FVyx_g', 'frank', 'private', self.roomID)
elif game == "2v2":
g = generals.Generals('ry0FVyx_g', 'frank', 'team')
else:
print("Please input one of the following game types: ")
print("1v1, ffa, private, 2v2")
def initializeMap(self,update):
'''Creates a map with nodes and connects the nodes
to create a graph that the bot can traverse'''
self.createMap(update)
for i in range(self.nRows):
for j in range(self.nCols):
print("updated tile: "+str(i)+" "+str(j))
currTile = self.mapGrid[i][j]
currTile.coords = [i,j]
if i+1 < self.nRows: # check down
if update['tile_grid'][i+1][j] in [generals.FOG,1,-1]:
currTile.down = self.mapGrid[i+1][j]
if i-1 >= 0: # check up
if update['tile_grid'][i-1][j] in [generals.FOG,1,-1]:
currTile.up = self.mapGrid[i-1][j]
if j+1 < self.nCols: # check right
if update['tile_grid'][i][j+1] in [generals.FOG,1,-1]:
currTile.right = self.mapGrid[i][j+1]
if j-1 >= 0: # check left
if update['tile_grid'][i][j-1] in [generals.FOG,1,-1]:
currTile.left = self.mapGrid[i][j-1]
def updateMap(self,update):
for i in range(self.nRows):
for j in range(self.nCols):
botTile = self.mapGrid[i][j]
tile = update['tile_grid'][i][j]
troops = update['army_grid'][i][j]
if tile == generals.MOUNTAIN:
botTile.state = generals.MOUNTAIN
elif tile == generals.FOG:
botTile.state = generals.FOG
elif tile == generals.OBSTACLE:
botTile.state = generals.OBSTACLE
elif tile == generals.ENEMY:
botTile.state = generals.ENEMY
botTile.army = troops
elif tile == generals.BOT:
botTile.state = generals.BOT
botTile.army = troops * -1
elif tile == generals.EMPTY:
botTile.state = generals.EMPTY
else:
print("unknown state")
def dijkstras(self,root,target):
heap = []
for t in self.tiles:
t.searchDist = 1000000
t.searchPrev = None
root.searchDist = 0
heapp.heappush(heap,t)
while heap != []:
u = heapq.heappop(heap)
for v in [u.up,u.down,u.left,u.right]:
if v:
alt = u.searchDist + v.army
if alt < v.searchDist:
v.searchDist = alt
v.searchPrev = u
return target
def createMap(self, update):
'''Craetes the initial Map - a 2D array with a
Tile object in each index'''
self.nCols = update['cols']
self.nRows = update['rows']
self.mapGrid = [ [Tile() for i in range(self.nCols)] for j in range(self.nRows) ]
def printMap(self):
print("")
for i in range(self.nRows):
rowStr = ""
for j in range(self.nCols):
tile = self.mapGrid[i][j]
if tile.state == generals.MOUNTAIN:
rowStr += "[ M ]"
elif tile.state == generals.FOG:
rowStr += "[|||]"
elif tile.state == generals.OBSTACLE:
rowStr += "[ M ]"
elif tile.state == generals.ENEMY:
rowStr += "[ "+str(tile.army)+" ]"
elif tile.state == generals.BOT:
rowStr += "[ "+str(tile.army)+" ]"
elif tile.state == generals.EMPTY:
rowStr += "[ ]"
else:
rowStr += "[ "+str(tile)+" ]"
print(rowStr)
print("")
print("")
frank = Bot()
madeMap = False
for update in g.get_updates():
if madeMap == False:
frank.initializeMap(update)
madeMap = True
frank.updateMap(update)
frank.printMap()
# get position of your general
pi = update['player_index']
y, x = update['generals'][pi]
# move units from general to arbitrary square
for dy, dx in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
if (0 <= y+dy < update['rows'] and 0 <= x+dx < update['cols'] and update['tile_grid'][y+dy][x+dx] == 0):
g.move(y, x, y+dy, x+dx)
break