<commit_before>require "tictactoe/player"

module Tictactoe
  class MinimaxPlayer < Player
    def move( board )
      # Obtains the available movements
      moves = board.moves

      # For each available movement
      bestValue = -2
      bestMove = ""
      moves.each do |move|
        newboard = Board.new(board.squares)
        value = search_best_move(newboard, move, self.mark, 1)
        if (value > bestValue)
          bestValue = value
          bestMove = move
        end
      end
      bestMove
    end

    def opponent_mark
      if (self.mark == "X")
        return "O"
      else
        return "X"
      end
    end

    def search_best_move(board, move, mark, level)

      # Puts the movement on the board
      board[move] = mark
      moves = board.moves

      # Max level
      if (level % 2 == 1)
        return 1 if (board.won? == mark)
        return 0 if (board.won? == " ")
        return -1 if (board.won? == opponent_mark)
        if (!board.won?)
          bestValue = -2;
          moves.each do |mov|
            newboard = Board.new(board.squares)
            value = search_best_move(newboard, mov, opponent_mark, level + 1)
            if (value > bestValue)
              bestValue = value
            end
          end
          return bestValue
        end
      # Min level
      elsif (level % 2 == 0)
        return -1 if (board.won? == opponent_mark)
        return 0 if (board.won? == " ")
        return 1 if (board.won? == self.mark)
        if (!board.won?)
          bestValue = 2;
          moves.each do |mov|
            newboard = Board.new(board.squares)
            value = search_best_move(newboard, mov, self.mark, level + 1)
            if (value < bestValue)
              bestValue = value
            end
          end
          return bestValue
        end
      end
    end
  end
end<commit_msg>Some bugs in minimax player have been solved<commit_after>require "tictactoe/player"

module Tictactoe
  class MinimaxPlayer < Player
    def move( board )
      # Obtains the available movements
      moves = board.moves

      # For each available movement
      bestValue = -2
      bestMove = ""
      moves.each do |move|
        newsquares = board.squares.dup
        newboard = Board.new(newsquares)
        value = search_best_move(newboard, move, self.mark, 1)
        if (value >= bestValue)
          bestValue = value
          bestMove = move
        end
      end
      bestMove
    end

    def search_best_move(board, move, mark, level)
      # Puts the movement on the board
      board[move] = mark
      moves = board.moves

      # Checks whether there is a winner or not
      return 1 if (board.won? == self.mark)
      return 0 if (board.won? == " ")
      return -1 if (board.won? == opponent_mark)

      # Max level
      if ((level % 2 == 1) && (!board.won?))
        bestValue = -2;
        moves.each do |mov|
          newsquares = board.squares.dup
          newboard = Board.new(newsquares)
          value = search_best_move(newboard, mov, opponent_mark, level + 1)
          if (value >= bestValue)
            bestValue = value
          end
        end
      # Min level
      elsif ((level % 2 == 0) && (!board.won?))
        bestValue = 2;
        moves.each do |mov|
          newsquares = board.squares.dup
          newboard = Board.new(newsquares)
          value = search_best_move(newboard, mov, self.mark, level + 1)
          if (value <= bestValue)
            bestValue = value
          end
        end
      end
      bestValue
    end

    def opponent_mark
      if (self.mark == "X")
        return "O"
      else
        return "X"
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.09110688060919228, 'fraction_numerical': 0.007886864291542018, 'mean_word_length': 1.62339514978602, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '100027', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1097, 'n_words': 329}