@@ -118,7 +118,7 @@ def query_player(game, state):
118
118
119
119
def random_player (game , state ):
120
120
"A player that chooses a legal move at random."
121
- return random .choice (game .legal_moves ())
121
+ return random .choice (game .legal_moves (state ))
122
122
123
123
def alphabeta_player (game , state ):
124
124
return alphabeta_search (state , game )
@@ -187,16 +187,22 @@ class Fig62Game(Game):
187
187
>>> alphabeta_search('A', g)
188
188
'a1'
189
189
"""
190
- succs = { 'A' : [( 'a1' , 'B' ), ( 'a2' , 'C' ), ( 'a3' , ' D' )] ,
191
- 'B' : [( 'b1' , ' B1'), ( 'b2' , ' B2'), ( 'b3' , ' B3' )] ,
192
- 'C' : [( 'c1' , ' C1'), ( 'c2' , ' C2'), ( 'c3' , ' C3' )] ,
193
- 'D' : [( 'd1' , ' D1'), ( 'd2' , ' D2'), ( 'd3' , ' D3' )]}
190
+ succs = dict ( A = dict ( a1 = 'B' , a2 = 'C' , a3 = ' D' ),
191
+ B = dict ( b1 = ' B1', b2 = ' B2', b3 = ' B3' ),
192
+ C = dict ( c1 = ' C1', c2 = ' C2', c3 = ' C3' ),
193
+ D = dict ( d1 = ' D1', d2 = ' D2', d3 = ' D3' ))
194
194
utils = Dict (B1 = 3 , B2 = 12 , B3 = 8 , C1 = 2 , C2 = 4 , C3 = 6 , D1 = 14 , D2 = 5 , D3 = 2 )
195
195
initial = 'A'
196
196
197
+ def legal_moves (self , state ):
198
+ return [move for (move , next ) in self .successors (state )]
199
+
200
+ def make_move (self , move , state ):
201
+ return self .succs [state ][move ]
202
+
197
203
def successors (self , state ):
198
- return self .succs .get (state , [] )
199
-
204
+ return self .succs .get (state , {}). items ( )
205
+
200
206
def utility (self , state , player ):
201
207
if player == 'MAX' :
202
208
return self .utils [state ]
@@ -283,3 +289,8 @@ def __init__(self, h=7, v=6, k=4):
283
289
def legal_moves (self , state ):
284
290
return [(x , y ) for (x , y ) in state .moves
285
291
if y == 0 or (x , y - 1 ) in state .board ]
292
+
293
+ __doc__ += random_tests ("""
294
+ >>> play_game(Fig62Game(), random_player, random_player)
295
+ -6
296
+ """ )
0 commit comments