def minimax_decision(state, game):\n",
" """Given a state in a game, calculate the best move by searching\n",
" forward all the way to the terminal states. [Figure 5.3]"""\n",
"\n",
" player = game.to_move(state)\n",
"\n",
" def max_value(state):\n",
" if game.terminal_test(state):\n",
" return game.utility(state, player)\n",
" v = -infinity\n",
" for a in game.actions(state):\n",
" v = max(v, min_value(game.result(state, a)))\n",
" return v\n",
"\n",
" def min_value(state):\n",
" if game.terminal_test(state):\n",
" return game.utility(state, player)\n",
" v = infinity\n",
" for a in game.actions(state):\n",
" v = min(v, max_value(game.result(state, a)))\n",
" return v\n",
"\n",
" # Body of minimax_decision:\n",
" return argmax(game.actions(state),\n",
" key=lambda a: min_value(game.result(state, a)))\n",
"
\n",
"\n",
"\n"
],
"text/plain": [
"