Class: Sudoku::SolutionSet

Inherits:
Object
  • Object
show all
Defined in:
lib/sudoku/solution_set.rb,
lib/sudoku/solution_set/choice.rb,
lib/sudoku/solution_set/process.rb,
lib/sudoku/solution_set/process/step.rb,
lib/sudoku/solution_set/process/out_of_history.rb,
lib/sudoku/solution_set/constraints/cell_must_be_filled.rb,
lib/sudoku/solution_set/constraints/box_must_contain_value.rb,
lib/sudoku/solution_set/constraints/row_must_contain_value.rb,
lib/sudoku/solution_set/constraints/column_must_contain_value.rb,
lib/sudoku/solution_set/process/steps/delete_redundant_choices.rb,
lib/sudoku/solution_set/process/steps/pick_unsatisfied_constraint.rb,
lib/sudoku/solution_set/process/steps/move_choice_into_included_set.rb,
lib/sudoku/solution_set/process/steps/pick_choice_satisfying_constraint.rb

Defined Under Namespace

Modules: Constraints Classes: Choice, Process

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(puzzle) ⇒ SolutionSet

Returns a new instance of SolutionSet.



10
11
12
13
14
15
16
17
18
# File 'lib/sudoku/solution_set.rb', line 10

def initialize(puzzle)
  @puzzle = Grid.new(puzzle)
  @process = Process.new(self)

  initialize_constraints
  initialize_excluded_choices
  initialize_included_choices
  delete_redundant_choices
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



8
9
10
# File 'lib/sudoku/solution_set.rb', line 8

def constraints
  @constraints
end

#included_choicesObject (readonly)

Returns the value of attribute included_choices.



8
9
10
# File 'lib/sudoku/solution_set.rb', line 8

def included_choices
  @included_choices
end

Instance Method Details

#delete_excluded_choices_ifObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sudoku/solution_set.rb', line 48

def delete_excluded_choices_if
  deleted_choices = []

  @excluded_choices.delete_if do |excluded_choice|
    if yield excluded_choice
      deleted_choices << excluded_choice
      true
    end
  end

  deleted_choices
end

#eachObject



20
21
22
23
24
25
26
27
28
# File 'lib/sudoku/solution_set.rb', line 20

def each
  loop do
    if @process.run!
      yield to_grid
    else
      return
    end
  end
end

#exclude_choice(choice) ⇒ Object



42
43
44
45
46
# File 'lib/sudoku/solution_set.rb', line 42

def exclude_choice(choice)
  @included_choices.delete(choice)
  @excluded_choices << choice
  choice
end

#excluded_choicesObject



30
31
32
33
34
# File 'lib/sudoku/solution_set.rb', line 30

def excluded_choices
  @excluded_choices.sort_by! { |choice|
    [choice.column, choice.row, choice.value]
  }
end

#include_choice(choice) ⇒ Object



36
37
38
39
40
# File 'lib/sudoku/solution_set.rb', line 36

def include_choice(choice)
  @excluded_choices.delete(choice)
  @included_choices << choice
  choice
end

#restore_excluded_choices(choices) ⇒ Object



61
62
63
64
65
# File 'lib/sudoku/solution_set.rb', line 61

def restore_excluded_choices(choices)
  choices.each do |choice|
    @excluded_choices << choice
  end
end

#to_gridObject



67
68
69
70
71
72
73
74
75
# File 'lib/sudoku/solution_set.rb', line 67

def to_grid
  grid = Grid.new

  @included_choices.each do |choice|
    grid[choice.column, choice.row] = choice.value
  end

  grid
end