EasyAI是一个纯 Pytho编写的人工智能框架,用于双人对弈类游戏,如TicTacToe、Coect4、Reversi等。它可以轻松地定义游戏机制,并与电脑进行对战。
简单示例首先定义一个游戏规则,并开始与AI的比赛:
from easyAI import TwoPlayersGame, Huma_Player, AI_Player, Negamaxclass GameOfBoes( TwoPlayersGame ): """ I tur, the players remove oe, two or three boes from a pile of boes. The player who removes the last boe loses. """ def __iit__(self, players): self.players = players self.pile = 20 # start with 20 boes i the pile self.player = 1 # player 1 starts def possible_moves(self): retur ['1','2','3'] def make_move(self,move): self.pile -= it(move) # remove boes. def wi(self): retur self.pile<=0 # oppoet took the last boe ? def is_over(self): retur self.wi() # Game stops whe someoe wis. def show(self): prit "%d boes left i the pile"%self.pile def scorig(self): retur 100 if game.wi() else 0 # For the AI# Start a match (ad store the history of moves whe it eds)ai = Negamax(13) # The AI will thik 13 moves i advacegame = GameOfBoes( [ Huma_Player(), AI_Player(ai) ] )history = game.play()结果:
20 boes left i the pilePlayer 1 what do you play ? 3Move #1: player 1 plays 3 :17 boes left i the pileMove #2: player 2 plays 1 :16 boes left i the pilePlayer 1 what do you play ?






评论