sys.path.append("../") from tmachine.VirtualHardware import VirtualHardware tape = [None,1,1,None,1,1,1,1,1,1,1,None] turing_machine = VirtualHardware(tape_length=50,init = tape) running = True state = 0 while running == True: print "state=",state print "tape position=",turing_machine.position() read_val = turing_machine.read() print "read_val=",read_val #print turing_machine.position() if state == 0 : if read_val == None: turing_machine.write(None) turing_machine.moveLeft() elif read_val == 1: turing_machine.write("1") turing_machine.moveLeft() state = 1 elif state == 1: if read_val == None: turing_machine.write(None) turing_machine.moveLeft() state = 2 elif read_val == 1: turing_machine.write(1) turing_machine.moveLeft() elif state == 2 : if read_val == None:
if state in statedict: read_function = statedict[state] else: print "ERROR: Found an undefind state (%s)" %state #Look up function from read value if read_val in read_function: state_function = read_function[read_val] else: print "ERROR: Got a read value with no definition. (%s)"%read_val break #print read_val write_val = state_function["write_value"] turing_machine.write(write_val) move_dir = state_function["move"] if move_dir == "R": turing_machine.moveLeft() else: turing_machine.moveRight() state = state_function["next_state"] # print read_val # print state_function # print state # # print turing_machine.position() turing_machine.tape()
# defines what a turing machine is from tmachine.VirtualHardware import VirtualHardware #creates virtual turing machine, including how long the tape of machine is turing_machine = VirtualHardware(tape_length=100) # asks for input state = raw_input("enter # ") #says input given will always be a number state = int(state) #converts integer given to binary number state = bin(state) #from second on, to remove 0,b from binary tape state = state[2:] #as long as there are still more digits in binary number... while state !="": print state # write binary number turing_machine.moveLeft()#move virtual tape left once(default is once, can be specified) turing_machine.write(state[0])#write first digit of binary code state = state[1:]#redueces state to what ever's left in binary code # show turing tape w turing_machine.tape()
""" This is an example using our turing machine class """ # Import our machine from tmachine.VirtualHardware import VirtualHardware # Initialize our turing machine turing_machine = VirtualHardware() # Write 10 1's to the tape for x in range(10): turing_machine.write(1) turing_machine.moveLeft() # print the tape for sanity tape = turing_machine.tape()