Sha256: bbc6103fb5413b70024846a8f1fe12b6c6a5120f93d66c84ddb5015bcd1e8a1f

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

require 'puzzle_generator/misc'
require 'puzzle_generator/chained_map'
require 'puzzle_generator/colored_map'

require 'ludy/array/rotate'

module PuzzleGenerator

  class Puzzle
    include DisplayMap
    attr_reader :tried_times, :tried_duration
    # create a puzzle with option. this won't start generating,
    # call Puzzle::generate to start generating.
    def initialize option = {}
      @option = DefaultOption.merge option
      @tried_times, @tried_duration = [0, 0], [0, 0]
    end
    # start generate a puzzle with options that created this puzzle instance.
    def generate
      raw_colors = (1..@option[:colors]).to_a
      step_colors = raw_colors.rotate

      make_chain
      make_color raw_colors
      until @result_color.result_map.kind_of? Array
        if step_colors == raw_colors
          make_chain
          make_color raw_colors
          step_colors = raw_colors.rotate
        else
          make_color step_colors
          step_colors.rotate!
        end
      end

      @result_map = @result_color.result_map
    end

    private
    Chain, Color = 0, 1 # :nodoc:
    def make_chain
      begin
        @result_chain =
          PuzzleGenerator::generate(
            @option[:timeout] - @tried_duration[Chain]){ ChainedMap.new @option }
      ensure
        update_info Chain, LastTriedInfo
      end
    end
    def make_color colors
      start = Time.now
      begin
        @result_color = ColoredMap.new @result_chain, colors
      ensure
        update_info Color, {:tried_times => 1, :tried_duration => Time.now - start}
      end
    end
    def update_info index, info
      @tried_times[index]    += info[:tried_times]
      @tried_duration[index] += info[:tried_duration]
    end
  end

end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
godfat-ludy-0.1.13 lib/puzzle_generator/puzzle.rb
ludy-0.1.10 lib/puzzle_generator/puzzle.rb
ludy-0.1.11 lib/puzzle_generator/puzzle.rb
ludy-0.1.13 lib/puzzle_generator/puzzle.rb