Sha256: 8f2e3de8ed4b1972d0419ba3d2c05d24191aa16e5ce11bb3eda48b4d932904b7

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require File.join('games', 'tictactoe', 'squares')
require File.join('games', 'tictactoe', 'square')

module TTT
  class SquaresFactory
    def self.build_custom_squares(config, squares_class = Squares)

      rows_and_cols = config.length

      #create multi-dimensional array
      squares = Array.new(rows_and_cols) do
        Array.new(rows_and_cols)
      end

      squares.each_with_index do |element, row|
        element.each_index do |col|
          squares[row][col] = create_custom_square(config[row][col][0],config[row][col][1])
        end
      end

      squares_class.new(collection_of_squares: squares)

    end


    def self.create_custom_square(display_value, value, square_class = Square)
      square_class.new(display_value: display_value,
                       value: value)
    end

    def self.build_empty_squares(rows_and_cols, squares_class = Squares)
      squares = Array.new(rows_and_cols) do
        Array.new(rows_and_cols)
      end

      i = 1
      squares.each_with_index do |element, row|
        element.each_index do |col|
          squares[row][col] = create_empty_square(i)
          i += 1
        end
      end

      squares_class.new(collection_of_squares: squares)
    end

    def self.create_empty_square(display_value, square_class = Square)
      square_class.new(display_value: display_value,
                       value: nil)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
games_bfox-0.6.0 lib/games/tictactoe/squares_factory.rb