1
- """Games, or Adversarial Search. (Chapter 5)
2
- """
1
+ """Games, or Adversarial Search (Chapter 5)"""
3
2
4
3
import collections
5
- import math
6
4
import random
7
5
8
6
from utils import * # noqa
9
7
10
8
infinity = float ('inf' )
9
+ GameState = collections .namedtuple ('GameState' , 'to_move, utility, board, moves' )
11
10
12
11
# ______________________________________________________________________________
13
12
# Minimax Search
@@ -156,7 +155,7 @@ def alphabeta_player(game, state):
156
155
157
156
def play_game (game , * players ):
158
157
"""Play an n-person, move-alternating game."""
159
-
158
+
160
159
state = game .initial
161
160
while True :
162
161
for player in players :
@@ -170,7 +169,6 @@ def play_game(game, *players):
170
169
171
170
172
171
class Game :
173
-
174
172
"""A game is similar to a problem, but it has a utility for each
175
173
state and a terminal test instead of a path cost and a goal
176
174
test. To create a game, subclass this class and implement actions,
@@ -235,10 +233,8 @@ def terminal_test(self, state):
235
233
def to_move (self , state ):
236
234
return ('MIN' if state in 'BCD' else 'MAX' )
237
235
238
- GameState = collections .namedtuple ('GameState' , 'to_move, utility, board, moves' )
239
236
240
237
class TicTacToe (Game ):
241
-
242
238
"""Play TicTacToe on an h x v board, with Max (first player) playing 'X'.
243
239
A state has the player to move, a cached utility, a list of moves in
244
240
the form of a list of (x, y) positions, and a board, in the form of
0 commit comments