Skip to content

Commit b4694b5

Browse files
authored
Update djikstra.py
1 parent 76c4d36 commit b4694b5

File tree

1 file changed

+43
-61
lines changed

1 file changed

+43
-61
lines changed

djikstra.py

Lines changed: 43 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import hexgame
1313

1414
import random
15-
15+
import math
1616
from collections import defaultdict
1717

1818
INIT_STATE, START, PLAYING, WAITING_FOR_ACK,\
@@ -44,12 +44,15 @@ def send_message_callback(writer, row, col, state):
4444

4545
@asyncio.coroutine
4646
def game_client(loop, state):
47-
"""The main client logic, based on a state machine."""
47+
"""The main client
48+
hexboard.current=2 logic, based on a state machine."""
4849
reader, writer = yield from asyncio.open_connection(HOST, PORT,
4950
loop=loop)
5051
print("Connected to the game server")
5152
sys.stdout.flush()
5253
state[0] = INIT_STATE
54+
player=2
55+
init=0
5356
while state[0] not in (END_STATE, CONNECTION_REFUSED):
5457
if state[0] == INIT_STATE:
5558
data = yield from reader.readline()
@@ -71,6 +74,9 @@ def game_client(loop, state):
7174
hexboard = hexgame.Hex.create_from_str(message[5:])
7275
hexgui.redraw(hexboard)
7376
hexgui.set_title("Hex game - your turn")
77+
if init==0 and "1" not in message:
78+
player=1
79+
init=1
7480
print(message)
7581
if message.startswith("End"):
7682
state[0] = END_STATE
@@ -80,8 +86,8 @@ def game_client(loop, state):
8086
print(message)
8187
if state[0] == PLAYING:
8288
row, col = None, None
83-
graph = make_graph(hexboard.size, hexboard.grid, hexboard.current)
84-
tab = find_best(hexboard.size, graph, hexboard.current)
89+
graph = make_graph(hexboard.size, hexboard.grid, player)
90+
tab = find_best(hexboard.size, hexboard.grid, graph, player)
8591
row=tab[0]
8692
col=tab[1]
8793
yield from send_message_callback(writer,row, col,state)
@@ -100,6 +106,7 @@ def game_client(loop, state):
100106
sys.stdout.flush()
101107

102108
if state[0] == END_STATE:
109+
print("Joueur :"+str(player))
103110
print("{} wins the game".format(hexgui.player_names[hexboard.winner]))
104111
writer.close()
105112

@@ -117,79 +124,63 @@ def make_graph(size, grid, current):
117124
graph.add_edge(f,t,0)
118125
else:
119126
if grid[i-1][j-1]==EMPTY:
120-
graph.add_edge(f,t,1)
121-
else:
122-
graph.add_edge(f,t,2)
127+
graph.add_edge(f,t,50)
123128

124129
t=""+str(i)+"#"+str(j-1)
125130
if j>0:
126131
if grid[i][j-1]==current:
127132
graph.add_edge(f,t,0)
128133
else:
129134
if grid[i][j-1]==EMPTY:
130-
graph.add_edge(f,t,1)
131-
else:
132-
graph.add_edge(f,t,2)
135+
graph.add_edge(f,t,50)
133136

134137
t=""+str(i+1)+"#"+str(j-1)
135138
if j>0 and i<size-1:
136139
if grid[i+1][j-1]==current:
137140
graph.add_edge(f,t,0)
138141
else:
139142
if grid[i+1][j-1]==EMPTY:
140-
graph.add_edge(f,t,1)
141-
else:
142-
graph.add_edge(f,t,2)
143+
graph.add_edge(f,t,50)
143144

144145
t=""+str(i+1)+"#"+str(j)
145146
if i<size-1:
146147
if grid[i+1][j]==current:
147148
graph.add_edge(f,t,0)
148149
else:
149150
if grid[i+1][j]==EMPTY:
150-
graph.add_edge(f,t,1)
151-
else:
152-
graph.add_edge(f,t,2)
151+
graph.add_edge(f,t,50)
153152

154153
t=""+str(i-1)+"#"+str(j)
155154
if i>0:
156155
if grid[i-1][j]==current:
157156
graph.add_edge(f,t,0)
158157
else:
159158
if grid[i-1][j]==EMPTY:
160-
graph.add_edge(f,t,1)
161-
else:
162-
graph.add_edge(f,t,2)
159+
graph.add_edge(f,t,50)
163160

164161
t=""+str(i-1)+"#"+str(j+1)
165162
if i>0 and j<size-1:
166163
if grid[i-1][j+1]==current:
167164
graph.add_edge(f,t,0)
168165
else:
169166
if grid[i-1][j+1]==EMPTY:
170-
graph.add_edge(f,t,1)
171-
else:
172-
graph.add_edge(f,t,2)
167+
graph.add_edge(f,t,50)
173168

174169
t=""+str(i)+"#"+str(j+1)
175170
if j<size-1:
176171
if grid[i][j+1]==current:
177172
graph.add_edge(f,t,0)
178173
else:
179174
if grid[i][j+1]==EMPTY:
180-
graph.add_edge(f,t,1)
181-
else:
182-
graph.add_edge(f,t,2)
175+
graph.add_edge(f,t,50)
183176

