<commit_before>class Game < ActiveRecord::Base
  after_initialize :init
  after_find :after_find
  before_save :before_save
  after_save :after_save

  def self.copy_grid(grid)
    new_grid = []
    grid.each { |array| new_grid.push(array.clone) }
    new_grid
  end

  def init
    if new_record?
      self.status = :in_play
      self.moves = []
      self.grid = [[],[],[],[],[],[],[]]
    end
  end

  def evaluate_status
    evaluate_winner(last_colour)
    evaluate_game_over
  end

  def last_colour
    self.moves.last[0]
  end

  def evaluate_winner(colour)
    self.winner = colour if has_connect_four(colour)
  end

  def evaluate_game_over
    self.status = :finished if self.winner
  end

  def has_connect_four(colour)
    has_horizontal_line(colour) || has_vertical_line(colour) || has_diagonal_line(colour)
  end

  def has_horizontal_line(colour)
    (0..6).each do |x|
      return true if has_straight_line(colour, x, true)
    end
    false
  end

  def has_vertical_line(colour)
    (0..6).each do |y|
      return true if has_straight_line(colour, y, false)
    end
    false
  end

  def has_diagonal_line(colour)
    (-3..3).each do |x|
      return true if has_diagonal(colour, x, 1)
    end
    (3..9).each do |x|
      return true if has_diagonal(colour, x, -1)
    end
    false
  end

  def has_straight_line(colour, a, horizonal)
    counter = 0
    (0..6).each do |b|
      value = horizonal ? grid_value(a, b) : grid_value(b, a)
      if value == colour
        counter += 1
      else
        counter = 0
      end
      return true if counter == 4
    end
    false
  end

  def has_diagonal(colour, x, direction)
    counter = 0
    (0..6).each do |y|
      if grid_value(x + (direction * y), y) == colour
        counter += 1
      else
        counter = 0
      end
      return true if counter == 4
    end
    false
  end

  def grid_value(x, y)
    return nil if x < 0 || x >= 7 || y < 0 || y >= 7
    stack = self.grid[y]
    stack.length > x ? stack[x] : nil
  end

  def apply_move(colour, column)
    self.moves << [colour, column]
    self.grid[0] << colour
  end

  def before_save
    self.moves = self.moves.to_json
    self.grid = self.grid.to_json
  end

  def after_save
    self.moves = JSON.parse(self.moves)
    self.grid = JSON.parse(self.grid)
  end

  def after_find
    self.status = self.status.to_sym
    self.moves = JSON.parse(self.moves)
    self.grid = JSON.parse(self.grid)
  end
end
<commit_msg>Stupid bug in the Game#apply_move method<commit_after>class Game < ActiveRecord::Base
  after_initialize :init
  after_find :after_find
  before_save :before_save
  after_save :after_save

  def self.copy_grid(grid)
    new_grid = []
    grid.each { |array| new_grid.push(array.clone) }
    new_grid
  end

  def init
    if new_record?
      self.status = :in_play
      self.moves = []
      self.grid = [[],[],[],[],[],[],[]]
    end
  end

  def evaluate_status
    evaluate_winner(last_colour)
    evaluate_game_over
  end

  def last_colour
    self.moves.last[0]
  end

  def evaluate_winner(colour)
    self.winner = colour if has_connect_four(colour)
  end

  def evaluate_game_over
    self.status = :finished if self.winner
  end

  def has_connect_four(colour)
    has_horizontal_line(colour) || has_vertical_line(colour) || has_diagonal_line(colour)
  end

  def has_horizontal_line(colour)
    (0..6).each do |x|
      return true if has_straight_line(colour, x, true)
    end
    false
  end

  def has_vertical_line(colour)
    (0..6).each do |y|
      return true if has_straight_line(colour, y, false)
    end
    false
  end

  def has_diagonal_line(colour)
    (-3..3).each do |x|
      return true if has_diagonal(colour, x, 1)
    end
    (3..9).each do |x|
      return true if has_diagonal(colour, x, -1)
    end
    false
  end

  def has_straight_line(colour, a, horizonal)
    counter = 0
    (0..6).each do |b|
      value = horizonal ? grid_value(a, b) : grid_value(b, a)
      if value == colour
        counter += 1
      else
        counter = 0
      end
      return true if counter == 4
    end
    false
  end

  def has_diagonal(colour, x, direction)
    counter = 0
    (0..6).each do |y|
      if grid_value(x + (direction * y), y) == colour
        counter += 1
      else
        counter = 0
      end
      return true if counter == 4
    end
    false
  end

  def grid_value(x, y)
    return nil if x < 0 || x >= 7 || y < 0 || y >= 7
    stack = self.grid[y]
    stack.length > x ? stack[x] : nil
  end

  def apply_move(colour, column)
    self.moves << [colour, column]
    self.grid[column] << colour
  end

  def before_save
    self.moves = self.moves.to_json
    self.grid = self.grid.to_json
  end

  def after_save
    self.moves = JSON.parse(self.moves)
    self.grid = JSON.parse(self.grid)
  end

  def after_find
    self.status = self.status.to_sym
    self.moves = JSON.parse(self.moves)
    self.grid = JSON.parse(self.grid)
  end
end

{'fraction_non_alphanumeric': 0.10754640839386602, 'fraction_numerical': 0.011097659402744148, 'mean_word_length': 2.428077455048409, 'pattern_counts': {'":': 0, '<': 17, '<?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': '2311534', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1748, 'n_words': 455}