Class: Xudoku::Result

Inherits:
Struct
  • Object
show all
Defined in:
lib/xudoku.rb

Overview

Holds a matched puzzle and solution pair returned by the generator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#puzzleBoard

Returns the unsolved board with clues set.

Returns:

  • (Board)

    the unsolved board with clues set



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/xudoku.rb', line 45

Result = Struct.new(:puzzle, :solution, keyword_init: true) do
  # Returns a human-readable representation of the puzzle and solution.
  #
  # @return [String] a labelled two-section string
  #
  # @example
  #   puts result.to_s
  #   # PUZZLE:
  #   # ... board output ...
  #   # SOLUTION:
  #   # ... board output ...
  def to_s
    output = "PUZZLE:\n"
    output << puzzle.to_s
    output << "\n"
    output << "SOLUTION:\n"
    output << solution.to_s
  end

  # Serialises the result to a JSON object with +puzzle+ and +solution+ keys.
  #
  # The +*_args+ splat is accepted for compatibility with <tt>JSON.generate</tt>
  # and <tt>to_json</tt> conventions but is otherwise unused.
  #
  # @return [String] a JSON string of the form <tt>{"puzzle":...,"solution":...}</tt>
  def to_json(*_args)
    { puzzle: puzzle, solution: solution }.to_json
  end
end

#solutionBoard

Returns the fully solved board corresponding to the puzzle.

Returns:

  • (Board)

    the fully solved board corresponding to the puzzle



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/xudoku.rb', line 45

Result = Struct.new(:puzzle, :solution, keyword_init: true) do
  # Returns a human-readable representation of the puzzle and solution.
  #
  # @return [String] a labelled two-section string
  #
  # @example
  #   puts result.to_s
  #   # PUZZLE:
  #   # ... board output ...
  #   # SOLUTION:
  #   # ... board output ...
  def to_s
    output = "PUZZLE:\n"
    output << puzzle.to_s
    output << "\n"
    output << "SOLUTION:\n"
    output << solution.to_s
  end

  # Serialises the result to a JSON object with +puzzle+ and +solution+ keys.
  #
  # The +*_args+ splat is accepted for compatibility with <tt>JSON.generate</tt>
  # and <tt>to_json</tt> conventions but is otherwise unused.
  #
  # @return [String] a JSON string of the form <tt>{"puzzle":...,"solution":...}</tt>
  def to_json(*_args)
    { puzzle: puzzle, solution: solution }.to_json
  end
end

Instance Method Details

#to_json(*_args) ⇒ String

Serialises the result to a JSON object with puzzle and solution keys.

The *_args splat is accepted for compatibility with JSON.generate and to_json conventions but is otherwise unused.

Returns:

  • (String)

    a JSON string of the form {"puzzle":...,"solution":...}



70
71
72
# File 'lib/xudoku.rb', line 70

def to_json(*_args)
  { puzzle: puzzle, solution: solution }.to_json
end

#to_sString

Returns a human-readable representation of the puzzle and solution.

Examples:

puts result.to_s
# PUZZLE:
# ... board output ...
# SOLUTION:
# ... board output ...

Returns:

  • (String)

    a labelled two-section string



56
57
58
59
60
61
62
# File 'lib/xudoku.rb', line 56

def to_s
  output = "PUZZLE:\n"
  output << puzzle.to_s
  output << "\n"
  output << "SOLUTION:\n"
  output << solution.to_s
end