class TuringMachine
	def initialize(initial_state, final_states, transitions)
		@initial_state = initial_state
		@final_states = final_states
		@transitions = transitions
	end
	
	def run(string)
		state = @initial_state
		tape = string
		head = 0
		
		puts '<tt>'
		
		while true do
			puts '[' + state + '] ' + (head == 0 ? '' : tape[0..head - 1]) + '<u>' + tape[head..head] + '</u>' + tape[head + 1..-1] + '<br/>';
			
			if @final_states.include?(state)
				puts 'Halt.'
				break
			end
			
			state, write, move = @transitions[[state, tape[head..head]]]
			
			if state == nil
				puts 'Crash.'
				break
			end
			
			tape[head] = write
			
			if move == :R
				if head == tape.length - 1
					tape += '^'
				end
				head += 1
			elsif move == :L
				if head == 0
					tape = '^' + tape
				else
					head -= 1
				end
			end
		end
		
		puts '</tt>'
	end
end
