{"id":1319,"date":"2023-03-25T10:12:45","date_gmt":"2023-03-25T02:12:45","guid":{"rendered":""},"modified":"2023-03-25T10:12:45","modified_gmt":"2023-03-25T02:12:45","slug":"\u8d4c\u80dc\u8d1f","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1319.html","title":{"rendered":"\u8d4c\u80dc\u8d1f"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

\u8d4c\u80dc\u8d1f<\/h1>\n

\u8d4c\u80dc\u8d1f\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u6e38\u620f(\u8d4c\u80dc\u8d1f)\u91c7\u7528\u7b56\u7565\u8fdb\u884c\u3002 \u6bcf\u4e2a\u7403\u5458\u6216\u7403\u961f\u5728\u5f00\u59cb\u6bd4\u8d5b\u524d\u90fd\u4f1a\u5236\u5b9a\u4e00\u4e2a\u6218\u7565\uff0c\u4ed6\u4eec\u5fc5\u987b\u6839\u636e\u76ee\u524d\u7684\u6bd4\u8d5b\u60c5\u51b5\u6539\u53d8\u6216\u5236\u5b9a\u65b0\u7684\u6218\u7565\u3002\n <\/div>\n

\u641c\u7d22\u7b97\u6cd5<\/h2>\n
\n \u8003\u8651\u7535\u8111\u6e38\u620f\u4e5f\u91c7\u7528\u4e0e\u4e0a\u8ff0\u76f8\u540c\u7684\u7b56\u7565\u3002 \u8bf7\u6ce8\u610f\uff0c\u641c\u7d22\u7b97\u6cd5\u662f\u8ba1\u7b97\u7535\u8111\u6e38\u620f\u7b56\u7565\u7684\u7b97\u6cd5\u3002\n <\/div>\n
\n \u600e\u4e48\u8fd0\u884c\u7684<\/strong>\u641c\u7d22\u7b97\u6cd5\u7684\u76ee\u6807\u662f\u627e\u5230\u6700\u4f18\u7684\u4e00\u7ec4\u79fb\u52a8\uff0c\u4ee5\u4fbf\u4ed6\u4eec\u53ef\u4ee5\u5230\u8fbe\u6700\u7ec8\u76ee\u7684\u5730\u5e76\u83b7\u80dc\u3002 \u8fd9\u4e9b\u7b97\u6cd5\u4f7f\u7528\u80dc\u51fa\u7684\u4e00\u7ec4\u6761\u4ef6\uff0c\u6bcf\u573a\u6bd4\u8d5b\u90fd\u6709\u6240\u4e0d\u540c\uff0c\u4ee5\u627e\u5230\u6700\u4f73\u7684\u79fb\u52a8\u65b9\u5f0f\u3002\n <\/div>\n
\n \u5c06\u7535\u8111\u6e38\u620f\u5f62\u8c61\u5316\u4e3a\u6811\u3002 \u6211\u4eec\u90fd\u77e5\u9053\u6811\u6709\u8282\u70b9\u3002 \u4ece\u6839\u5f00\u59cb\uff0c\u53ef\u4ee5\u8fdb\u5165\u6700\u7ec8\u7684\u83b7\u80dc\u8282\u70b9\uff0c\u4f46\u662f\u5177\u6709\u6700\u4f73\u7684\u79fb\u52a8\u8def\u5f84\u3002 \u8fd9\u662f\u641c\u7d22\u7b97\u6cd5\u7684\u5de5\u4f5c\u3002 \u8fd9\u79cd\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u672a\u6765\u7684\u72b6\u6001\u3002 \u641c\u7d22\u7b97\u6cd5\u641c\u7d22\u8fd9\u68f5\u6811\uff0c\u5728\u6e38\u620f\u7684\u6bcf\u4e2a\u6b65\u9aa4\u6216\u8282\u70b9\u505a\u51fa\u51b3\u5b9a\u3002\n <\/div>\n

\u7ec4\u5408\u641c\u7d22<\/h2>\n
\n \u4f7f\u7528\u641c\u7d22\u7b97\u6cd5\u7684\u4e3b\u8981\u7f3a\u70b9\u662f\u5b83\u4eec\u672c\u8d28\u4e0a\u662f\u7a77\u5c3d\u7684\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4ed6\u4eec\u63a2\u7d22\u6574\u4e2a\u641c\u7d22\u7a7a\u95f4\u4ee5\u627e\u5230\u5bfc\u81f4\u8d44\u6e90\u6d6a\u8d39\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5982\u679c\u8fd9\u4e9b\u7b97\u6cd5\u9700\u8981\u641c\u7d22\u6574\u4e2a\u641c\u7d22\u7a7a\u95f4\u4ee5\u627e\u5230\u6700\u7ec8\u89e3\u51b3\u65b9\u6848\uff0c\u90a3\u5c06\u66f4\u52a0\u9ebb\u70e6\u3002\n <\/div>\n
\n \u8981\u6d88\u9664\u8fd9\u6837\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ec4\u5408\u641c\u7d22\uff0c\u5b83\u4f7f\u7528\u542f\u53d1\u5f0f\u6765\u63a2\u7d22\u641c\u7d22\u7a7a\u95f4\uff0c\u5e76\u901a\u8fc7\u6d88\u9664\u53ef\u80fd\u7684\u9519\u8bef\u52a8\u4f5c\u6765\u51cf\u5c0f\u5176\u5927\u5c0f\u3002 \u56e0\u6b64\uff0c\u8fd9\u6837\u7684\u7b97\u6cd5\u53ef\u4ee5\u8282\u7701\u8d44\u6e90\u3002 \u8fd9\u91cc\u8ba8\u8bba\u4e86\u4e00\u4e9b\u4f7f\u7528\u542f\u53d1\u5f0f\u641c\u7d22\u7a7a\u95f4\u5e76\u8282\u7701\u8d44\u6e90\u7684\u7b97\u6cd5 -\n <\/div>\n

Minimax\u7b97\u6cd5<\/h2>\n
\n \u8fd9\u662f\u7ec4\u5408\u641c\u7d22\u4f7f\u7528\u542f\u53d1\u5f0f\u7b56\u7565\u52a0\u5feb\u641c\u7d22\u7b56\u7565\u7684\u7b56\u7565\u3002 Minimax\u7b56\u7565\u7684\u6982\u5ff5\u53ef\u4ee5\u901a\u8fc7\u4e24\u4e2a\u73a9\u5bb6\u6e38\u620f\u7684\u4f8b\u5b50\u6765\u7406\u89e3\uff0c\u5176\u4e2d\u6bcf\u4e2a\u73a9\u5bb6\u90fd\u8bd5\u56fe\u9884\u6d4b\u5bf9\u624b\u7684\u4e0b\u4e00\u6b65\u884c\u52a8\u5e76\u5c1d\u8bd5\u6700\u5c0f\u5316\u8be5\u529f\u80fd\u3002 \u800c\u4e14\uff0c\u4e3a\u4e86\u83b7\u80dc\uff0c\u73a9\u5bb6\u603b\u662f\u4f1a\u6839\u636e\u5f53\u524d\u7684\u60c5\u51b5\u5c1d\u8bd5\u6700\u5927\u5316\u81ea\u5df1\u7684\u529f\u80fd\u3002\n <\/div>\n
\n \u542f\u53d1\u5f0f\u5728\u50cfMinimax\u8fd9\u6837\u7684\u7b56\u7565\u4e2d\u626e\u6f14\u7740\u91cd\u8981\u7684\u89d2\u8272\u3002 \u6811\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u4f1a\u6709\u4e00\u4e2a\u4e0e\u4e4b\u76f8\u5173\u7684\u542f\u53d1\u5f0f\u51fd\u6570\u3002 \u57fa\u4e8e\u8fd9\u79cd\u542f\u53d1\u5f0f\u65b9\u6cd5\uff0c\u5b83\u5c06\u51b3\u5b9a\u5411\u6700\u6709\u5229\u4e8e\u4ed6\u4eec\u7684\u8282\u70b9\u8fc8\u8fdb\u3002\n <\/div>\n

Alpha-Beta\u4fee\u526a<\/h2>\n
\n Minimax\u7b97\u6cd5\u7684\u4e00\u4e2a\u4e3b\u8981\u95ee\u9898\u662f\u5b83\u53ef\u4ee5\u63a2\u7d22\u90a3\u4e9b\u65e0\u5173\u7684\u6811\u7684\u90e8\u5206\uff0c\u5bfc\u81f4\u8d44\u6e90\u7684\u6d6a\u8d39\u3002 \u56e0\u6b64\uff0c\u5fc5\u987b\u6709\u4e00\u4e2a\u7b56\u7565\u6765\u51b3\u5b9a\u6811\u7684\u54ea\u4e00\u90e8\u5206\u662f\u76f8\u5173\u7684\uff0c\u54ea\u4e00\u4e2a\u662f\u65e0\u5173\u7d27\u8981\u7684\uff0c\u5e76\u4e14\u5c06\u4e0d\u76f8\u5173\u7684\u90e8\u5206\u7559\u7ed9\u672a\u5f00\u53d1\u7684\u90e8\u5206\u3002 Alpha-Beta\u4fee\u526a\u5c31\u662f\u8fd9\u6837\u4e00\u79cd\u7b56\u7565\u3002\n <\/div>\n
\n Alpha-Beta\u4fee\u526a\u7b97\u6cd5\u7684\u4e3b\u8981\u76ee\u6807\u662f\u907f\u514d\u641c\u7d22\u6811\u4e2d\u6ca1\u6709\u4efb\u4f55\u89e3\u51b3\u65b9\u6848\u7684\u90a3\u4e9b\u90e8\u5206\u3002 Alpha-Beta\u4fee\u526a\u7684\u4e3b\u8981\u6982\u5ff5\u662f\u4f7f\u7528\u540d\u4e3aAlpha\u7684\u4e24\u4e2a\u8fb9\u754c(\u6700\u5927\u4e0b\u754c)\u548cBeta\uff0c\u5373\u6700\u5c0f\u4e0a\u754c\u3002 \u8fd9\u4e24\u4e2a\u53c2\u6570\u662f\u9650\u5236\u53ef\u80fd\u89e3\u51b3\u65b9\u6848\u96c6\u5408\u7684\u503c\u3002 \u5b83\u5c06\u5f53\u524d\u8282\u70b9\u7684\u503c\u4e0ealpha\u548cbeta\u53c2\u6570\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u79fb\u52a8\u5230\u5177\u6709\u89e3\u51b3\u65b9\u6848\u7684\u6811\u90e8\u5206\u5e76\u4e22\u5f03\u5176\u4f59\u90e8\u5206\u3002\n <\/div>\n

<\/span>Negamax\u7b97\u6cd5<\/h2>\n
\n \u8fd9\u4e2a\u7b97\u6cd5\u4e0eMinimax\u7b97\u6cd5\u6ca1\u6709\u533a\u522b\uff0c\u4f46\u5b83\u5177\u6709\u66f4\u4f18\u96c5\u7684\u5b9e\u73b0\u3002 \u4f7f\u7528Minimax\u7b97\u6cd5\u7684\u4e3b\u8981\u7f3a\u70b9\u662f\u9700\u8981\u5b9a\u4e49\u4e24\u4e2a\u4e0d\u540c\u7684\u542f\u53d1\u5f0f\u51fd\u6570\u3002 \u8fd9\u4e9b\u542f\u53d1\u5f0f\u4e4b\u95f4\u7684\u8054\u7cfb\u662f\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u73a9\u5bb6\u6765\u8bf4\u6e38\u620f\u7684\u72b6\u6001\u8d8a\u597d\uff0c\u5bf9\u53e6\u4e00\u4e2a\u73a9\u5bb6\u6765\u8bf4\u5c31\u8d8a\u7cdf\u7cd5\u3002 \u5728Negamax\u7b97\u6cd5\u4e2d\uff0c\u4e24\u4e2a\u542f\u53d1\u51fd\u6570\u7684\u76f8\u540c\u5de5\u4f5c\u662f\u5728\u5355\u4e2a\u542f\u53d1\u5f0f\u51fd\u6570\u7684\u5e2e\u52a9\u4e0b\u5b8c\u6210\u7684\u3002\n <\/div>\n

\u5efa\u8bbe\u673a\u5668\u4eba\u73a9\u6e38\u620f<\/h2>\n
\n \u8981\u5728AI\u4e2d\u6784\u5efa\u673a\u5668\u4eba\u73a9\u4e24\u4e2a\u73a9\u5bb6\u6e38\u620f\uff0c\u9700\u8981\u5b89\u88c5easyAI\u5e93\u3002 \u8fd9\u662f\u4e00\u4e2a\u4eba\u5de5\u667a\u80fd\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u6784\u5efa\u53cc\u4eba\u6e38\u620f\u7684\u6240\u6709\u529f\u80fd\u3002 \u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u4e0b\u8f7d\u5b83 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
pip install easyAI
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u4e00\u4e2a\u673a\u5668\u4eba\u73a9\u6700\u540e\u7684\u786c\u5e01<\/h2>\n
\n \u5728\u8fd9\u573a\u6bd4\u8d5b\u4e2d\uff0c\u4f1a\u6709\u4e00\u5806\u786c\u5e01\u3002 \u6bcf\u4e2a\u73a9\u5bb6\u5fc5\u987b\u4ece\u8be5\u5806\u4e2d\u53d6\u51fa\u4e00\u4e9b\u786c\u5e01\u3002\u8fd9\u573a\u6bd4\u8d5b\u7684\u76ee\u6807\u662f\u907f\u514d\u62ff\u4e0b\u6700\u540e\u4e00\u679a\u786c\u5e01\u3002 \u6211\u4eec\u5c06\u4f7f\u7528\u7ee7\u627f\u81eaeasyAI\u5e93\u7684TwoPlayersGame\u7c7b\u7684LastCoinStanding\u7c7b\u3002 \u4ee5\u4e0b\u4ee3\u7801\u663e\u793a\u4e86\u6b64\u6e38\u620f\u7684Python\u4ee3\u7801 -\n <\/div>\n
\n \u5982\u4e0b\u6240\u793a\u5bfc\u5165\u6240\u9700\u7684\u8f6f\u4ef6\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>easyAI import <\/span>TwoPlayersGame, id_solve, Human_Player, AI_Player
from <\/span>easyAI.AI import <\/span>TT
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u7ee7\u627fTwoPlayerGame\u7c7b\u4e2d\u7684\u7c7b\u6765\u5904\u7406\u6e38\u620f\u7684\u6240\u6709\u64cd\u4f5c -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
class <\/span>LastCoin_game(TwoPlayersGame):
    def <\/span>__init__(self, players):
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u8981\u73a9\u5bb6\u5e76\u5f00\u59cb\u6e38\u620f\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
self.players = players
self.nplayer = 1
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u6e38\u620f\u4e2d\u7684\u786c\u5e01\u6570\u91cf\uff0c\u8fd9\u91cc\u4f7f\u752815\u4e2a\u786c\u5e01\u8fdb\u884c\u6e38\u620f\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
self.num_coins = 15
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u73a9\u5bb6\u5728\u79fb\u52a8\u4e2d\u53ef\u4ee5\u83b7\u5f97\u7684\u6700\u5927\u786c\u5e01\u6570\u91cf\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
self.max_coins = 4
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\u6709\u4e00\u4e9b\u4e1c\u897f\u9700\u8981\u5b9a\u4e49\uff0c\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\u3002 \u5b9a\u4e49\u53ef\u80fd\u7684\u79fb\u52a8\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>possible_moves(self):
   return <\/span>[str(a) for <\/span><\/span>a in <\/span>range(1, self.max_coins + 1)]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u786c\u5e01\u7684\u6e05\u9664 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>make_move(self, move):
   self.num_coins -= int<\/span>(move)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u8c01\u62ff\u8d70\u4e86\u6700\u540e\u4e00\u679a\u786c\u5e01\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>win_game(self):
   return <\/span>self.num_coins <= 0
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u4f55\u65f6\u505c\u6b62\u6e38\u620f\uff0c\u5373\u4f55\u65f6\u6709\u4eba\u83b7\u80dc\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>is_over(self):
   return <\/span>self.win<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u5982\u4f55\u8ba1\u7b97\u5206\u6570\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>score(self):
   return <\/span>100 if <\/span>self.win_game<\/span>() else 0
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u5806\u4e2d\u5269\u4f59\u7684\u786c\u5e01\u6570\u91cf\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>show(self):
   print(self.num_coins, 'coins left in <\/span>the pile'<\/span>)
if <\/span>__name__ == \"__main__\"<\/span>:
   tt = TT()
   LastCoin_game.ttentry <\/span>= lambda <\/span>self: self.num_coins
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7528\u4e0b\u9762\u7684\u4ee3\u7801\u5757\u89e3\u51b3\u6e38\u620f -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
r, d, m = id_solve(LastCoin_game,
   range(2, 20), win_score=100, tt=tt)
print(r, d, m)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u51b3\u5b9a\u8c01\u5c06\u5f00\u59cb\u6e38\u620f\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
game = LastCoin_game([AI_Player(tt), Human_Player()])
game.play<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0b\u9762\u7684\u8f93\u51fa\u6f14\u793a\u8fd9\u4e2a\u6e38\u620f\u7684\u7b80\u5355\u73a9\u6cd5 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
d:2, a:0, m:1
d:3, a:0, m:1
d:4, a:0, m:1
d:5, a:0, m:1
d:6, a:100, m:4
1 6 4
15 coins left in <\/span>the pile
Move #1: player 1 plays 4 :
<\/span> 11 coins left in <\/span>the pile
Player 2 what do you play ? 2
Move #2: player 2 plays 2 :
<\/span> 9 coins left in <\/span>the pile
Move #3: player 1 plays 3 :
<\/span> 6 coins left in <\/span>the pile
Player 2 what do you play ? 1
Move #4: player 2 plays 1 :
<\/span> 5 coins left in <\/span>the pile
Move #5: player 1 plays 4 :
<\/span> 1 coins left in <\/span>the pile
Player 2 what do you play ? 1
Move #6: player 2 plays 1 :
<\/span> 0 coins left in <\/span>the pile
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/span>\u673a\u5668\u4eba\u73a9\u4e95\u5b57\u6e38\u620f<\/h2>\n
\n Tic-Tac-Toe\u975e\u5e38\u719f\u6089\uff0c\u662f\u6700\u53d7\u6b22\u8fce\u7684\u6e38\u620f\u4e4b\u4e00\u3002\u6211\u4eec\u901a\u8fc7\u4f7f\u7528Python\u4e2d\u7684easyAI\u5e93\u6765\u521b\u5efa\u8fd9\u4e2a\u6e38\u620f\u3002 \u4ee5\u4e0b\u4ee3\u7801\u662f\u8fd9\u6b3e\u6e38\u620f\u7684Python\u4ee3\u7801 -\n <\/div>\n
\n \u5982\u4e0b\u6240\u793a\u5bfc\u5165\u8f6f\u4ef6\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>easyAI import <\/span>TwoPlayersGame, AI_Player, Negamax
from <\/span>easyAI.Player import <\/span>Human_Player
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7ee7\u627fTwoPlayerGame\u4e2d\u7684\u7c7b\u6765\u5904\u7406\u6e38\u620f\u7684\u6240\u6709\u64cd\u4f5c -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
class <\/span>TicTacToe_game(TwoPlayersGame):
   def <\/span>__init__(self, players):
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u5b9a\u4e49\u73a9\u5bb6\u5e76\u5f00\u59cb\u6e38\u620f -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
self.players = players
self.nplayer = 1
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u677f\u7684\u7c7b\u578b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
self.board = [0] * 9
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u53ef\u80fd\u7684\u4e3e\u63aa(\u52a8\u4f5c)\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>possible_moves(self):
    return <\/span>[x + 1 for <\/span><\/span>x, y in <\/span>enumerate(self.board) if <\/span>y == 0]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u4e00\u4e2a\u73a9\u5bb6\u7684\u4e3e\u63aa(\u52a8\u4f5c) -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>make_move(self, move):
    self.board[int<\/span>(move) - 1] = self.nplayer
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u4e00\u4e2a\u73a9\u5bb6\u4f55\u65f6\u8fdb\u884c\u79fb\u52a8 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>umake_move(self, move):
   self.board[int<\/span>(move) - 1] = 0
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u8f93\u6761\u4ef6\u662f\u5bf9\u624b\u5728\u4e00\u6761\u7ebf\u4e0a\u6709\u4e09\u4e2a -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>condition_for_lose(self):
   possible_combinations = [[1,2,3], [4,5,6], [7,8,9],
      [1,4,7], [2,5,8], [3,6,9], [1,5,9], [3,5,7]]
   return <\/span>any([all([(self.board[z-1] == self.nopponent)
      for <\/span><\/span>z in <\/span>combination]) for <\/span><\/span>combination in <\/span>possible_combinations])
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u6e38\u620f\u7ed3\u675f\u7684\u6761\u4ef6 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>is_over(self):
   return <\/span>(self.possible_moves<\/span>() == []) or <\/span>self.condition_for_lose<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u663e\u793a\u73a9\u5bb6\u5728\u6e38\u620f\u4e2d\u7684\u5f53\u524d\u4f4d\u7f6e -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>show(self):
   print('\\n'<\/span><\/span>+'\\n'.join<\/span>([' '<\/span>.join<\/span>([['.'<\/span>, 'O'<\/span>, 'X'<\/span>][self.board[3*j + i]]
      for <\/span><\/span>i in <\/span>range(3)]) for <\/span><\/span>j in <\/span>range(3)]))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8ba1\u7b97\u5206\u6570\u4ee3\u7801 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>scoring(self):
   return <\/span>-100 if <\/span>self.condition_for_lose<\/span>() else 0
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u5b9a\u4e49\u7b97\u6cd5\u5e76\u5f00\u59cb\u6e38\u620f\u7684\u4e3b\u8981\u65b9\u6cd5 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
if <\/span>__name__ == \"__main__\"<\/span>:
   algo = Negamax(7)
   TicTacToe_game([Human_Player(), AI_Player(algo)]).play<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u53ef\u4ee5\u770b\u5230\u4e0b\u9762\u7684\u8f93\u51fa\u548c\u8fd9\u4e2a\u6e38\u620f\u7684\u7b80\u5355\u73a9\u6cd5 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
. . .
. . .
. . .
Player 1 what do you play ? 1
Move #1: player 1 plays 1 :
<\/span> O . .
. . .
. . .
Move #2: player 2 plays 5 :
<\/span> O . .
. X .
121
. . .
Player 1 what do you play ? 3
Move #3: player 1 plays 3 :
<\/span> O . O
. X .
. . .
Move #4: player 2 plays 2 :
<\/span> O X O
. X .
. . .
Player 1 what do you play ? 4
Move #5: player 1 plays 4 :
<\/span> O X O
O X .
. . .
Move #6: player 2 plays 8 :
<\/span> O X O
O X .
. X .
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"\u8d4c\u80dc\u8d1fzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[],"class_list":["post-1319","post","type-post","status-publish","format-standard","hentry","category-rgznjc1"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1319"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1319"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1319\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}