12
12
import hexgame
13
13
14
14
import random
15
-
15
+ import math
16
16
from collections import defaultdict
17
17
18
18
INIT_STATE , START , PLAYING , WAITING_FOR_ACK ,\
@@ -44,12 +44,15 @@ def send_message_callback(writer, row, col, state):
44
44
45
45
@asyncio .coroutine
46
46
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."""
48
49
reader , writer = yield from asyncio .open_connection (HOST , PORT ,
49
50
loop = loop )
50
51
print ("Connected to the game server" )
51
52
sys .stdout .flush ()
52
53
state [0 ] = INIT_STATE
54
+ player = 2
55
+ init = 0
53
56
while state [0 ] not in (END_STATE , CONNECTION_REFUSED ):
54
57
if state [0 ] == INIT_STATE :
55
58
data = yield from reader .readline ()
@@ -71,6 +74,9 @@ def game_client(loop, state):
71
74
hexboard = hexgame .Hex .create_from_str (message [5 :])
72
75
hexgui .redraw (hexboard )
73
76
hexgui .set_title ("Hex game - your turn" )
77
+ if init == 0 and "1" not in message :
78
+ player = 1
79
+ init = 1
74
80
print (message )
75
81
if message .startswith ("End" ):
76
82
state [0 ] = END_STATE
@@ -80,8 +86,8 @@ def game_client(loop, state):
80
86
print (message )
81
87
if state [0 ] == PLAYING :
82
88
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 )
85
91
row = tab [0 ]
86
92
col = tab [1 ]
87
93
yield from send_message_callback (writer ,row , col ,state )
@@ -100,6 +106,7 @@ def game_client(loop, state):
100
106
sys .stdout .flush ()
101
107
102
108
if state [0 ] == END_STATE :
109
+ print ("Joueur :" + str (player ))
103
110
print ("{} wins the game" .format (hexgui .player_names [hexboard .winner ]))
104
111
writer .close ()
105
112
@@ -117,79 +124,63 @@ def make_graph(size, grid, current):
117
124
graph .add_edge (f ,t ,0 )
118
125
else :
119
126
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 )
123
128
124
129
t = "" + str (i )+ "#" + str (j - 1 )
125
130
if j > 0 :
126
131
if grid [i ][j - 1 ]== current :
127
132
graph .add_edge (f ,t ,0 )
128
133
else :
129
134
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 )
133
136
134
137
t = "" + str (i + 1 )+ "#" + str (j - 1 )
135
138
if j > 0 and i < size - 1 :
136
139
if grid [i + 1 ][j - 1 ]== current :
137
140
graph .add_edge (f ,t ,0 )
138
141
else :
139
142
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 )
143
144
144
145
t = "" + str (i + 1 )+ "#" + str (j )
145
146
if i < size - 1 :
146
147
if grid [i + 1 ][j ]== current :
147
148
graph .add_edge (f ,t ,0 )
148
149
else :
149
150
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 )
153
152
154
153
t = "" + str (i - 1 )+ "#" + str (j )
155
154
if i > 0 :
156
155
if grid [i - 1 ][j ]== current :
157
156
graph .add_edge (f ,t ,0 )
158
157
else :
159
158
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 )
163
160
164
161
t = "" + str (i - 1 )+ "#" + str (j + 1 )
165
162
if i > 0 and j < size - 1 :
166
163
if grid [i - 1 ][j + 1 ]== current :
167
164
graph .add_edge (f ,t ,0 )
168
165
else :
169
166
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 )
173
168
174
169
t = "" + str (i )+ "#" + str (j + 1 )
175
170
if j < size - 1 :
176
171
if grid [i ][j + 1 ]== current :
177
172
graph .add_edge (f ,t ,0 )
178
173
else :
179
174
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 )
183
176
184
177
t = "" + str (i + 1 )+ "#" + str (j + 1 )
185
178
if i < size - 1 and j < size - 1 :
186
179
if grid [i + 1 ][j + 1 ]== current :
187
180
graph .add_edge (f ,t ,0 )
188
181
else :
189
182
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 )
193
184
else :
194
185
if grid [i ][j ]== EMPTY :
195
186
f = "" + str (i )+ "#" + str (j )
@@ -199,79 +190,63 @@ def make_graph(size, grid, current):
199
190
graph .add_edge (f ,t ,1 )
200
191
else :
201
192
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 )
205
194
206
195
t = "" + str (i )+ "#" + str (j - 1 )
207
196
if j > 0 :
208
197
if grid [i ][j - 1 ]== current :
209
198
graph .add_edge (f ,t ,1 )
210
199
else :
211
200
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 )
215
202
216
203
t = "" + str (i + 1 )+ "#" + str (j - 1 )
217
204
if j > 0 and i < size - 1 :
218
205
if grid [i + 1 ][j - 1 ]== current :
219
206
graph .add_edge (f ,t ,1 )
220
207
else :
221
208
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 )
225
210
226
211
t = "" + str (i + 1 )+ "#" + str (j )
227
212
if i < size - 1 :
228
213
if grid [i + 1 ][j ]== current :
229
214
graph .add_edge (f ,t ,1 )
230
215
else :
231
216
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 )
235
218
236
219
t = "" + str (i - 1 )+ "#" + str (j )
237
220
if i > 0 :
238
221
if grid [i - 1 ][j ]== current :
239
222
graph .add_edge (f ,t ,1 )
240
223
else :
241
224
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 )
245
226
246
227
t = "" + str (i - 1 )+ "#" + str (j + 1 )
247
228
if i > 0 and j < size - 1 :
248
229
if grid [i - 1 ][j + 1 ]== current :
249
230
graph .add_edge (f ,t ,1 )
250
231
else :
251
232
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 )
255
234
256
235
t = "" + str (i )+ "#" + str (j + 1 )
257
236
if j < size - 1 :
258
237
if grid [i ][j + 1 ]== current :
259
238
graph .add_edge (f ,t ,1 )
260
239
else :
261
240
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 )
265
242
266
243
t = "" + str (i + 1 )+ "#" + str (j + 1 )
267
244
if i < size - 1 and j < size - 1 :
268
245
if grid [i + 1 ][j + 1 ]== current :
269
246
graph .add_edge (f ,t ,1 )
270
247
else :
271
248
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 )
275
250
return graph
276
251
277
252
def djikstra (graph , initial , end ):
@@ -295,18 +270,18 @@ def djikstra(graph, initial, end):
295
270
296
271
next_destinations = {node : shortest_paths [node ] for node in shortest_paths if node not in visited }
297
272
if not next_destinations :
298
- return "Route Not Possible"
273
+ return "Route Non Possible"
299
274
current_node = min (next_destinations , key = lambda k : next_destinations [k ][1 ])
300
275
301
- path = []
276
+ path = list ()
302
277
while current_node is not None :
303
278
path .append (current_node )
304
279
next_node = shortest_paths [current_node ][0 ]
305
280
current_node = next_node
306
- path = path [:: - 1 ]
281
+ #print( path)
307
282
return path
308
283
309
- def find_best (size , graph , current ):
284
+ def find_best (size , grid , graph , current ):
310
285
lst = list ()
311
286
if current == 1 :
312
287
for i in range (size ):
@@ -318,23 +293,30 @@ def find_best(size, graph, current):
318
293
for i in range (size ):
319
294
lst .append (djikstra (graph , "0#" + str (j ), "" + str (size - 1 )+ "#" + str (i )))
320
295
321
- sum = weigh ( graph , lst [ 0 ])
322
- path = lst [ 0 ]
296
+ sum = math . inf
297
+ path = random . choice ( lst )
323
298
for elt in lst :
324
299
s = weigh (graph ,elt )
325
- if s < sum :
300
+ if s != 0 and s < sum :
326
301
path = elt
327
302
sum = s
328
303
329
304
tab = random .choice (path ).split ("#" )
330
305
tab [0 ]= int (tab [0 ])
331
306
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 ])
332
311
return tab
333
312
334
313
def weigh (graph , elt ):
335
314
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)
338
320
return sum
339
321
340
322
def main ():
0 commit comments