-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuvbbot-adv2.py
352 lines (272 loc) · 10.7 KB
/
uvbbot-adv2.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
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
#!/usr/bin/python
from client import Client
from constants import Action, Direction
import random
import math
import sys
# This function is called everytime the client gets a new map.
# It decides what move to make and returns the decision.
# The client module takes care of the network communication magic.
def dummy(board):
# We need this variable to keep state between moves
global curr_dir
global curr_loc
global snowball_locs
global snowballs
global min_snowballs
global max_snowballs
global min_player_distance
global max_throw_snowball_distance
# Print out our view of the game board
print board
possible_moves = list()
if snowballs < min_snowballs:
max_snowballs = 7
possible_moves.append((Action.MAKESNOWBALL , curr_dir))
#replace with the curr_loc + direction at some point
for loc in board.objects:
if board.get_object_at(loc) == board.ME:
curr_loc = eval(loc)
player_locs = list()
closest_player_loc = ()
closest_player_slope = 0
closest_player_dir = 0
closest_player_dist = sys.maxint
for loc in board.objects:
if board.get_object_at(loc) == board.PLAYER:
player_loc = eval(loc)
player_locs.append(player_loc)
player_dir = get_direction(player_loc)
player_dist = get_distance(player_loc)
player_slope = get_slope(player_loc)
print "Player Dist:" , player_dist," Player Slope:" , player_slope
if player_dist < closest_player_dist:
closest_player_dist = player_dist
<<<<<<< HEAD
closest_player_dir = get_navig_direction(player_loc , board)
closest_player_slope = get_slope(player_loc)
=======
closest_player_dir = player_dir
closest_player_slope = player_slope
>>>>>>> parent of c04a546... Cleaned up some unnecessary code
closest_player_loc = player_loc
print "Closest Player Location " , closest_player_loc , " Closest Player Distance " , closest_player_dist , " Closest Player Dir " , closest_player_dir , " Closest Player Slope " , closest_player_slope
print "Curr Dir:" , curr_dir
if closest_player_loc:
<<<<<<< HEAD
if closest_player_dist < min_player_distance:
possible_moves.extend(get_possible_directions([get_opposite_direction(closest_player_dir)] , Action.MOVE , order = 1))
elif (closest_player_dist <= max_throw_snowball_distance or curr_dir == closest_player_dir) and is_cardinal(closest_player_slope):
possible_moves.extend(get_possible_directions([closest_player_dir] , Action.THROWSNOWBALL , order = 1))
=======
if closest_player_dist <= min_player_distance:
possible_directions = get_possible_directions(get_opposite_diagonal_directions(closest_player_dir) , Action.MOVE , order = 1)
possible_moves.extend(possible_directions)
elif (closest_player_dist <= max_throw_snowball_distance or curr_dir == closest_player_dir) and (abs(closest_player_slope) == 1 or abs(closest_player_slope) == 0):
possible_directions = get_possible_directions([closest_player_dir] , Action.THROWSNOWBALL , order = 1)
possible_moves.extend(possible_directions)
>>>>>>> parent of c04a546... Cleaned up some unnecessary code
else:
print " Closest Player Dir:" , closest_player_dir
possible_moves.extend(get_possible_directions([closest_player_dir] , Action.MOVE , order = 2))
#TODO: implement snowball tracking
tmp_snowball_locs = dict()
for (snowball_loc) , snowball_dir in snowball_locs.iteritems():
print "Snowball Loc: " , snowball_loc , " Snowball Dir: ", snowball_dir
snowball_loc = board.next_pos_in_direction(snowball_loc , snowball_dir, amount = 2)
if board.get_object_at(snowball_loc) == board.SNOWBALL:
tmp_snowball_locs[snowball_loc] = snowball_dir
snowball_locs = tmp_snowball_locs
#Fix this to deal with Rougue snowballs
for loc in board.objects:
if board.get_object_at(loc) == board.SNOWBALL:
snowball_loc = eval(loc)
if snowball_loc not in snowball_locs:
min_player_dist = sys.maxint
closest_player_loc = ()
for player_loc in player_locs:
player_dist = get_distance(snowball_loc , player_loc)
player_slope = get_slope(snowball_loc , player_loc)
player_slope = abs(player_slope)
if player_dist < min_player_dist and player_loc != curr_loc and (player_slope == 1 or player_slope == 0):
print "Player Dist" , player_dist , " Player Loc " , player_loc , " Player Slope " , player_slope
min_player_dist = player_dist
closest_player_loc = player_loc
if closest_player_loc:
snowball_dir = get_direction(snowball_loc , closest_player_loc)
print "Snowball Loc " , snowball_loc , " Snowball Dir " , snowball_dir
snowball_locs[snowball_loc] = snowball_dir
avoid_locs = []
#Fix this to something better once collision is understood
for (snowball_loc), snowball_dir in snowball_locs.iteritems():
print snowball_loc , snowball_dir
avoid_locs.append(snowball_loc)
avoid_locs.append(board.next_pos_in_direction(snowball_loc , snowball_dir))
avoid_locs.append(board.next_pos_in_direction(snowball_loc , snowball_dir, amount = 2))
if snowballs < max_snowballs:
possible_moves.append((Action.MAKESNOWBALL , curr_dir))
else:
max_snowballs = 0
possible_moves.extend(get_possible_directions([curr_dir] , Action.MOVE , order = 4))
print possible_moves
for possible_move in possible_moves:
possible_action = possible_move[0]
possible_direction = possible_move[1]
if possible_action == Action.MOVE:
<<<<<<< HEAD
possible_next_loc = board.next_pos_in_direction(curr_loc , possible_direction)
object = board.get_object_at(possible_next_loc)
is_close_to_player = True
if closest_player_loc:
possible_distance = get_distance(possible_next_loc , closest_player_loc)
if possible_distance < min_player_distance:
is_close_to_player = False
if possible_distance > closest_player_distance:
is_close_to_player = True
if not object and possible_next_loc not in avoid_locs and is_close_to_player:
=======
#Moving Action
#TODO Add all non-player pieces to avoid_locs
possible_next_loc = board.next_pos_in_direction((0, 0) , possible_direction)
object = board.get_object_at(possible_next_loc)
if object or possible_next_loc in avoid_locs:
continue
else:
>>>>>>> parent of c04a546... Cleaned up some unnecessary code
curr_dir = possible_direction
for snowball_loc, snowball_dir in snowball_locs.iteritems():
snowball_loc = board.next_pos_in_direction(snowball_loc , get_opposite_direction(curr_dir))
return possible_move
elif possible_action == Action.THROWSNOWBALL:
if snowballs > 0 and check_snowball_path(board , possible_direction , curr_loc) and curr_loc not in avoid_locs:
print "Throwing Snowball", possible_direction
curr_dir = possible_direction
print curr_loc , curr_dir
snowball_locs[curr_loc] = curr_dir
snowballs -= 1
return possible_move
elif possible_action == Action.MAKESNOWBALL:
if curr_loc not in avoid_locs:
curr_dir = possible_direction
snowballs += 1
return possible_move
def get_distance(loc , curr_loc=(0,0)):
#return math.sqrt(math.pow(loc[0] - curr_loc[0] , 2) + math.pow(loc[1] - curr_loc[1], 2))
#return abs(loc[0] - curr_loc[0]) + abs(loc[1] - curr_loc[1])
x_diff = abs(loc[0] - curr_loc[0])
y_diff = abs(loc[1] - curr_loc[1])
return max(x_diff , y_diff)
<<<<<<< HEAD
def get_abs_distance(loc , curr_loc=(0,0)):
return math.sqrt(math.pow(loc[0] - curr_loc[0] , 2) + math.pow(loc[1] - curr_loc[1] , 2))
def get_direction(loc, curr_loc=(0,0)):
=======
def get_direction(loc , curr_loc=(0,0)):
#global last_loc
>>>>>>> parent of c04a546... Cleaned up some unnecessary code
x_diff = curr_loc[0] - loc[0]
y_diff = curr_loc[1] - loc[1]
if x_diff > 0:
#Some kind of west
if y_diff < 0:
return Direction.SOUTHWEST
elif y_diff > 0:
return Direction.NORTHWEST
else:
return Direction.WEST
elif x_diff < 0:
#Some kind of east
if y_diff < 0:
return Direction.SOUTHEAST
elif y_diff > 0:
return Direction.NORTHEAST
else:
return Direction.EAST
else:
if y_diff > 0:
return Direction.NORTH
else:
return Direction.SOUTH
def get_navig_direction(loc , board, curr_loc=(0,0)):
min_direction = 0
min_direction_dist = sys.maxint
for i in range(8):
distance = get_abs_distance(board.next_pos_in_direction(curr_loc , i) , loc)
print "Distance: " , distance , "Direction: ", i
if distance < min_direction_dist:
min_direction_dist = distance
min_direction = i
print "Min Direction: " , min_direction
return min_direction
def get_slope(loc , curr_loc = (0,0)):
if curr_loc[1] - loc[1] == 0:
return 0
else:
return float((curr_loc[0] - loc[0])) / float((curr_loc[1] - loc[1]))
def get_opposite_direction(direction):
return (direction + 4) % 8
def get_opposite_diagonal_directions(direction):
return [(direction + 3 ) % 8 , (direction + 5) % 8]
def get_perp_directions(direction):
return (direction + 1) % 8
def get_possible_directions(directions , action, order = 2):
possible_directions = []
for i in range(order):
for direction in directions:
#print i , direction
possible_direction = (direction + i) % 8
possible_direction_opp = (direction - i + 8) % 8
possible_direction_tuple = (action , possible_direction)
possible_direction_opp_tuple = (action, possible_direction_opp)
if possible_direction_tuple not in possible_directions:
possible_directions.append(possible_direction_tuple)
if possible_direction_opp_tuple not in possible_directions:
possible_directions.append(possible_direction_opp_tuple)
return possible_directions
def check_snowball_path(board , direction, curr_loc = (0,0)):
for i in range(8):
object = board.get_object_at(board.next_pos_in_direction(curr_loc , direction, i))
if object == board.TREE or object == board.SNOWMAN:
return False
elif object == board.PLAYER:
return True
def adjust_distance(distance , slope):
slope = abs(slope)
if slope != 0:
return distance / math.sqrt(2)
else:
return distance
# Initial direction to try to move
curr_dir = Direction.NORTH
curr_loc = (0 , 0)
snowball_locs = dict()
snowballs = 0
snowball_target = 0
min_snowballs = 3
max_snowballs = 10
min_player_distance = 3# * math.sqrt(2)
max_throw_snowball_distance = 5# * math.sqrt(2)
# Create our client
c = Client()
# Insert your key here
c.KEY = "g2lVRjJN96svcwTYPG8K0MmUfdQq6Xg1"
# Tell the client to use this function to decide the next move.
# This function is defined above.
c.decide_move = dummy
# Connect to the game server
if c.connect():
# If we successfully connected, start playing
print 'Connected'
# Tell the client to start listening for maps
c.start()
# Wait until the user presses a key
raw_input('Press any key to quit...')
# Tell the client to stop listening for maps
c.stop()
# Wait for the client to finish its last request
c.join()
# Disconnect from the server
c.disconnect()
else:
# If we didn't successfully connected, exit
print 'Could not connect'