def main(bot): keepPlaying = True player = X h1('%s accepts your challenge!' % bot.name) while keepPlaying: game = TicTacToe() h2("Starting New Game!") h3("You are playing as %s." % player) while not game.isOver: if game.toPlay == player: game = playerTurn(player, game) else: game = botTurn(bot, game) h3(game.currentState) player = otherSide(player) keepPlaying = playAgainP() h3('Thank you for playing. Goodbye.')
def choose(self, game): root = game.path self.log('analyzing node: %s' % root) choices = [(branch, self.getScore(root, branch)) for branch in game.moves] opponent = otherSide(game.toPlay) # !! we'll track two numbers because we have to consider # both the number of paths where the opponent wins AND # the number of paths where we tie. The initial value here # is incredibly pessimistic (opponent wins every time). minMax = (fact(9), 0) best = None for branch, nodeScore in choices: score = (getattr(nodeScore, opponent), nodeScore.Tie) self.log(' .%s -> %6s' % (branch, score)) if score < minMax: minMax = score best = branch assert best is not None, "Something went terribly wrong!" return best