Class: CombinatorialPuzzleSolver::Possibilities
 Inherits:

Array
 Object
 Array
 CombinatorialPuzzleSolver::Possibilities
 Defined in:
 lib/combinatorial_puzzle_solver/possibilities.rb
Overview
A collection of the possible values an identifier can have. It is reduced by invoking #cannot_be! and #must_be! until it only has one possible value, and thus becomes #resolved?.
Specifications
is an array
Instance Method Summary (collapse)

 (Hash<Identifier,Object>) cannot_be!(value, resolvable = Hash.new)
Reduce the possible values by stating that the identifier cannot have a certain value.

 (Hash<Identifier,Object>) dependent_identifiers_cannot_be!(value, resolvable = Hash.new)
Notifies dependent identifiers that they can't have a certain value.

 (Possibilities) initialize(solution_space, identifier)
constructor
Creates a set of possibilities for a specific identifier, which initially would be all possible symbols of the puzzle.

 (Hash<Identifier,Object>) must_be!(value, resolvable = Hash.new)
Reduce the possible values by stating that the identifier can only have a certain value.

 (true, false) resolved?
True if the identifier can only have one possible value, false otherwise.
Constructor Details
 (Possibilities) initialize(solution_space, identifier)
Creates a set of possibilities for a specific identifier, which initially would be all possible symbols of the puzzle.
15 16 17 18 19 20 
# File 'lib/combinatorial_puzzle_solver/possibilities.rb', line 15 def initialize(solution_space, identifier) @solution_space = solution_space @identifier = identifier super(identifier.puzzle.symbols) unless identifier.has_value? end 
Instance Method Details
 (Hash<Identifier,Object>) cannot_be!(value, resolvable = Hash.new)
Reduce the possible values by stating that the identifier cannot have a certain value.
71 72 73 74 75 76 77 78 79 
# File 'lib/combinatorial_puzzle_solver/possibilities.rb', line 71 def cannot_be!(value, resolvable=Hash.new) raise Inconsistency if @identifier.value == value if delete(value) then raise Inconsistency if empty? resolvable[@identifier] = first if resolved? end resolvable end 
 (Hash<Identifier,Object>) dependent_identifiers_cannot_be!(value, resolvable = Hash.new)
Notifies dependent identifiers that they can't have a certain value.
53 54 55 56 57 58 
# File 'lib/combinatorial_puzzle_solver/possibilities.rb', line 53 def dependent_identifiers_cannot_be!(value, resolvable=Hash.new) @identifier.dependent_identifiers.each{dependency @solution_space[dependency].cannot_be!(value, resolvable) } resolvable end 
 (Hash<Identifier,Object>) must_be!(value, resolvable = Hash.new)
Reduce the possible values by stating that the identifier can only have a certain value.
33 34 35 36 37 38 39 40 41 
# File 'lib/combinatorial_puzzle_solver/possibilities.rb', line 33 def must_be!(value, resolvable=Hash.new) raise Inconsistency if @identifier.has_value? && @identifier.value != value raise Inconsistency unless include?(value) clear push(value) dependent_identifiers_cannot_be!(value, resolvable) end 
 (true, false) resolved?
Returns true if the identifier can only have one possible value, false otherwise.
83 84 85 
# File 'lib/combinatorial_puzzle_solver/possibilities.rb', line 83 def resolved? size == 1 end 