Class: Sangi::StepBuilder
- Inherits:
-
Object
- Object
- Sangi::StepBuilder
- Defined in:
- lib/sangi/step_builder.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#result_value ⇒ Object
readonly
Returns the value of attribute result_value.
-
#rod_factory ⇒ Object
readonly
Returns the value of attribute rod_factory.
-
#steps ⇒ Object
readonly
Returns the value of attribute steps.
Instance Method Summary collapse
-
#initialize(config:, expression:, board:, result_value:, rod_factory:) ⇒ StepBuilder
constructor
A new instance of StepBuilder.
- #place_name(place) ⇒ Object
- #place_rod(row:, place:, kind:, title: "棒を置く", brief_message: nil, learn_message: nil, event_type: :place_rod) ⇒ Object
- #push(title:, brief_message:, event:, learn_message: nil) ⇒ Object
- #remove_rod(row:, place:, kind:, title: "棒を取り除く", brief_message: nil, learn_message: nil, event_type: :remove_rod) ⇒ Object
- #rod_name(kind) ⇒ Object
- #rod_value_name(kind) ⇒ Object
- #row_label(row) ⇒ Object
Constructor Details
#initialize(config:, expression:, board:, result_value:, rod_factory:) ⇒ StepBuilder
Returns a new instance of StepBuilder.
5 6 7 8 9 10 11 12 |
# File 'lib/sangi/step_builder.rb', line 5 def initialize(config:, expression:, board:, result_value:, rod_factory:) @config = config @expression = expression @board = board @result_value = result_value @rod_factory = rod_factory @steps = [] end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
3 4 5 |
# File 'lib/sangi/step_builder.rb', line 3 def board @board end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/sangi/step_builder.rb', line 3 def config @config end |
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
3 4 5 |
# File 'lib/sangi/step_builder.rb', line 3 def expression @expression end |
#result_value ⇒ Object (readonly)
Returns the value of attribute result_value.
3 4 5 |
# File 'lib/sangi/step_builder.rb', line 3 def result_value @result_value end |
#rod_factory ⇒ Object (readonly)
Returns the value of attribute rod_factory.
3 4 5 |
# File 'lib/sangi/step_builder.rb', line 3 def rod_factory @rod_factory end |
#steps ⇒ Object (readonly)
Returns the value of attribute steps.
3 4 5 |
# File 'lib/sangi/step_builder.rb', line 3 def steps @steps end |
Instance Method Details
#place_name(place) ⇒ Object
64 65 66 67 |
# File 'lib/sangi/step_builder.rb', line 64 def place_name(place) names = %w[一の位 十の位 百の位 千の位 万の位 十万の位 百万の位 千万の位 一億の位 十億の位] names.fetch(place, "10^#{place}の位") end |
#place_rod(row:, place:, kind:, title: "棒を置く", brief_message: nil, learn_message: nil, event_type: :place_rod) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sangi/step_builder.rb', line 26 def place_rod(row:, place:, kind:, title: "棒を置く", brief_message: nil, learn_message: nil, event_type: :place_rod) cell = work_cell(row, place) cell.add_rod(rod_factory.build(kind)) board.highlight = { row: row, place_index: place } push( title: title, brief_message: || "#{row_label(row)}の#{place_name(place)}に#{rod_name(kind)}棒を1本置きます。", learn_message: || "#{place_name(place)}に#{rod_value_name(kind)}を表す棒を1本追加します。", event: Event.new(type: event_type, row: row, place_index: place, kind: kind) ) end |
#push(title:, brief_message:, event:, learn_message: nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sangi/step_builder.rb', line 14 def push(title:, brief_message:, event:, learn_message: nil) steps << Step.new( index: steps.size + 1, title: title, brief_message: , learn_message: || , event: event, board: board.clone_deep, numeric_state: numeric_state ) end |
#remove_rod(row:, place:, kind:, title: "棒を取り除く", brief_message: nil, learn_message: nil, event_type: :remove_rod) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sangi/step_builder.rb', line 38 def remove_rod(row:, place:, kind:, title: "棒を取り除く", brief_message: nil, learn_message: nil, event_type: :remove_rod) cell = work_cell(row, place) removed = cell.remove_rod(kind) raise InternalError, "#{row_label(row)}の#{place_name(place)}に#{rod_name(kind)}棒がありません。" if removed.nil? board.highlight = { row: row, place_index: place } push( title: title, brief_message: || "#{row_label(row)}の#{place_name(place)}から#{rod_name(kind)}棒を1本取り除きます。", learn_message: || "#{place_name(place)}から#{rod_value_name(kind)}を表す棒を1本取り除きます。", event: Event.new(type: event_type, row: row, place_index: place, kind: kind) ) end |
#rod_name(kind) ⇒ Object
56 57 58 |
# File 'lib/sangi/step_builder.rb', line 56 def rod_name(kind) kind == :five ? "five" : "unit" end |
#rod_value_name(kind) ⇒ Object
60 61 62 |
# File 'lib/sangi/step_builder.rb', line 60 def rod_value_name(kind) kind == :five ? "5" : "1" end |
#row_label(row) ⇒ Object
52 53 54 |
# File 'lib/sangi/step_builder.rb', line 52 def row_label(row) row == :work ? "Work" : row.to_s.upcase end |