<commit_before>class InitialGameState

  attr_reader :warrior_max_health
  
  def initialize(warrior)
    @warrior_max_health = warrior.health
  end
end

class Player

  UnexpectedWarriorStateError = Class.new(StandardError)

  def initial_game_state(warrior)
    @initial_game_state ||= InitialGameState.new(warrior).freeze
  end

  def play_turn(warrior)
    init_state = initial_game_state(warrior)

    if facing_enemy?(warrior)
      warrior.attack!
    elsif taken_damage?(warrior, init_state.warrior_max_health)
      warrior.rest!
    elsif can_move_forward?(warrior)
      warrior.walk!(:forward)
    else
      raise UnexpectedWarriorStateError
    end
  end

  def can_move_forward?(warrior)
    warrior.feel(:forward).empty?
  end

  def facing_enemy?(warrior)
    warrior.feel(:forward).enemy?
  end

  def taken_damage?(warrior, max_health)
    warrior.health < max_health
  end
end
<commit_msg>Passing Ruby Warrior Level 4, needs refactoring.<commit_after>UnexpectedGameStateError = Class.new(StandardError)

class GameState

  attr_reader :warrior_health
  
  def initialize(warrior)
    @warrior_health = warrior.health
  end
end

class Player

  def play_turn(warrior)
    init_state = initial_game_state(warrior)

    if confronted_by?(:enemy, warrior)
      warrior.attack!
    elsif taken_damage?(warrior, init_state)
      if taken_damage?(warrior, last_game_state)
        warrior.walk!(:forward)
      else
        warrior.rest!
      end
    elsif can_move_forward?(warrior)
      warrior.walk!(:forward)
    else
      raise UnexpectedGameStateError
    end

    save_current_game_state(warrior)
  end

  ## game state

  attr_reader :last_game_state

  def initial_game_state(warrior)
    if @initial_game_state.nil?
      @initial_game_state = GameState.new(warrior).freeze
      save_current_game_state(warrior)
    end
    @initial_game_state
  end

  def save_current_game_state(warrior)
    @last_game_state = GameState.new(warrior).freeze
  end

  ## warrior status

  def can_move_forward?(warrior)
    warrior.feel(:forward).empty?
  end

  def confronted_by?(entity, warrior)
    warrior.feel(:forward).send("#{entity}?".to_sym)
  rescue NoMethodError
    false
  end

  def taken_damage?(warrior, game_state)
    warrior.health < game_state.warrior_health
  end
end

{'fraction_non_alphanumeric': 0.08033000434216239, 'fraction_numerical': 0.0004342162396873643, 'mean_word_length': 3.830188679245283, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2556557', 'n_tokens_mistral': 784, 'n_tokens_neox': 778, 'n_words': 140}