Class: Sangi::CopyEngine

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CopyEngine

Returns a new instance of CopyEngine.



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

def initialize(config)
  @config = config
end

Instance Method Details

#build(display_a:, display_b:, result:, expression:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sangi/engines/copy_engine.rb', line 7

def build(display_a:, display_b:, result:, expression:)
  rod_factory = RodFactory.new
  place_count = [
    digit_count(display_a.abs),
    digit_count(display_b.abs),
    digit_count(result.abs),
    1
  ].max
  board = Board.from_numbers(
    a: display_a,
    b: display_b,
    work: 0,
    place_count: place_count,
    rod_factory: rod_factory
  )
  board.row(:work).sign = result <=> 0
  builder = StepBuilder.new(
    config: @config,
    expression: expression,
    board: board,
    result_value: result,
    rod_factory: rod_factory
  )

  builder.push(
    title: "準備",
    brief_message: "A行とB行を表示し、Work行を空にします。",
    learn_message: "0を含む計算なので、必要な棒だけをWork行へ写して結果を確認します。",
    event: Event.new(type: :setup)
  )
  copy_result_to_work(builder, result.abs)
  board.row(:work).normalize_zero_sign!
  builder.push(
    title: "完了",
    brief_message: "計算結果は#{result}です。",
    learn_message: "Work行の棒が表す値が計算結果です。結果は#{result}です。",
    event: Event.new(type: :finish)
  )
  builder.steps
end