Skip to content

Commit 39bc660

Browse files
authored
query_player: re-prompt until a legal move is entered (#1332)
Loop query_player until the entered move is legal. Supersedes #1078 by @SandersLin.
1 parent 70a2740 commit 39bc660

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎games.py‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,15 @@ def query_player(game, state):
183183
print("")
184184
move = None
185185
if game.actions(state):
186-
move_string = input('Your move? ')
187-
try:
188-
move = eval(move_string)
189-
except NameError:
190-
move = move_string
186+
while True:
187+
move_string = input('Your move? ')
188+
try:
189+
move = eval(move_string)
190+
except NameError:
191+
move = move_string
192+
if move in game.actions(state):
193+
break
194+
print('illegal move, try again')
191195
else:
192196
print('no legal moves: passing turn to next player')
193197
return move

0 commit comments

Comments
 (0)