-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuvbbot-simp.py
182 lines (134 loc) · 3.87 KB
/
uvbbot-simp.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
#!/usr/bin/python
from client import Client
from constants import Action, Direction
import random
import math
# 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 last_loc
global snowballs
global snowball_target
global min_snowballs
global max_snowballs
global moves
global max_moves
# Print out our view of the game board
print board
if snowballs < snowball_target:
snowballs += 1
return (Action.MAKESNOWBALL, curr_dir)
else:
snowball_target = 0
if snowballs < min_snowballs:
snowball_target = max_snowballs
if not last_loc:
for loc in board.objects:
if board.get_object_at(loc) == board.ME:
print loc
last_loc = loc
distance = 0
safe_dirs = (Direction.NORTH , Direction.SOUTH , Direction.EAST , Direction.WEST)
for loc in board.objects:
if board.get_object_at(loc) == board.PLAYER:
player_loc = eval(loc)
last_dir = curr_dir
curr_dir = get_direction(player_loc)
distance = get_distance(player_loc)
print distance
max_snowballs = 5
min_snowballs = 2
#moves = 0
if distance < 3:
curr_dir = get_opposite_direction(curr_dir)
elif (distance < 4 and curr_dir in safe_dirs) or (distance < 4.5 and curr_dir not in safe_dirs) or (last_dir == curr_dir and curr_dir in safe_dirs and distance < 8):
snowballs -= 1
return (Action.THROWSNOWBALL , curr_dir)
elif board.get_object_at(loc) == board.SNOWBALL:
snowball_loc = eval(loc)
snowball_dist = get_distance(snowball_loc)
if snoball_dist < 3.5:
curr_dir = get_perp_direction(get_direction(snowball_loc))
else:
max_snowballs = 10
min_snowballs = 3
#print last_loc
print curr_dir
obj = board.get_object_at(board.next_pos_in_direction((0 , 0) , curr_dir))
while obj:
curr_dir += 1
if curr_dir > 7:
curr_dir = 0
obj = board.get_object_at(board.next_pos_in_direction((0,0) , curr_dir))
#last_loc = board.next_pos_in_direction((0, 0) , curr_dir)
moves+=1
return (Action.MOVE, curr_dir)
def get_distance(loc , curr_loc=(0,0)):
return math.sqrt(math.pow(loc[0] , 2) + math.pow(loc[1] , 2))
def get_direction(loc , curr_loc=(0,0)):
#global last_loc
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_slope(loc , curr_loc):
return (curr_loc[0] - loc[0]) / (curr_loc[1] - loc[1])
def get_opposite_direction(direction):
return (direction + 3) % 8
def get_perp_direction(direction):
return direction + 1
# Initial direction to try to move
curr_dir = Direction.SOUTH
last_loc = ()
snowballs = 0
snowball_target = 0
min_snowballs = 3
max_snowballs = 10
moves = 0
max_moves = 100
# 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'