<commit_before>require "oxlinegame/object/base"

module Oxlinegame
  module Object
    class Board
      include Base

      def initialize(window, n_rows)
        super(window, window.width / 2, window.height / 2)
        @cell_width = @window.width / n_rows
        @cell_height = @window.height / n_rows
        @cells = Array.new(n_rows) { Array.new(n_rows) { 0 } }
        signs = [" ", "o", "x"]
        (@window.options[:n_players] - 2).times do |i|
          signs << i + 3
        end
        @cell_images = signs.collect.with_index do |label, i|
          [
            i,
            Gosu::Image.from_text(@window, label, @font_path,
                                  @cell_height, 0, @cell_width, :center)
          ]
        end.to_h
        @n_rows = n_rows
      end

      def draw
        @cells.each_with_index do |columns, n_row|
          columns.each_with_index do |cell, n_column|
            @window.draw_rectangle_outline(@cell_width * n_column,
                                           @cell_height * n_row,
                                           @cell_width * (n_column + 1),
                                           @cell_height * (n_row + 1),
                                           Gosu::Color::GRAY,
                                           ZOrder::OBJECT)
            @cell_images[cell].draw(@cell_width * n_column,
                                    @cell_height * n_row,
                                    ZOrder::OBJECT)

          end
        end
      end

      def mark(turn, x, y)
        if @cells[y][x] == 0
          @cells[y][x] = turn
          true
        else
          false
        end
      end

      def markable?
        @cells.any? do |columns|
          columns.any? {|cell| cell == 0 }
        end
      end

      def lined?
        @n_rows.times do |i|
          if line_check(@cells[i])
            return true
          end
          if line_check(@cells.collect {|columns| columns[i] })
            return true
          end
        end
        if line_check(@n_rows.times.collect {|i| @cells[i][i] })
          return true
        end
        if line_check(@n_rows.times.collect {|i| @cells[i][@n_rows - i - 1] })
          return true
        end
        false
      end

      private
      def line_check(line)
        n_win_cells = @window.options[:n_win_cells]
        if n_win_cells
          continuities = line.join.scan(/(\w)\1{#{n_win_cells - 1},}/)
          continuities.reject! {|continuity| continuity[0] == "0" }
          not continuities.empty?
        else
          line.uniq.size == 1 and line[0] != 0
        end
      end
    end
  end
end
<commit_msg>Support all slant lines<commit_after>require "oxlinegame/object/base"

module Oxlinegame
  module Object
    class Board
      include Base

      def initialize(window, n_rows)
        super(window, window.width / 2, window.height / 2)
        @cell_width = @window.width / n_rows
        @cell_height = @window.height / n_rows
        @cells = Array.new(n_rows) { Array.new(n_rows) { 0 } }
        signs = [" ", "o", "x"]
        (@window.options[:n_players] - 2).times do |i|
          signs << i + 3
        end
        @cell_images = signs.collect.with_index do |label, i|
          [
            i,
            Gosu::Image.from_text(@window, label, @font_path,
                                  @cell_height, 0, @cell_width, :center)
          ]
        end.to_h
        @n_rows = n_rows
      end

      def draw
        @cells.each_with_index do |columns, n_row|
          columns.each_with_index do |cell, n_column|
            @window.draw_rectangle_outline(@cell_width * n_column,
                                           @cell_height * n_row,
                                           @cell_width * (n_column + 1),
                                           @cell_height * (n_row + 1),
                                           Gosu::Color::GRAY,
                                           ZOrder::OBJECT)
            @cell_images[cell].draw(@cell_width * n_column,
                                    @cell_height * n_row,
                                    ZOrder::OBJECT)

          end
        end
      end

      def mark(turn, x, y)
        if @cells[y][x] == 0
          @cells[y][x] = turn
          true
        else
          false
        end
      end

      def markable?
        @cells.any? do |columns|
          columns.any? {|cell| cell == 0 }
        end
      end

      def lined?
        column_lines = @cells.transpose
        slant_lines = @cells.collect.with_index do |l, i|
          line = l.dup
          i.times do
            line.push(line.shift)
          end
          line
        end.transpose
        r_slant_lines = @cells.collect.with_index do |l, i|
          line = l.dup
          i.times do
            line.unshift(line.pop)
          end
          line
        end.transpose
        @n_rows.times do |i|
          if line_check(@cells[i])
            return true
          end
          if line_check(column_lines[i])
            return true
          end
          if line_check(slant_lines[i])
            return true
          end
          if line_check(r_slant_lines[i])
            return true
          end
        end
        false
      end

      private
      def line_check(line)
        n_win_cells = @window.options[:n_win_cells]
        if n_win_cells
          continuities = line.join.scan(/(\w)\1{#{n_win_cells - 1},}/)
          continuities.reject! {|continuity| continuity[0] == "0" }
          not continuities.empty?
        else
          line.uniq.size == 1 and line[0] != 0
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.0983722576079264, 'fraction_numerical': 0.006192498230714791, 'mean_word_length': 1.227344365642238, 'pattern_counts': {'":': 0, '<': 7, '<?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': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '337694', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1636, 'n_words': 371}