Class: PuzzleGenerator::Puzzle

Inherits:
Object
  • Object
show all
Includes:
DisplayMap
Defined in:
lib/puzzle_generator/puzzle.rb

Instance Attribute Summary collapse

Attributes included from DisplayMap

#result_map

Instance Method Summary collapse

Methods included from DisplayMap

#display_map

Constructor Details

#initialize(option = {}) ⇒ Puzzle

create a puzzle with option. this won’t start generating, call Puzzle::generate to start generating.



15
16
17
18
# File 'lib/puzzle_generator/puzzle.rb', line 15

def initialize option = {}
  @option = DefaultOption.merge option
  @tried_times, @tried_duration = [0, 0], [0, 0]
end

Instance Attribute Details

#tried_durationObject (readonly)

Returns the value of attribute tried_duration.



12
13
14
# File 'lib/puzzle_generator/puzzle.rb', line 12

def tried_duration
  @tried_duration
end

#tried_timesObject (readonly)

Returns the value of attribute tried_times.



12
13
14
# File 'lib/puzzle_generator/puzzle.rb', line 12

def tried_times
  @tried_times
end

Instance Method Details

#generateObject

start generate a puzzle with options that created this puzzle instance.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puzzle_generator/puzzle.rb', line 20

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