# Path: simpleai/environments.py
# class RLEnvironment(Environment):
# 
#     def __init__(self, agents, initial_state):
#         super(RLEnvironment, self).__init__(agents, initial_state)
# 
#     def step(self, viewer=None):
#         super(RLEnvironment, self).step(viewer)
#         for agent in self.agents:
#             agent.set_reward(self.reward(self.state, agent), self.is_completed(self.state))
# 
#     def reward(self, state, agent):
#         raise NotImplementedError()


from simpleai.machine_learning.reinforcement_learning import TDQLearner, RLProblem, \
                                                             make_exponential_temperature, \
                                                             PerformanceCounter
from simpleai.environments import RLEnvironment
import random


class HumanPlayer(TicTacToePlayer):

    def set_reward(self, reward, terminal=False):
        print(('reward:', reward))

    def program(self, perception):
        print('Current board:')
        rows = perception.split()
        s = ['+-+-+-+-+', '| |0|1|2|', '+-+-+-+-+']
        for i, row in enumerate(rows):
            row = row.replace('_', ' ')
            s.append('|%d|' % i + '|'.join(list(row)) + '|')
            s.append('+-+-+-+-+')
        print(('\n'.join(s)))
        a = input('Make your move (r, c):')
        r, c = a
        return r * 3 + c


class RandomPlayer(TicTacToePlayer):

    def program(self, perception):
        try:
            return random.choice(self.problem.actions(self.problem.update_state(perception, self)))
        except:
            return None



# Path: simpleai/environments.py
# class RLEnvironment(Environment):
# 
#     def __init__(self, agents, initial_state):
#         super(RLEnvironment, self).__init__(agents, initial_state)
# 
#     def step(self, viewer=None):
#         super(RLEnvironment, self).step(viewer)
#         for agent in self.agents:
#             agent.set_reward(self.reward(self.state, agent), self.is_completed(self.state))
# 
#     def reward(self, state, agent):
#         raise NotImplementedError()


# Path: samples/machine_learning/tic_tac_toe.py
from simpleai.machine_learning.reinforcement_learning import TDQLearner, RLProblem, \
                                                             make_exponential_temperature, \
                                                             PerformanceCounter
from simpleai.environments import RLEnvironment
import random



class HumanPlayer(TicTacToePlayer):

    def set_reward(self, reward, terminal=False):
        print(('reward:', reward))

    def program(self, perception):
        print('Current board:')
        rows = perception.split()
        s = ['+-+-+-+-+', '| |0|1|2|', '+-+-+-+-+']
        for i, row in enumerate(rows):
            row = row.replace('_', ' ')
            s.append('|%d|' % i + '|'.join(list(row)) + '|')
            s.append('+-+-+-+-+')
        print(('\n'.join(s)))
        a = input('Make your move (r, c):')
        r, c = a
        return r * 3 + c


class RandomPlayer(TicTacToePlayer):

    def program(self, perception):
        try:
            return random.choice(self.problem.actions(self.problem.update_state(perception, self)))
        except:
            return None



class TicTacToeGame(RLEnvironment):