-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHunting.py
209 lines (192 loc) · 7.31 KB
/
Hunting.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
'''This code is for hunting simulation where 4 hunter bots chasing 1
prey bot. We can increase the number of hunters bot and prey bots
then its result differ.'''
from cProfile import label
from cmath import atan
from dis import dis
from lib2to3.pgen2.token import NOTEQUAL
from re import X
import matplotlib.pyplot as plt
from matplotlib import style
import math
lowerBoundX=0
upperBoundX=300
lowerBoundY=0
upperBoundY=300
plt.ion()
style.use('fivethirtyeight')
hunters=[
[10,10,150,150],
[10,150,10,150],
]
preyLineY=[]
preyLineX=[]
preys=[
[90],
[90]
]
Slope=[1,1,1,1]
distance = [100,100,100,100]
walls=[[0,0,0,0],
[0,90,180,270]]
def overlapping():
for i in (0,2):
for j in (i+1,3):
if(hunters[0][i]==hunters[0][j]):
if(abs(hunters[1][i]-hunters[1][j])<10):
hunters[1][j]+=1
def moveRobotTwo():
global hunters,preys,Slope
# sum=1
diff=5
for i in (0,3):
if(Slope[i]!=0):
diff+=abs(math.degrees(math.atan(Slope[i])))
if(preys[0][0]<8 and preys[1][0]<8):
diff=45
elif(preys[0][0]<8 and preys[1][0]>285):
diff=(-45)
elif(preys[0][0]>285 and preys[1][0]>285):
diff=135
elif(preys[0][0]>285 and preys[1][0]<8):
diff=135
elif(preys[0][0]<8):
diff=45
elif(preys[0][0]>285):
diff=(-90)
elif(preys[1][0]<8):
diff=0
elif(preys[1][0]>285):
diff=(-180)
# diff=diff-sum
diff=math.tan(diff)
# slope=int(abs(c-a)/abs(d-b))
if(preys[0][0]>=10 and preys[0][0]<=290) and (preys[1][0]>10 and preys[1][0]<=290):
if(preys[0][0]==290):
preys[1][0]+=2
preys[0][0]-=2
elif(preys[1][0]==290):
preys[0][0]+=2
preys[1][0]-=2
elif(preys[0][0]==10):
if(preys[1][0]==10):
preys[1][0]+=2
preys[0][0]+=2
else:
preys[1][0]-=2
preys[0][0]+=2
elif(preys[1][0]==10):
if(preys[0][0]==10):
preys[0][0]+=2
preys[1][0]+=2
else:
preys[0][0]-=2
preys[1][0]+=2
else:
if(preys[0][0]>hunters[0][0]):
if (preys[1][0]>hunters[1][0]):
preys[0][0]+=2
preys[1][0]+=diff*2
else:
preys[0][0]+=2
preys[1][0]-=diff*2
else:
if (preys[1][0]>hunters[1][0]):
preys[0][0]-=2
preys[1][0]+=diff*2
else:
preys[0][0]-=2
preys[1][0]-=diff*2
def moveRobotOne():
global hunters,preys,Slope
i=0
while i<4:
if(abs(preys[0][0]-hunters[0][i])!=0):
S=int(abs(preys[1][0]-hunters[1][i])/abs(preys[0][0]-hunters[0][i]))
if S > 1:
Slope[i]=1
i+=1
elif S < -1:
Slope[i]=-1
i+=1
else:
Slope[i]=S
i+=1
else:
i+=1
j=0
while j<4:
if(preys[0][0]==hunters[0][j]):
if(hunters[1][j]>preys[1][0]):
hunters[1][j]-=Slope[j]*1
j+=1
else:
hunters[1][j]+=Slope[j]*1
j+=1
elif(preys[1][0]==hunters[1][j]):
if(hunters[0][j]>preys[0][0]):
hunters[0][j]-=1
j+=1
else :
hunters[0][j]+=1
j+=1
else:
if(preys[0][0]>hunters[0][j]):
if(preys[1][0]>hunters[1][j]):
hunters[0][j]+=1
hunters[1][j]+=Slope[j]*1
j+=1
else:
hunters[0][j]+=1
hunters[1][j]-=Slope[j]*1
j+=1
else:
if(preys[1][0]>hunters[1][j]):
hunters[0][j]-=1
hunters[1][j]+=Slope[j]*1
j+=1
else:
hunters[0][j]-=1
hunters[1][j]-=Slope[j]*1
j+=1
def game():
while True:
plt.clf()
plt.box()
plt.scatter(preys[0][0],preys[1][0],color='b',label='Prey')
plt.scatter(preys[0][0],preys[1][0],s=5000,facecolors='none',edgecolors='b',linestyle='--')
preyLineX.append(preys[0][0])
preyLineY.append(preys[1][0])
plt.scatter(hunters[0][0],hunters[1][0],color='r',label='hunter1')
plt.scatter(hunters[0][1],hunters[1][1],color='r',label='hunter2')
plt.scatter(hunters[0][2],hunters[1][2],color='r',label='hunter3')
plt.scatter(hunters[0][3],hunters[1][3],color='r',label='hunter4')
plt.plot(preyLineX,preyLineY)
plt.ylim(0,300)
plt.xlim(0,300)
# some=[10,10,10,10]
# plt.boxplot(some)
# plt.axis('off')
plt.legend(bbox_to_anchor=(0.75,1.15),ncol=2)
# plt.grid(False)
plt.show()
plt.pause(0.1)
# distanceFromWalls()
for i in (0,3):
distance[i]=int(math.sqrt(pow(abs(preys[0][0]-hunters[0][i]),2)+pow(abs(preys[1][0]-hunters[1][i]),2)))
minimum=distance.index(min(distance))
if(distance[minimum]<5):
print("Captured")
break
if(distance[minimum]<50):
moveRobotTwo()
moveRobotOne()
else:
moveRobotOne()
overlapping()
print(distance[minimum])
def main():
# ax= plt.subplots()
game()
if __name__=="__main__":
main()