184177
t=""+str(i+1)+"#"+str(j+1)
185178
if i<size-1 and j<size-1:
186179
if grid[i+1][j+1]==current:
187180
graph.add_edge(f,t,0)
188181
else:
189182
if grid[i+1][j+1]==EMPTY:
190-
graph.add_edge(f,t,1)
191-
else:
192-
graph.add_edge(f,t,2)
183+
graph.add_edge(f,t,50)
193184
else:
194185
if grid[i][j]==EMPTY:
195186
f=""+str(i)+"#"+str(j)
@@ -199,79 +190,63 @@ def make_graph(size, grid, current):
199190
graph.add_edge(f,t,1)
200191
else:
201192
if grid[i-1][j-1]==EMPTY:
202-
graph.add_edge(f,t,2)
203-
else:
204-
graph.add_edge(f,t,3)
193+
graph.add_edge(f,t,100)
205194

206195
t=""+str(i)+"#"+str(j-1)
207196
if j>0:
208197
if grid[i][j-1]==current:
209198
graph.add_edge(f,t,1)
210199
else:
211200
if grid[i][j-1]==EMPTY:
212-
graph.add_edge(f,t,2)
213-
else:
214-
graph.add_edge(f,t,3)
201+
graph.add_edge(f,t,100)
215202

216203
t=""+str(i+1)+"#"+str(j-1)
217204
if j>0 and i<size-1:
218205
if grid[i+1][j-1]==current:
219206
graph.add_edge(f,t,1)
220207
else:
221208
if grid[i+1][j-1]==EMPTY:
222-
graph.add_edge(f,t,2)
223-
else:
224-
graph.add_edge(f,t,3)
209+
graph.add_edge(f,t,100)
225210

226211
t=""+str(i+1)+"#"+str(j)
227212
if i<size-1:
228213
if grid[i+1][j]==current:
229214
graph.add_edge(f,t,1)
230215
else:
231216
if grid[i+1][j]==EMPTY:
232-
graph.add_edge(f,t,2)
233-
else:
234-
graph.add_edge(f,t,3)
217+
graph.add_edge(f,t,100)
235218

236219
t=""+str(i-1)+"#"+str(j)
237220
if i>0:
238221
if grid[i-1][j]==current:
239222
graph.add_edge(f,t,1)
240223
else:
241224
if grid[i-1][j]==EMPTY:
242-
graph.add_edge(f,t,2)
243-
else:
244-
graph.add_edge(f,t,3)
225+
graph.add_edge(f,t,100)
245226

246227
t=""+str(i-1)+"#"+str(j+1)
247228
if i>0 and j<size-1:
248229
if grid[i-1][j+1]==current:
249230
graph.add_edge(f,t,1)
250231
else:
251232
if grid[i-1][j+1]==EMPTY:
252-
graph.add_edge(f,t,2)
253-
else:
254-
graph.add_edge(f,t,3)
233+
graph.add_edge(f,t,100)
255234

256235
t=""+str(i)+"#"+str(j+1)
257236
if j<size-1:
258237
if grid[i][j+1]==current:
259238
graph.add_edge(f,t,1)
260239
else:
261240
if grid[i][j+1]==EMPTY:
262-
graph.add_edge(f,t,2)
263-
else:
264-
graph.add_edge(f,t,3)
241+
graph.add_edge(f,t,100)
265242

266243
t=""+str(i+1)+"#"+str(j+1)
267244
if i<size-1 and j<size-1:
268245
if grid[i+1][j+1]==current:
269246
graph.add_edge(f,t,1)
270247
else:
271248
if grid[i+1][j+1]==EMPTY:
272-
graph.add_edge(f,t,2)
273-
else:
274-
graph.add_edge(f,t,3)
249+
graph.add_edge(f,t,100)
275250
return graph
276251

277252
def djikstra(graph, initial, end):
@@ -295,18 +270,18 @@ def djikstra(graph, initial, end):
295270

296271
next_destinations = {node: shortest_paths[node] for node in shortest_paths if node not in visited}
297272
if not next_destinations:
298-
return "Route Not Possible"
273+
return "Route Non Possible"
299274
current_node = min(next_destinations, key=lambda k: next_destinations[k][1])
300275

301-
path = []
276+
path = list()
302277
while current_node is not None:
303278
path.append(current_node)
304279
next_node = shortest_paths[current_node][0]
305280
current_node = next_node
306-
path = path[::-1]
281+
#print(path)
307282
return path
308283

309-
def find_best(size, graph, current):
284+
def find_best(size, grid, graph, current):
310285
lst=list()
311286
if current==1:
312287
for i in range(size):
@@ -318,23 +293,30 @@ def find_best(size, graph, current):
318293
for i in range(size):
319294
lst.append(djikstra(graph, "0#"+str(j), ""+str(size-1)+"#"+str(i)))
320295

321-
sum=weigh(graph,lst[0])
322-
path=lst[0]
296+
sum=math.inf
297+
path=random.choice(lst)
323298
for elt in lst:
324299
s=weigh(graph,elt)
325-
if s<sum:
300+
if s!=0 and s<sum:
326301
path=elt
327302
sum=s
328303

329304
tab=random.choice(path).split("#")
330305
tab[0]=int(tab[0])
331306
tab[1]=int(tab[1])
307+
while grid[tab[0]][tab[1]]:
308+
tab=random.choice(path).split("#")
309+
tab[0]=int(tab[0])
310+
tab[1]=int(tab[1])
332311
return tab
333312

334313
def weigh(graph, elt):
335314
sum=0
336-
for i in range(len(elt)-2):
337-
sum+=graph.weights[(elt[i],elt[i+1])]
315+
longeur=len(elt)-2
316+
for i in range(longeur):
317+
if isinstance(graph.weights.get((elt[i],elt[i+1])), int):
318+
sum+=graph.weights.get((elt[i],elt[i+1]))
319+
#print(sum)
338320
return sum
339321

340322
def main():

0 commit comments

Comments
 (0)