<commit_before>require "deck"

class War
  attr_accessor :player1, :player2
  attr_reader :deck, :quiet

  def initialize(quiet = false)
    @player1 = []
    @player2 = []
    @deck = Deck.new
    @quiet = quiet

    deal_cards
  end

  def say(output)
    puts output unless quiet
  end

  def deal_cards
    deck.shuffle

    while deck.count > 0
      player1 << deck.draw
      player2 << deck.draw
    end
  end

  def battle
    card1 = player1.pop
    card2 = player2.pop

    pool = [card1, card2].shuffle

    if card1.value > card2.value
      player1.unshift(*pool)
      say "Player 1 wins with a #{card1.rank} vs a #{card2.rank}."
    elsif card2.value > card1.value
      player2.unshift(*pool)
      say "Player 2 wins with a #{card2.rank} vs a #{card1.rank}."
    else
      say "WORLD WAR!!! with a #{card1.rank}"
      world_war(card1, card2, pool)
    end
    say "#{player1.count} / #{player2.count}"
  end

  def world_war(prev_card1, prev_card2, pool)
    cards1 = player1.pop(4).compact
    cards2 = player2.pop(4).compact

    card1 = cards1.first || prev_card1
    card2 = cards2.first || prev_card2

    pool += cards1
    pool += cards2

    pool.shuffle!

    if card2.nil? || card1.value > card2.value
      player1.unshift(*pool)
      say "Player 1 wins the WORLD WAR with a #{card1.rank}!"
    elsif card1.nil? || card2.value > card1.value
      player2.unshift(*pool)
      say "Player 2 wins the WORLD WAR with a #{card2.rank}!"
    else
      say "MORE WORLD WAR!!! with a #{card1.rank}"
      world_war(card1, card2, pool)
    end
  end

  def run
    i = 0
    until player1.count == 0 || player2.count == 0
      battle
      i += 1
    end
    say "Turns: #{i}"
  end
end<commit_msg>Refactor to move winning calculation into method<commit_after>require "deck"

class War
  attr_accessor :player1, :player2
  attr_reader :deck, :quiet

  def initialize(quiet = false)
    @player1 = []
    @player2 = []
    @deck = Deck.new
    @quiet = quiet

    deal_cards
  end

  def say(output)
    puts output unless quiet
  end

  def deal_cards
    deck.shuffle

    while deck.count > 0
      player1 << deck.draw
      player2 << deck.draw
    end
  end

  def battle
    card1 = player1.pop
    card2 = player2.pop

    pool = [card1, card2].shuffle

    find_winner(card1, card2, pool)

    say "#{player1.count} / #{player2.count}"
  end

  def world_war(prev_card1, prev_card2, pool)
    cards1 = player1.pop(4).compact
    cards2 = player2.pop(4).compact

    card1 = cards1.first || prev_card1
    card2 = cards2.first || prev_card2

    pool += cards1
    pool += cards2

    pool.shuffle!

    find_winner(card1, card2, pool)
  end

  def find_winner(card1, card2, pool)
    if card1.value > card2.value
      player1.unshift(*pool)
      say "Player 1 wins with a #{card1.rank}!"
    elsif card2.value > card1.value
      player2.unshift(*pool)
      say "Player 2 wins with a #{card2.rank}!"
    else
      say "WORLD WAR!!! with a #{card1.rank}"
      world_war(card1, card2, pool)
    end
  end

  def run
    i = 0
    until player1.count == 0 || player2.count == 0
      battle
      i += 1
    end
    say "Turns: #{i}"
  end
end
{'fraction_non_alphanumeric': 0.11233480176211454, 'fraction_numerical': 0.03870358716173694, 'mean_word_length': 2.3711558854718984, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2524497', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1129, 'n_words': 312}