<commit_before>module LocoBot
  # Representation of a table on which robots can be placed.
  class Table

    # @!attribute [r] width
    # @return [Fixnum] width of the Table
    # Determines the farthest accessible point on the Table's x-axis.
    attr_reader :width

    # @!attribute [r] height
    # @return [Fixnum] height of the table
    # Determines the farthest accessible point on the Table's y-axis.
    attr_reader :height

    # @!attribute [r] robots
    # @return [Array<Robot>] a collection of [Robot]
    # The collection of Robots currently placed on the Table.
    attr_reader :robots

    # @param width [Integer] The width of the Table
    # @param height [Integer] The height of the Table
    def initialize(width = 5, height = 5)
      @width = width
      @height = height
      @robots = []

      validate_dimensions
    end

    # Places the given Robot on the Table at the given coordinates, facing the given Direction.
    # If the Robot was previously on another Table, it is removed from it before being placed on the current.
    # @param robot [Robot] the Robot to place on the Table
    # @param x [Integer] the x-axis coordinate
    # @param y [Integer] the y-axis coordinate
    # @param direction [Direction] the facing Direction
    # @return [Boolean] true if placing was successful, false otherwise.
    def place_robot(robot, x, y, direction)
      return false unless position_valid?(x, y)

      robot.table.remove_robot(robot) if robot.table

      robot.table = self
      robot.direction = direction
      robot.x = x
      robot.y = y

      @robots.push(robot) unless robots.include?(robot)

      true
    end

    # Removes the given Robot from the Table. Position attributes of the Robot are set to nil
    # @param robot [Robot] the Robot to remove from the Table
    # @return [Boolean] true if removing was successful, false otherwise.
    def remove_robot(robot)
      return false unless robots.include?(robot)

      robot.table = nil
      robot.x = nil
      robot.y = nil
      robot.direction = nil

      @robots.delete(robot)

      true
    end

    # Determines whether a Robot can be placed at the given coordinates.
    # @param x [Integer] the x-axis coordinate
    # @param y [Integer] the y-axis coordinate
    # @return [Boolean] true if a Robot can be placed at the given coordinates, false otherwise.
    def position_valid?(x, y)
      position_within_bounds?(x, y) and position_free?(x, y)
    end

    private

    def position_within_bounds?(x, y)
      x >= 0 && y >= 0 && x < width && y < height
    end

    def position_free?(x, y)
      robots.none? {|robot| robot.x == x && robot.y == y }
    end

    def validate_dimensions
      errors = []<commit_msg>Refactor Table methods so they modify the Robot's instance variables<commit_after>module LocoBot
  # Representation of a table on which robots can be placed.
  class Table

    # @!attribute [r] width
    # @return [Fixnum] width of the Table
    # Determines the farthest accessible point on the Table's x-axis.
    attr_reader :width

    # @!attribute [r] height
    # @return [Fixnum] height of the table
    # Determines the farthest accessible point on the Table's y-axis.
    attr_reader :height

    # @!attribute [r] robots
    # @return [Array<Robot>] a collection of [Robot]
    # The collection of Robots currently placed on the Table.
    attr_reader :robots

    # @param width [Integer] The width of the Table
    # @param height [Integer] The height of the Table
    def initialize(width = 5, height = 5)
      @width = width
      @height = height
      @robots = []

      validate_dimensions
    end

    # Places the given Robot on the Table at the given coordinates, facing the given Direction.
    # If the Robot was previously on another Table, it is removed from it before being placed on the current.
    # @param robot [Robot] the Robot to place on the Table
    # @param x [Integer] the x-axis coordinate
    # @param y [Integer] the y-axis coordinate
    # @param direction [Direction] the facing Direction
    # @return [Boolean] true if placing was successful, false otherwise.
    def place_robot(robot, x, y, direction)
      return false unless position_valid?(x, y)

      robot.remove if robot.table

      robot.instance_variable_set(:@table, self)
      robot.instance_variable_set(:@x, x)
      robot.instance_variable_set(:@y, y)
      robot.instance_variable_set(:@direction, direction)

      @robots.push(robot) unless robots.include?(robot)

      true
    end

    # Removes the given Robot from the Table. Position attributes of the Robot are set to nil.
    # @param robot [Robot] the Robot to remove from the Table
    # @return [Boolean] true if removing was successful, false otherwise.
    def remove_robot(robot)
      return false unless robots.include?(robot)

      robot.instance_variable_set(:@table, nil)
      robot.instance_variable_set(:@x, nil)
      robot.instance_variable_set(:@y, nil)
      robot.instance_variable_set(:@direction, nil)

      @robots.delete(robot)

      true
    end

    # Determines whether a Robot can be placed at the given coordinates.
    # @param x [Integer] the x-axis coordinate
    # @param y [Integer] the y-axis coordinate
    # @return [Boolean] true if a Robot can be placed at the given coordinates, false otherwise.
    def position_valid?(x, y)
      position_within_bounds?(x, y) and position_free?(x, y)
    end

    private

    def position_within_bounds?(x, y)
      x >= 0 && y >= 0 && x < width && y < height
    end

    def position_free?(x, y)
      robots.none? {|robot| robot.x == x && robot.y == y }
    end

    def validate_dimensions
      errors = []
{'fraction_non_alphanumeric': 0.08498335377606449, 'fraction_numerical': 0.0014017872787804452, 'mean_word_length': 2.851551956815115, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '7181867', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1674, 'n_words': 657}