class ComputerPlayer(object): # Loss, Tie, Win rewards = [50, 400, 900] table = None def __init__(self, game, player): self.player = player self.agent = Agent("tictactoe") self.game = game def generate(self): "Generate a move for the computer player." msg = sum(x << (n*2) for n, x in enumerate(self.game.board)) msg |= self.player << 18 message = msg for n in count(1): action = self.agent.process(message) pos = action % 9 if self.game.board[pos]: self.game.output("%d round%s", n, s(n)) self.game.goto(*self.game.square[pos]) self.game.win.refresh() # Keep part of the failure around. message = ((action << 20) & 0xFFFF0000) | msg else: break return pos def result(self, points): bonus = self.rewards[points + 1] self.agent.reward(bonus)
def __init__(self, game, player): self.player = player self.agent = Agent("tictactoe") self.game = game