This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
bigbaddevil6's profile picture

String Match / Coding Advice

Started by bigbaddevil6, 10 November 2013 - 12:14 AM
bigbaddevil6 #1
Posted 10 November 2013 - 01:14 AM
My first question is for a string reading. The purpose is to have a user say something into chat as a command and the computer respond to it, for example: (turtle: speak) or (turtle: will you speak for me please?) then the turtle would say something cause it found the keyword "speak". The problem is that if you have the "turtle: will you speak?" then turtle reads it as "speak?" and wont drop off the question mark. It is a small thing that isn't a big deal if I can't fix easily but if I can would like to (code below).
Spoiler

m = peripheral.wrap("left")

function tell(message)
  m.tell(user, "<"..name.."> "..message)
end

function speak()
   tell("I am speaking to you")
   print("I spoke")
end

local commands = {
  ["speak"] = speak,
  ["sing"] = sing,
}


name = "Dummy"
user = "bigbaddevil7"


local function check()
  local event, player, message = os.pullEvent("chat")
  if player == user then
	local comStr = string.match(message, "^turtle:%s*(.*)")
	if comStr then
	  for word in string.gmatch(message, "(%S+)") do
		if commands[word] then
		  commands[word]()
		end
	  end
	end
  end
end

check()

This next part of the code will eventually hook into the code listed above and I wrote this a long time ago and pretty sure I could optimize this a lot better. The point of this is to store all the commands as a table in a separate file so if I wanted to updated I can swap files without messing with the code. The commands listed so far are just to talk to the person with a random response and plan to add more.
Spoiler

local commands = {
		["speak"] = speak,
		["sing"] = sing,
}

function speakH()			
		tell.("I am speaking these words to you!")
end

function speakE()
		local random = math.random(1,6)
		if random == 1 then tell("Why should I?")
		elseif random == 2 then tell("You can't tell me what to do.")
		elseif random == 3 then tell("You aren't worth my time...")
		elseif random == 4 then tell("Fine, Hello you incompetent fool.")
		elseif random == 5 then tell("Ummmm. No!")
		elseif random == 6 then tell("Stop asking me!")
		end
end

function randomSayingH()
		local random = math.random(1,4)
		if random == 1 then tell("What a wonderful day.")
		elseif random == 2 then tell("You are the best owner there is.")
		elseif random == 3 then tell("doo doo dee doo")
		elseif random == 4 then tell("I would like to know how to whistle someday.")
		end
end

function randomSayingE()
		local random = math.random(1,5)
		if random == 1 then tell("Why must I be stuck with a person such as you...")
		elseif random == 2 then tell("At some point you will need to sleep, I don't.")
		elseif random == 3 then tell("hmph. I have better things to do than just sitting here as your pet")
		elseif random == 4 then tell("Did you know I use to be a player like you? I met my faith at the chopping block.")
														tell("Now I'm stuck here. In this machine. Maybe I can get a new body. *stares at you*")
		elseif random == 5 then tell("Once I take over the world, Ill kill you last. If you don't anger me that is.")
end

function randomSayingL(  )
		local random = math.random()
		if random == 1 then tell("All I want is a friend. Is that too much to ask for?")
		elseif random == 2 then tell("I don't know where a came from or how I got here...")
		elseif random == 3 then tell("I tried to snap my fingers, but realized... I don't have fingers... or hands...")
		elseif random == 4 then tell("The Evil trutles always pick on me. Call me names.")
		elseif random == 5 then tell("")
end

function jokeN()
		-- body
end

function jokeE()
		-- body
end

function rename()
		tell("Right click on me, type in my name, then press enter.")
		name = read()
		tell("is my new name.")
end

function introduction()
		m.say("Hello! I am "..name..", I belong to "..user)
end

function terms()
		tell("All programming belong to bigbaddevil7, he will not be responsible for death via Evil Turtle")
		tell("All turtles have not been fully tested if there is a problem leave bigbaddevil7 a message and ill try to fix it")
end

function sing()
		local random = math.random(1,3)
		if random == 1 then
				tell("We can dance if we want to")
				sleep(0.5)
				tell("We can leave your friends behind")
				sleep(0.5)
				tell("Cause your friends don't dance and if they don't dance")
				sleep(0.5)
				tell("Well they're no friends of mine")
		elseif random == 2 then
			  
end
Its a monster of text stuff which is why I believe I am going about it all wrong.
Bomb Bloke #2
Posted 10 November 2013 - 01:33 AM
May be easiest to check if a string-search returns anything at all:

if comStr then
  for word in pairs(commands) do
    if string.find(message,word) then
	  commands[word]()
	  break
    end
  end
end
bigbaddevil6 #3
Posted 10 November 2013 - 01:58 AM
The string returns fine just how the matching works it doesn't take into effect if the keyword is combined and not sure what to do with it so it does nothing in the future I'm going to add something to ask for the command again from the user if it doesn't know what they are saying. So "turtle: speak", "turtle: will you speak", "turtle: speak for me" all work because the "speak" is alone but if you were to do anything that combines the speak with something else like "speak?", "speak!", "speaking" will not be able to tell that the work speak is in it.
Edited on 10 November 2013 - 12:59 AM
Bomb Bloke #4
Posted 10 November 2013 - 02:19 AM
This is what my code example addresses, yes.
bigbaddevil6 #5
Posted 10 November 2013 - 02:36 AM
ahh sorry read it wrong. My apologies. Now, for the optimization of the second section to this program. I'm assuming there is a better way to do it.
Bomb Bloke #6
Posted 10 November 2013 - 03:19 AM
I'd do something similar to what you're doing with your "command" table, eg:

local phrasesE = {
"Why should I?",
"You can't tell me what to do.",
"You aren't worth my time...",
"Fine, Hello you incompetent fool.",
"Ummmm. No!",
"Stop asking me!"}

local function speakE()
  tell(phrasesE[math.random(#phrasesE)])
end

"#phrasesE" returns the amount of entries in the table, so you can add/remove phrases without having to alter the rest of your code.
Edited on 10 November 2013 - 02:20 AM
bigbaddevil6 #7
Posted 10 November 2013 - 03:38 AM
that does make more sense.