Class: CombinatorialPuzzleSolver::Identifier

Inherits:
Object
  • Object
show all
Defined in:
lib/combinatorial_puzzle_solver/identifier.rb

Overview

Designates the smallest unit that should be mapped to a number, e.g. a cell in a Sudoku puzzle.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Identifier) initialize(puzzle)

Creates an Identifier

Parameters:

  • puzzle (Puzzle)

    the puzzle this identifier belongs to.



18
19
20
21
22
# File 'lib/combinatorial_puzzle_solver/identifier.rb', line 18

def initialize(puzzle)
  @puzzle = puzzle;
  @value = nil
  @constraints = []
end

Instance Attribute Details

- (Array<Constraint>) constraints (readonly)

Returns the constraints that affect this identifier

Returns:

  • (Array<Constraint>)

    the constraints that affect this identifier

Specifications:

should return an Array of all dependent constraints



11
12
13
# File 'lib/combinatorial_puzzle_solver/identifier.rb', line 11

def constraints
  @constraints
end

- (Puzzle) puzzle (readonly)

Returns the puzzle that this identifier belongs to.

Returns:

  • (Puzzle)

    the puzzle that this identifier belongs to.



14
15
16
# File 'lib/combinatorial_puzzle_solver/identifier.rb', line 14

def puzzle
  @puzzle
end

- (Object?) value (readonly)

Returns the value that this identifier is assigned to, or nil.

Returns:

  • (Object, nil)

    the value that this identifier is assigned to, or nil.

Specifications:

should have value nil by default



8
9
10
# File 'lib/combinatorial_puzzle_solver/identifier.rb', line 8

def value
  @value
end

Instance Method Details

- (Array<Identifier>) dependent_identifiers

Returns all other identifiers that are covered by this identifier's constraints.

Returns:

  • (Array<Identifier>)

    all other identifiers that are covered by this identifier's constraints.

Specifications:

should give all other identifiers covered by its constraints



32
33
34
# File 'lib/combinatorial_puzzle_solver/identifier.rb', line 32

def dependent_identifiers
  @constraints.flatten.uniq - [self]
end

- (true, false) has_value?

Returns true if this identifier has a value, false otherwise.

Returns:

  • (true, false)

    true if this identifier has a value, false otherwise.

Specifications:

should return true if it has a value, false otherwise



37
38
39
# File 'lib/combinatorial_puzzle_solver/identifier.rb', line 37

def has_value?
  !@value.nil?
end

- (String) inspect

Returns a string representation of this identifier, which is defined by Puzzle#inspect_identifier.

Returns:

See Also:



51
52
53
# File 'lib/combinatorial_puzzle_solver/identifier.rb', line 51

def inspect
  @puzzle.inspect_identifier(self)
end

- (Object) set!(value)

Sets the value for this identifier

Parameters:

  • value (Object)

    the value to set.

Specifications:

should set the given value



26
27
28
# File 'lib/combinatorial_puzzle_solver/identifier.rb', line 26

def set!(value)
  @value = value
end

- (String) to_s

Returns a string representation of this identifier, which is defined by Puzzle#identifier_to_s.

Returns:

See Also:



44
45
46
# File 'lib/combinatorial_puzzle_solver/identifier.rb', line 44

def to_s
  @puzzle.identifier_to_s(self)
end