# Need help identifying problem

``````local fiveStateBB = {
initState = "A",
endState = "H",
blank = "0",
rules = {
{"A", "0", "1", "right", "B"},
{"A", "1", "1", "left", "C"},

{"B", "0", "1", "right", "C"},
{"B", "1", "1", "right", "B"},

{"C", "0", "1", "right", "D"},
{"C", "1", "0", "left", "E"},

{"D", "0", "1", "left", "A"},
{"D", "1", "1", "left", "D"},

{"E", "0", "1", "stay", "H"},
{"E", "1", "0", "left", "A"}
}
}

local function turing(machine)
local state, tapeNum, step = machine.initState, 1, 0

repeat
for _, rule in ipairs(machine.rules) do
if rule[1] == state and rule[2] == tapeNum then
tapeNum = rule[3]
state = rule[5]
print(tapeNum..state)
break
end
end

step = step + 1
print("Step: "..step.." | "..tapeNum.." | State: "..state)
if state == machine.endState then
print("Halted.")
end
wait(1)
until state == machine.endState
end

turing(fiveStateBB)
``````

This script keeps outputting the same thing over and over,

when it really shouldnâ€™t.

After a couple prints I realized the line `if rule[1] == state and rule[2] == tapeNum then` always returns false for some reason. I just wanna know why that is. Been scratching my head at it for a good 20 minutes so any answer would be awesome.

Sorry for the vague title, just didnâ€™t know what to call this lol

1 Like

That line is returning false because rule[2] is 0, while tapeNum is 1.

â€¦No?

Itâ€™s looping through every child in â€śrulesâ€ť. Eventually itâ€™ll reach the table where rule[2] IS 1.

1 Like

Ohh didnâ€™t catch that, sorry. Itâ€™s hard to see anything else wrong because itâ€™s very confusing with all the variables lol.

rule[2] is a string and tapeNum is a number, try `tonumber(rule[2]) == tapeNum`

Tried that, same result.

aaaaa30charac

tested the code in studio and I found the solution

code
``````local fiveStateBB = {
initState = "A",
endState = "H",
blank = "0",
rules = {
{"A", "0", "1", "right", "B"},
{"A", "1", "1", "left", "C"},

{"B", "0", "1", "right", "C"},
{"B", "1", "1", "right", "B"},

{"C", "0", "1", "right", "D"},
{"C", "1", "0", "left", "E"},

{"D", "0", "1", "left", "A"},
{"D", "1", "1", "left", "D"},

{"E", "0", "1", "stay", "H"},
{"E", "1", "0", "left", "A"}
}
}

local function turing(machine)
local state, tapeNum, step = machine.initState, "1", 0

repeat
for _, rule in ipairs(machine.rules) do
if rule[1] == state and rule[2] == tapeNum then
tapeNum = rule[3]
state = rule[5]
print(tapeNum..state)

break
end
end

step += 1
print("Step: "..step.." | "..tapeNum.." | State: "..state)

if state == machine.endState then
print("Halted.")
end

only change I made was changing `local state, tapeNum, step = machine.initState, 1, 0` to `local state, tapeNum, step = machine.initState, "1", 0`
this just turns `tapeNum` to a string at the start, the reason `tonumber()` didnâ€™t work is because we change `tapeNum` later on inside the for loop as seen here `tapeNum = rule[3]`
`rule[3]` is already a string