Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c68651a

Browse files
committed
Fixed issue 8 and incomplete definition of Fig62Game.
1 parent bf3c4a5 commit c68651a

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

games.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def query_player(game, state):
118118

119119
def random_player(game, state):
120120
"A player that chooses a legal move at random."
121-
return random.choice(game.legal_moves())
121+
return random.choice(game.legal_moves(state))
122122

123123
def alphabeta_player(game, state):
124124
return alphabeta_search(state, game)
@@ -187,16 +187,22 @@ class Fig62Game(Game):
187187
>>> alphabeta_search('A', g)
188188
'a1'
189189
"""
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'))
194194
utils = Dict(B1=3, B2=12, B3=8, C1=2, C2=4, C3=6, D1=14, D2=5, D3=2)
195195
initial = 'A'
196196

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+
197203
def successors(self, state):
198-
return self.succs.get(state, [])
199-
204+
return self.succs.get(state, {}).items()
205+
200206
def utility(self, state, player):
201207
if player == 'MAX':
202208
return self.utils[state]
@@ -283,3 +289,8 @@ def __init__(self, h=7, v=6, k=4):
283289
def legal_moves(self, state):
284290
return [(x, y) for (x, y) in state.moves
285291
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

Comments
 (0)