Advertisement
hackobster

TuringMain

Jul 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.17 KB | None | 0 0
  1. function write()
  2.     turtle.digDown()
  3.     turtle.placeDown()
  4. end
  5.  
  6. function write1()
  7.     turtle.select(2)
  8.     write()
  9. end
  10.  
  11. function write0()
  12.     turtle.select(1)
  13.     write()
  14. end
  15.  
  16. -- Assumes direction is ether "L" or "R", does nothing for anything else
  17. function move(direction)
  18.     if (direction == "L") then
  19.         turtle.back()
  20.     else
  21.         if (direction == "R") then
  22.             turtle.forward()
  23.         end
  24.     end
  25. end
  26.  
  27. local turingMachine = {
  28.     -- state
  29.     [1]={
  30.         -- read symbol
  31.         [0]={
  32.             writeCommand=write1,
  33.             moveCommand="R",
  34.             gotoState=2
  35.         },
  36.         [1]={
  37.             writeCommand=write1,
  38.             moveCommand="L",
  39.             gotoState=2
  40.         }
  41.     },
  42.     [2]={
  43.         [0]={  
  44.             writeCommand=write1,
  45.             moveCommand="L",
  46.             gotoState=1
  47.         },
  48.         [1]={  
  49.             writeCommand=write0,
  50.             moveCommand="L",
  51.             gotoState=3
  52.         }        
  53.     },
  54.     [3]={
  55.         [0]={  
  56.             writeCommand=write1,
  57.             moveCommand="R",
  58.             gotoState=0 -- halt
  59.         },
  60.         [1]={  
  61.             writeCommand=write1,
  62.             moveCommand="L",
  63.             gotoState=4
  64.         }        
  65.     },
  66.     [4]={
  67.         [0]={  
  68.             writeCommand=write1,
  69.             moveCommand="R",
  70.             gotoState=4
  71.         },
  72.         [1]={  
  73.             writeCommand=write0,
  74.             moveCommand="R",
  75.             gotoState=1
  76.         }        
  77.     }
  78. }
  79.  
  80. local currentState = 1
  81. local stepCount = 0
  82. -- while we are not in the halt state
  83. while currentState ~= 0 do
  84.     local nextActions = nil
  85.     turtle.select(1)
  86.     -- assumes turtle has selected the iron slot
  87.     if turtle.compareDown() then
  88.         nextActions = turingMachine[currentState][0]
  89.     else
  90.         nextActions = turingMachine[currentState][1]
  91.     end
  92.     -- assumes nextActions ~= nil
  93.     nextActions.writeCommand()
  94.     move(nextActions.moveCommand)
  95.     currentState = nextActions.gotoState
  96.     stepCount = stepCount + 1
  97.     --print("now in state=" .. currentState)
  98. end
  99. print("Halted after " .. stepCount .. " steps")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement