@@ -136,6 +136,10 @@ def min_value(state, alpha, beta, depth):
136
136
137
137
def query_player (game , state ):
138
138
"""Make a move by querying standard input."""
139
+ print ("current state:" )
140
+ game .display (state )
141
+ print ("available moves: {}" .format (game .actions (state )))
142
+ print ("" )
139
143
move_string = input ('Your move? ' )
140
144
try :
141
145
move = eval (move_string )
@@ -153,18 +157,6 @@ def alphabeta_player(game, state):
153
157
return alphabeta_full_search (state , game )
154
158
155
159
156
- def play_game (game , * players ):
157
- """Play an n-person, move-alternating game."""
158
-
159
- state = game .initial
160
- while True :
161
- for player in players :
162
- move = player (game , state )
163
- state = game .result (state , move )
164
- if game .terminal_test (state ):
165
- game .display (state )
166
- return game .utility (state , game .to_move (game .initial ))
167
-
168
160
# ______________________________________________________________________________
169
161
# Some Sample Games
170
162
@@ -204,6 +196,17 @@ def display(self, state):
204
196
205
197
def __repr__ (self ):
206
198
return '<{}>' .format (self .__class__ .__name__ )
199
+
200
+ def play_game (self , * players ):
201
+ """Play an n-person, move-alternating game."""
202
+ state = self .initial
203
+ while True :
204
+ for player in players :
205
+ move = player (self , state )
206
+ state = self .result (state , move )
207
+ if self .terminal_test (state ):
208
+ self .display (state )
209
+ return self .utility (state , self .to_move (self .initial ))
207
210
208
211
209
212
class Fig52Game (Game ):
@@ -255,7 +258,9 @@ def actions(self, state):
255
258
256
259
def result (self , state , move ):
257
260
if move not in state .moves :
258
- return state # Illegal move has no effect
261
+ return GameState (to_move = ('O' if state .to_move == 'X' else 'X' ),
262
+ utility = self .compute_utility (state .board , move , state .to_move ),
263
+ board = state .board , moves = state .moves ) # Illegal move has no effect
259
264
board = state .board .copy ()
260
265
board [move ] = state .to_move
261
266
moves = list (state .moves )
0 commit comments