ass3: implement minimax_search
This commit is contained in:
@@ -47,8 +47,30 @@ class Game:
|
|||||||
|
|
||||||
|
|
||||||
def minimax_search(game: Game, state: State) -> Action | None:
|
def minimax_search(game: Game, state: State) -> Action | None:
|
||||||
# YOUR CODE HERE
|
_, result = max_value(game, state)
|
||||||
assert False, "Not implemented"
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def max_value(game: Game, state: State) -> tuple[float, Action | None]:
|
||||||
|
if game.is_terminal(state):
|
||||||
|
return game.utility(state, player), None
|
||||||
|
v, move = float("-inf"), float("-inf")
|
||||||
|
for a in game.actions(state):
|
||||||
|
v2, a2 = min_value(game, game.result(state, a))
|
||||||
|
if v2 > v:
|
||||||
|
v, move = v2, a
|
||||||
|
return float(v), Action(move)
|
||||||
|
|
||||||
|
|
||||||
|
def min_value(game: Game, state: State) -> tuple[float, Action | None]:
|
||||||
|
if game.is_terminal(state):
|
||||||
|
return game.utility(state, player), None
|
||||||
|
v, move = float("+inf"), float("+inf")
|
||||||
|
for a in game.actions(state):
|
||||||
|
v2, a2 = max_value(game, game.result(state, a))
|
||||||
|
if v2 < v:
|
||||||
|
v, move = v2, a
|
||||||
|
return float(v), Action(move)
|
||||||
|
|
||||||
|
|
||||||
game = Game(5)
|
game = Game(5)
|
||||||
|
|||||||
Reference in New Issue
Block a user