{"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
\u8d4c\u80dc\u8d1f\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n\u641c\u7d22\u7b97\u6cd5<\/h2>\n
\u7ec4\u5408\u641c\u7d22<\/h2>\n
Minimax\u7b97\u6cd5<\/h2>\n
Alpha-Beta\u4fee\u526a<\/h2>\n
<\/span>Negamax\u7b97\u6cd5<\/h2>\n
\u5efa\u8bbe\u673a\u5668\u4eba\u73a9\u6e38\u620f<\/h2>\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
# 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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
# 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 # 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 # 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 # 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 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>