input: Write a function to implement a reinforcement learning environment that plays tic-tac-toe in Python. The function should include an initializer to set the initial state of the game, a reset method to reset the game to its initial state, a step function to take an action and update the state of the game, and a method to check if a player has won the game. The function should also include a game loop to play a game of tic-tac-toe using random actions.
  
  class TicTacToeEnv():
      def __init__(self):
          self.state = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
          self.player = 'X'
          self.player_turn = 0
      
      def reset(self):
          self.state = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
          self.player = 'X'
          self.player_turn = 0
          return self.state
          
      def step(self, action):
          self.state[action] = self.player
          winner = self.check_winner()
          done = False
          if winner == 'X' or winner == 'O':
              reward = 1
              done = True
          else:
              reward = 0
          if self.player == 'X':
              self.player = 'O'
          else:
              self.player = 'X'
          if self.player_turn == 8:
              done = True
              reward = 0.5
          self.player_turn += 1
          return self.state, reward, done, None
          
      def check_winner(self):
          win_state = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
          for i in win_state:
              if self.state[i[0]] == self.state[i[1]] == self.state[i[2]] == 'X':
                  return 'X'
              if self.state[i[0]] == self.state[i[1]] == self.state[i[2]] == 'O':
                  return 'O'
          return None
  
  assert TicTacToeEnv().check_winner() == None
  assert TicTacToeEnv().reset() == [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
  initial_state, reward, done, _ = TicTacToeEnv().step(0)
  assert initial_state == ['X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
  assert reward == 0
  assert done == False
  initial_state, reward, done, _ = TicTacToeEnv().step(1)
  assert initial_state == ['X', 'O', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
  assert reward == 0
  assert done == False
output: def __init__(self):
      self.state = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
      self.player = 'X'
      self.player_turn = 0
  
  def reset(self):
      self.state = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
      self.player = 'X'
      self.player_turn = 0
      return self.state
      
  def step(self, action):
      self.state[action] = self.player
      winner = self.check_winner()
      done = False
      if winner == 'X' or winner == 'O':
          reward = 1
          done = True
      else:
          reward = 0
      if self.player == 'X':
          self.player = 'O'
      else:
          self.player = 'X'
      if self.player_turn == 8:
          done = True
          reward = 0.5
      self.player_turn += 1
      return self.state, reward, done, None
      
  def check_winner(self):
      win_state = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
      for i in win_state:
          if self.state[i[0]] == self.state[i[1]] == self.state[i[2]] == 'X':
              return 'X'
          if self.state[i[0]] == self.state[i[1]] == self.state[i[2]] == 'O':
              return 'O'
      return None