Weird Morse Code system thing

So uh… I was messing around… and this thought just came to me: What if I could make a morse code system INSIDE Roblox…?

Apparently, I went so far that I created a reverse text Morse code system. Try it out for yourself if you want (FYI, using this enough might make you a Backwards Morse Code Master, or even do something like this in your game for a puzzle)

local input = ""

-- tod(dot) C(glue) hsad(dash) Ecaps(Space)

local function isMorse(text)
	return not text:find("[^%.%- /]")  -- only . - spaces and slashes allowed
end

local function isReversed(text)
	return text:find("%a") and text == string.reverse(string.reverse(text))
end

local morse = {
	["A"] = ".-", ["B"] = "-...", ["C"] = "-.-.",
	["D"] = "-..", ["E"] = ".", ["F"] = "..-.",
	["G"] = "--.", ["H"] = "....", ["I"] = "..",
	["J"] = ".---", ["K"] = "-.-", ["L"] = ".-..",
	["M"] = "--", ["N"] = "-.", ["O"] = "---",
	["P"] = ".--.", ["Q"] = "--.-", ["R"] = ".-.",
	["S"] = "...", ["T"] = "-", ["U"] = "..-",
	["V"] = "...-", ["W"] = ".--", ["X"] = "-..-",
	["Y"] = "-.--", ["Z"] = "--..",
	["1"] = ".----", ["2"] = "..---", ["3"] = "...--",
	["4"] = "....-", ["5"] = ".....", ["6"] = "-....",
	["7"] = "--...", ["8"] = "---..", ["9"] = "----.",
	["0"] = "-----"
}

local wordToMorse = {
	dot = ".",
	dash = "-",
	space = "/",
	Ecaps = "/",
	C = ""
}

local function decodeBackwardsWordMorse(input)
	local letters = {}
	local currentLetter = ""

	for w in input:gmatch("%S+") do
		local word = string.reverse(w):lower()
		local symbol = wordToMorse[word] or "?"

		if symbol == "/" then
			if currentLetter ~= "" then
				table.insert(letters, currentLetter)
				currentLetter = ""
			end
			-- mark a word break
			table.insert(letters, "/")
		elseif symbol == "" then
			-- C: do nothing, keep building currentLetter
		else
			currentLetter = currentLetter .. symbol
		end
	end

	-- Add last letter if not empty
	if currentLetter ~= "" then
		table.insert(letters, currentLetter)
	end

	-- Reverse order if needed
	local reversedLetters = {}
	for i = #letters, 1, -1 do
		table.insert(reversedLetters, letters[i])
	end

	return table.concat(reversedLetters, " ")  -- letters separated by space
end

-- Decode a single Morse letter
local function decodeLetter(code)
	for letter, morseCode in pairs(morse) do
		if morseCode == code then
			return letter
		end
	end
	return "?"
end

function reverseWords(text)
	local words = {}
	for w in text:gmatch("%S+") do
		table.insert(words, 1, w) -- insert at front instead of end
	end
	return table.concat(words, " ")
end

-- Decode Morse input with spaces for letters and slashes for words
local function decodeMorse(input)
	local result = {}
	local i = 1
	while i <= #input do
		local matched = false
		local longestMatch = ""
		local matchedLetter = ""

		-- Find the longest Morse code that fits starting at i
		for letter, code in pairs(morse) do
			if input:sub(i, i + #code - 1) == code then
				if #code > #longestMatch then
					longestMatch = code
					matchedLetter = letter
				end
			end
		end

		if matchedLetter ~= "" then
			table.insert(result, matchedLetter)
			i = i + #longestMatch
		else
			local c = input:sub(i, i)
			if c == "/" then
				table.insert(result, " ")
			end
			i = i + 1
		end
	end
	
	local sentence = table.concat(result)
	return sentence
end

local function decodeReversed(input)
	return string.reverse(input)
end

local function autoDecode(input)
	-- First, check if it looks like a backwards word-Morse input
	if input:find("%a") and (input:find("tod") or input:find("hsad") or input:find("Ecaps") or input:find("C")) then
		print("Detected backwards word-Morse input!")
		local morseInput = decodeBackwardsWordMorse(input)
		local decoded = decodeMorse(morseInput)
		return string.reverse(decoded)  -- reverse the final result
	elseif isMorse(input) then
		print("Detected Morse code!")
		return decodeMorse(input)
	else
		print("Detected reversed text!")
		return decodeReversed(input)
	end
end

print(autoDecode(input))

So a bit of the way this kinda works… is that if you wanna write something in text morse code… you have to write backwards to. Here’s an example

If you do “tod tod tod tod C tod C tod hsad tod tod C tod hsad tod tod C hsad hsad hsad Ecaps hsad C tod tod tod tod C tod C tod hsad tod C tod”, you get “OLLEH EREHT” in the output.

But if you write backwards to front, you get “hsad tod tod C tod hsad tod tod C tod hsad tod C hsad hsad hsad C tod hsad hsad Ecaps hsad hsad hsad C tod hsad tod tod C tod hsad tod tod C tod C tod tod tod tod”, which translates to “HELLO WORLD” in the output.

(EDIT), Unfortunately there was a problem with DecodeBackwardsWordMorse, So replace it with the fixed code below.

local function decodeBackwardsWordMorse(input)
	local out, buf = {}, {}

	for w in input:gmatch("%S+") do
		local token = wordToMorse[string.reverse(w):lower()] or "?"

		if token == "/" then
			-- finish current letter if in progress, then add word break
			if #buf > 0 then
				table.insert(out, table.concat(buf))
				buf = {}
			end
			table.insert(out, "/")
		elseif token == "" then
			-- glue between dot/dash of the same letter; do nothing
		else
			table.insert(buf, token) -- '.' or '-'
		end
	end

	-- flush the last letter
	if #buf > 0 then
		table.insert(out, table.concat(buf))
	end

	-- return a compact Morse stream; spaces are unnecessary
	return table.concat(out)
end
1 Like