This repository has been archived by the owner on Jul 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raidtwo.py
289 lines (258 loc) · 9.32 KB
/
raidtwo.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
import os
import sys
import subprocess
import math
from random import randint
import cv2
import numpy as np
def main():
full_img = cv2.imread('raid2_test.jpg', 1)
query_img = np.zeros((600, 800, 3), np.uint8)
sys.stdin.readline()
LANG = sys.stdin.readline().rstrip()
sys.stdin.readline()
FILE = sys.stdin.readline().rstrip()
sys.stdin.readline()
start_position = {
'R': int(sys.stdin.readline()),
'C': int(sys.stdin.readline()),
}
army = {
'R': start_position['R'],
'C': start_position['C'],
'resource': int(sys.stdin.readline()),
}
sys.stdin.readline()
enemy_count = int(sys.stdin.readline())
sys.stdin.readline()
enemy = {}
for i in range(enemy_count):
enemy[i] = {
'R': int(sys.stdin.readline()),
'C': int(sys.stdin.readline()),
'resource': int(sys.stdin.readline()),
'colour': (randint(0, 255), randint(0, 255), randint(0, 255)),
'alive': True,
}
sys.stdin.readline()
travel_cost = {
'army': float(sys.stdin.readline()),
'spy': float(sys.stdin.readline()),
}
# Initializing SPY status
sys.stdin.readline()
spy = {
'alive': False,
'R': army['R'],
'C': army['C'],
'assign_cost': int(sys.stdin.readline()),
}
# Constants
r2r_ratio = 250
discover_radius = 20
iterate_dist = 10
# Uncover area around army
cv2.circle(
img=query_img,
center=(army['C'], army['R']),
radius=int(1.5*army['resource']/r2r_ratio),
color=[255, 255, 255],
thickness=-1,
lineType=8,
shift=0
)
participant_image = np.bitwise_and(query_img, full_img)
# Draw army circle
cv2.circle(
img=participant_image,
center=(army['C'], army['R']),
radius=army['resource']/r2r_ratio,
color=(255, 255, 255),
thickness=-1,
lineType=8,
shift=0
)
cv2.imwrite('image.jpg', participant_image)
RUN_CMD = {
'c': './',
'cpp': './',
'python2': 'python2 ./',
'python3': 'python3 ./',
}
p = subprocess.Popen(
RUN_CMD[LANG]+FILE,
shell=False,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE
)
p.stdin.write("%d\n" % (army['resource']))
p.stdin.write("%d\n" % (enemy_count))
p.stdin.write("%d\n" % (spy['assign_cost']))
p.stdin.write("%d %d\n" % (travel_cost['spy'], travel_cost['army']))
p.stdin.flush()
while True:
if enemy_count == 0:
army['resource'] -= \
int(math.sqrt((army['R']-start_position['R'])**2 +
(army['C']-start_position['C'])**2)*travel_cost['army'])
print 'Yay! You emerged victorius with %d resources left' \
% (army['resource'])
print str(int(army['resource']))
sys.exit()
if p.poll() != None:
# Participant killed the program
break
input = p.stdout.readline()
if input.find('ABANDON SPY') >= 0:
spy['alive'] = False
p.stdin.write("DONE\n")
p.stdin.flush()
elif input.find('ASSIGN SPY') >= 0:
if spy['alive'] is True:
print 'You cannot assign more than one spy'
sys.exit()
spy['alive'] = True
army['resource'] -= spy['assign_cost']
spy['R'] = army['R']
spy['C'] = army['C']
p.stdin.write("DONE\n")
p.stdin.flush()
elif input.find('MOVE SPY') >= 0:
r, c = p.stdout.readline().split()
if spy['alive'] is False:
print 'You tried moving a spy without assigning him'
sys.exit()
r = float(r)
c = float(c)
distance = math.sqrt((r - spy['R'])**2 + (c - spy['C'])**2)
if(abs(r - spy['R']) < iterate_dist/2):
dr = 0
if c > spy['C']:
dc = iterate_dist
else:
dc = -iterate_dist
elif(abs(c - spy['C']) < iterate_dist/2):
dc = 0
if r > spy['R']:
dr = iterate_dist
else:
dr = -iterate_dist
else:
dr = (r - spy['R'])*iterate_dist/distance
dc = (c - spy['C'])*iterate_dist/distance
while abs(r-spy['R']) > iterate_dist \
or abs(c-spy['C']) > iterate_dist:
if spy['alive'] is False:
break
spy['R'] += dr
spy['C'] += dc
cv2.circle(
img=query_img,
center=(int(spy['C']), int(spy['R'])),
radius=discover_radius,
color=(255, 255, 255),
thickness=-1,
lineType=8,
shift=0
)
for i in enemy:
if enemy[i]['alive'] is True:
dist = ((enemy[i]['R'] - spy['R'])**2 +
(enemy[i]['C'] - spy['C'])**2)
if math.sqrt(dist) < discover_radius:
spy['alive'] = False
p.stdin.write("Spy_killed\n")
break
army['resource'] -= \
math.sqrt((dr)**2 + (dc)**2)*travel_cost['spy']
if spy['alive'] is True:
spy['R'] = int(r)
spy['C'] = int(c)
p.stdin.write("DONE\n")
p.stdin.flush()
elif input.find('MOVE ARMY') >= 0:
r, c = p.stdout.readline().split()
r = float(r)
c = float(c)
distance = math.sqrt((r - army['R'])**2 + (c - army['C'])**2)
if(abs(r - army['R']) < iterate_dist/2):
dr = 0
if c > army['C']:
dc = iterate_dist
else:
dc = -iterate_dist
elif(abs(c - army['C']) < iterate_dist/2):
dc = 0
if r > army['R']:
dr = iterate_dist
else:
dr = -iterate_dist
else:
dr = (r - army['R'])*iterate_dist/distance
dc = (c - army['C'])*iterate_dist/distance
radius_army = int(1.5*army['resource']/r2r_ratio)
while abs(r-army['R']) > iterate_dist or \
abs(c-army['C']) > iterate_dist:
army['R'] += dr
army['C'] += dc
cv2.circle(
img=query_img,
center=(int(army['C']), int(army['R'])),
radius=radius_army,
color=(255, 255, 255),
thickness=-1,
lineType=8,
shift=0
)
for i in enemy:
if enemy[i]['alive'] is True:
dist = (enemy[i]['R'] - army['R'])**2 + \
(enemy[i]['C'] - army['C'])**2
if math.sqrt() < radius_army:
if army['resource'] >= enemy[i]['resource']:
enemy[i]['alive'] = False
enemy_count -= 1
army['resource'] += enemy[i]['resource']/2
else:
print 'You failed in your conquest to win over the world. \
Your resources (%d) was no match against your opponents (%d)'\
% (army['resource'], enemy[i]['resource'])
sys.exit()
army['resource'] -= \
int(math.sqrt((dr)**2 + (dc)**2))*travel_cost['army']
if army['resource'] <= 10:
print 'You wasted all your resources \
in travelling without fighting wars'
sys.exit()
army['R'] = int(r)
army['C'] = int(c)
p.stdin.write("DONE\n")
p.stdin.flush()
elif input.find('SNAPSHOT') >= 0:
participant_image = cv2.bitwise_and(query_img, full_img)
# Draw army circle
cv2.circle(
img=participant_image,
center=(army['C'], army['R']),
radius=int(army['resource']/r2r_ratio),
color=(255, 255, 255),
thickness=-1,
lineType=8,
shift=0)
# Draw enemy circles
for i in enemy:
if enemy[i]['alive'] is True:
cv2.circle(
img=participant_image,
center=(enemy[i]['C'], enemy[i]['R']),
radius=int(enemy[i]['resource']/r2r_ratio),
color=enemy[i]['colour'],
thickness=-1,
lineType=8,
shift=0)
participant_image = cv2.bitwise_and(query_img, participant_image)
cv2.imwrite('image.jpg', participant_image)
p.stdin.write("DONE\n")
p.stdin.flush()
if __name__ == '__main__':
main()