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,
image
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
		
		task.wait(1)
	until state == machine.endState
end

turing(fiveStateBB)

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

1 Like