Class: Sangi::StepBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/sangi/step_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#boardObject (readonly)

Returns the value of attribute board.



3
4
5
# File 'lib/sangi/step_builder.rb', line 3

def board
  @board
end

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/sangi/step_builder.rb', line 3

def config
  @config
end

#expressionObject (readonly)

Returns the value of attribute expression.



3
4
5
# File 'lib/sangi/step_builder.rb', line 3

def expression
  @expression
end

#result_valueObject (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_factoryObject (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

#stepsObject (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: brief_message || "#{row_label(row)}#{place_name(place)}#{rod_name(kind)}棒を1本置きます。",
    learn_message: 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: brief_message,
    learn_message: learn_message || brief_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

Raises:



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: brief_message || "#{row_label(row)}#{place_name(place)}から#{rod_name(kind)}棒を1本取り除きます。",
    learn_message: 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