Class: TuringMachine::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/turing_machine/instance.rb

Overview

Public: An instance of a Turing machine.

Instance Method Summary collapse

Constructor Details

#initialize(instructions, initial_state, tape = Tape.new) ⇒ Instance

Returns a new instance of Instance.



6
7
8
9
10
11
# File 'lib/turing_machine/instance.rb', line 6

def initialize(instructions, initial_state, tape = Tape.new)
  @instructions = Instructions.new(instructions)
  @state = StateRegister.new(initial_state)
  @tape = tape
  @sequence = 1
end

Instance Method Details

#halted?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/turing_machine/instance.rb', line 25

def halted?
  @state.current == 'HALT'
end

#proceedObject



18
19
20
21
22
23
# File 'lib/turing_machine/instance.rb', line 18

def proceed
  current = action
  update_sequence
  update_tape(current)
  update_state(current)
end

#to_sObject



13
14
15
16
# File 'lib/turing_machine/instance.rb', line 13

def to_s
  "#{'%3d' % @sequence} #{@tape} #{@state}#{instr_to_s}\n    " +
  ' ' * @tape.index + '^'
end