from keras.models import load_model
from . import Player
from ..utils import normalize_board, utility


class KerasPlayer(Player):
    '''
    Takes moves based on a Keras neural network model.
    '''

    name = 'Keras'

    def __init__(self, filepath):
        self.model = load_model(filepath)

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    ##########
    # Player #
    ##########

    def choose_move(self, state):
        assert state.cur_player() == 0
        best_action = None
        best_value = -1000000
        for action in state.legal_moves():
            s = state.copy()
            s = s.make_move(action)
            value = self.model.predict(normalize_board(s.board), batch_size=1)
            assert value >= -1.0 and value <= 1.0
            if value > best_value:
                best_action = action
                best_value = value
        return best_action

from keras.models import load_model
from . import Player
from ..utils import normalize_board, utility


class KerasPlayer(Player):
    '''
    Takes moves based on a Keras neural network model.
    '''

    name = 'Keras'

    def __init__(self, filepath):
        self.model = load_model(filepath)

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    ##########
    # Player #
    ##########

    def choose_move(self, game):
        assert game.cur_player() == 0
        best_move = None
        best_value = -1000000
        for move in game.legal_moves():
            next_game = game.copy().make_move(move)
            value = self.model.predict(normalize_board(next_game.board), batch_size=1)
            assert value >= -1.0 and value <= 1.0
            if value > best_value:
                best_move = move
                best_value = value
        return best_move

Rename state to game in KerasPlayer
Rename state to game in KerasPlayer

  from keras.models import load_model
  from . import Player
  from ..utils import normalize_board, utility
  
  
  class KerasPlayer(Player):
      '''
      Takes moves based on a Keras neural network model.
      '''
  
      name = 'Keras'
  
      def __init__(self, filepath):
          self.model = load_model(filepath)
  
      def __str__(self):
          return self.name
  
      def __repr__(self):
          return self.name
  
      ##########
      # Player #
      ##########
  
-     def choose_move(self, state):
+     def choose_move(self, game):
-         assert state.cur_player() == 0
+         assert game.cur_player() == 0
-         best_action = None
+         best_move = None
          best_value = -1000000
-         for action in state.legal_moves():
+         for move in game.legal_moves():
+             next_game = game.copy().make_move(move)
-             s = state.copy()
-             s = s.make_move(action)
-             value = self.model.predict(normalize_board(s.board), batch_size=1)
+             value = self.model.predict(normalize_board(next_game.board), batch_size=1)
              assert value >= -1.0 and value <= 1.0
              if value > best_value:
-                 best_action = action
+                 best_move = move
                  best_value = value
-         return best_action
+         return best_move
  
Rename state to game in KerasPlayer
## Code Before:
from keras.models import load_model
from . import Player
from ..utils import normalize_board, utility


class KerasPlayer(Player):
    '''
    Takes moves based on a Keras neural network model.
    '''

    name = 'Keras'

    def __init__(self, filepath):
        self.model = load_model(filepath)

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    ##########
    # Player #
    ##########

    def choose_move(self, state):
        assert state.cur_player() == 0
        best_action = None
        best_value = -1000000
        for action in state.legal_moves():
            s = state.copy()
            s = s.make_move(action)
            value = self.model.predict(normalize_board(s.board), batch_size=1)
            assert value >= -1.0 and value <= 1.0
            if value > best_value:
                best_action = action
                best_value = value
        return best_action

## Instruction:
Rename state to game in KerasPlayer
## Code After:
from keras.models import load_model
from . import Player
from ..utils import normalize_board, utility


class KerasPlayer(Player):
    '''
    Takes moves based on a Keras neural network model.
    '''

    name = 'Keras'

    def __init__(self, filepath):
        self.model = load_model(filepath)

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    ##########
    # Player #
    ##########

    def choose_move(self, game):
        assert game.cur_player() == 0
        best_move = None
        best_value = -1000000
        for move in game.legal_moves():
            next_game = game.copy().make_move(move)
            value = self.model.predict(normalize_board(next_game.board), batch_size=1)
            assert value >= -1.0 and value <= 1.0
            if value > best_value:
                best_move = move
                best_value = value
        return best_move
