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

Need help coding advanced AI!

Started by Kron, 18 January 2013 - 10:03 AM
Kron #1
Posted 18 January 2013 - 11:03 AM
Hi guys, as you can probably see I'm a noob to the CC forums. However, I know code between beginner and advanced. I'm creating a special code for my underground bunker. It reads as so.


I am the AI of the Cron facillity. How can I help you?
[ENTER] [REPORT] [SUGGEST]


I want to make enter open a door (redstone circut at the back), which I can probably do my self.

However, with report and suggest, I want it to where I can type whatever, and it will say something along the lines as "Your report has been submitted to Cron personell.", or "Your suggestion has been submitted to Cron personell." here is all the code I have so far.

os.oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
while true do
term.clear()
term.setCursorPos(1,1)
print("Hello. I am the Cron[I] AI. How can I help you?")
print(" ")
print("[ENTER] [REPORT] [SUGGEST] ; Caps Lock Required")
input = io.read()
if input == "ENTER" then
  print("Opening...")
  print("Have a good day!")
  redstone.setOutput("back", true)
  sleep(3)
  redstone.setOutput("back", false)
  term.clear()
  term.setCursorPos(1,1)
  os.reboot()

I just need help finishing the code. Help would be nice!
theoriginalbit #2
Posted 18 January 2013 - 11:06 AM
ok firstly don't use os reboot, if you want to run the same code lots just surround the whole think in an infinite while loop
while true do

secondly just extend your if statement, so instead of

if input == "ENTER" then
make it

if input == "ENTER" then
  -- code
elseif input == "REPORT" then
  -- code
elseif input == "SUGGEST" then
  -- code
else
  print( "Uknown command" )
end
Kron #3
Posted 18 January 2013 - 11:12 AM
Thank you very much for the help. I'll try the code and see how it works. :)/>
Kron #4
Posted 18 January 2013 - 11:20 AM
ok firstly don't use os reboot, if you want to run the same code lots just surround the whole think in an infinite while loop
while true do

secondly just extend your if statement, so instead of

if input == "ENTER" then
make it

if input == "ENTER" then
  -- code
elseif input == "REPORT" then
  -- code
elseif input == "SUGGEST" then
  -- code
else
  print( "Uknown command" )
end

Okay, I get "bios:206: [string "startup"]:19: 'end' expected (to close 'if' at line 7)
mibac138 #5
Posted 18 January 2013 - 11:27 AM
Maybe you not ended the while true do ? ;)/>
theoriginalbit #6
Posted 18 January 2013 - 11:28 AM
make sure that all the elseif's don't have a space its 'elseif' not 'else if' … if you have done that make sure that you have an 'end' at the end of the if/ifelse statements…. make sure you have 'end'ed the while loop… short of that I'm gunna have to see the code…
Kron #7
Posted 18 January 2013 - 11:28 AM
Maybe you not ended the while true do ? ;)/>

make sure that all the elseif's don't have a space its 'elseif' not 'else if' … if you have done that make sure that you have an 'end' at the end of the if/ifelse statements…. short of that I'm gunna have to see the code…


Nah, I tried ending it and even took out os.pull/while true/ etc etc… Also tried elseif and else if… and I'll just make a more simple version that just allows "Enter", xD.

Thanks for trying though guys!
TheOddByte #8
Posted 18 January 2013 - 11:32 AM
Yeah I think you need two ends,
One for the "while true do" and the other for "if"
Know if you use "elseif" then it still counts as one if you need too add an end on!

Here Is An Example How The Code Could Look:
Spoiler


os.oldPull = os.pullEvent
os.pullevent = os.pullEventRaw
while true do
term.clear()
term.setCursorPos(1,1)
print("Hello. I am the Cron[I] Al, How can I help You?")
print("")
print("[ENTER] [REPORT] [SUGGEST] ; Caps lock Required")
input = io.read()
if input == "ENTER" then
print("Opening...")
print("Have A good day")
redstone.setOutput("back" , true)
sleep(3)
redstone.setOutput("back" , false)
term.clear()
term.setCursorPos(1,1)
	end
		end

theoriginalbit #9
Posted 18 January 2013 - 11:38 AM
Thanks for trying though guys!
Not a try… its the answer… post your code…
ChunLing #10
Posted 18 January 2013 - 11:58 AM
If you have more than 4 different responses you want to different inputs, then it's probably worth your while to make a table of the possible responses, like so:
os.oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
local task = {ENTER = function()
    print("Opening...")
    print("Have a good day!")
    redstone.setOutput("back", true)
    sleep(3)
    redstone.setOutput("back", false)
 end,
REPORT = function()
-- put report submitting stuff here
    print("Your report has been submitted to Cron[I] personell.")
end,
SUGGEST = function()
-- put suggestion submitting stuff here
    print("Your suggestion has been submitted to Cron[I] personell.")
end,
}
while true do
    term.clear()
    term.setCursorPos(1,1)
    print("Hello. I am the Cron[I] AI. How can I help you?")
    print(" ")
    print("[ENTER] [REPORT] [SUGGEST] ; Caps Lock Required")
    input = io.read()
    if task[input] then task[input]() end
end
Kron #11
Posted 19 January 2013 - 08:07 AM
If you have more than 4 different responses you want to different inputs, then it's probably worth your while to make a table of the possible responses, like so:

-snip-

I coppied all that you have, and I keep getting:

bios:206: [string "startup"]:16: unexpected symbol

os.oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
local task = {ENTER = function()
	  print("Opening...")
	  print("Have a good day!")
	  redstone.setOutput("left", true)	 ---	 Moved my door
	  sleep(3)
	  redstone.setOutput("left", false)
end,
REPORT = function()
   write("Report: ")
   if input = "1337pwned" then
	print("Error!")
   else
	print("Report Submitted.")
end,
SUGGEST = function()
   write("Suggest: ")
   if input == "1337pwnd" then
	print("Error!")
   else
	print("Suggestion Submitted.")
end,
}
while true do
	   term.clear()
	   term.setCursorPos(1,1)
	   print("Hello. I am the Cron[I] Artifical Intelligence. How can I help you?")
	   print(" ")
	   print("[ENTER] [REPORT] [SUGGEST] ; Caps Lock Required")
	   input = io.read(0
	   if task[input] then task[input] () end
end
Doyle3694 #12
Posted 19 January 2013 - 08:37 AM
both REPORT and SUGGEST have unended if statements.
TheOddByte #13
Posted 19 January 2013 - 09:49 AM
Yeah Like This:
Spoiler

function makePizza()
write("What would You Want?")
pizza = read()
if pizza == "Pizza" or pizza == "pizza" then
print("Ain't nobody got time for that!")
end --One End For The Function
 end-- One End For The If
ChunLing #14
Posted 19 January 2013 - 03:14 PM
To clarify why this results in an unexpected symbol error, Lua knows (forgive the anthropomorphism) that the comma is there to mark the end of the table entry…but the function to be contained as the value for that entry hasn't finished (that's actually putting it sorta backwards, but it's easier to explain that way).