EasyAI是一个纯 Python编写的人工智能框架,用于双人对弈类游戏,如TicTacToe、Connect4、Reversi等。它可以轻松地定义游戏机制,并与电脑进行对战。
简单示例首先定义一个游戏规则,并开始与AI的比赛:
from easyAI import TwoPlayersGame, Human_Player, AI_Player, Negamaxclass GameOfBones( TwoPlayersGame ): """ In turn, the players remove one, two or three bones from a pile of bones. The player who removes the last bone loses. """ def __init__(self, players): self.players = players self.pile = 20 # start with 20 bones in the pile self.nplayer = 1 # player 1 starts def possible_moves(self): return ['1','2','3'] def make_move(self,move): self.pile -= int(move) # remove bones. def win(self): return self.pile<=0 # opponent took the last bone ? def is_over(self): return self.win() # Game stops when someone wins. def show(self): print "%d bones left in the pile"%self.pile def scoring(self): return 100 if game.win() else 0 # For the AI# Start a match (and store the history of moves when it ends)ai = Negamax(13) # The AI will think 13 moves in advancegame = GameOfBones( [ Human_Player(), AI_Player(ai) ] )history = game.play()结果:
20 bones left in the pilePlayer 1 what do you play ? 3Move #1: player 1 plays 3 :17 bones left in the pileMove #2: player 2 plays 1 :16 bones left in the pilePlayer 1 what do you play ?
评论