Class: TuringMachine::Instance
- Inherits:
-
Object
- Object
- TuringMachine::Instance
- Defined in:
- lib/turing_machine/instance.rb
Overview
Public: An instance of a Turing machine.
Instance Method Summary collapse
- #halted? ⇒ Boolean
-
#initialize(instructions, initial_state, tape = Tape.new) ⇒ Instance
constructor
A new instance of Instance.
- #proceed ⇒ Object
- #to_s ⇒ Object
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
25 26 27 |
# File 'lib/turing_machine/instance.rb', line 25 def halted? @state.current == 'HALT' end |
#proceed ⇒ Object
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_s ⇒ Object
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 |