I am working on a text based RPG called The Forbidden Caves.
I will post my progress here.

Here is the nonfunctional code (the interpereter is broken):

Code:


local c_stage = 0
local c_branch = 0

function scrClear ()
 io.write("\27[2J")
end

function rpg (stage,branch)
 if stage == 0 then print "You are in a small abandoned village. There is a tavern to the WEST, a cave to the NORTH and a   sign to the EAST." end
 if stage == 1 and branch == 1 then print "You enter the cave and walk until it gets too dark too see. You stumble on until you just can't go any further. You turn back." end
 if stage == 1 and branch == 2 then print [[The sign reads: "Welcome to the town of Pleasantville! The happy town of..." The population has been scratched out. Someone has carved into the sign: "run, while you can still can! remember, it can always see you..."]] end
 interp()
end

function interp ()
 c_input = io.read()
 if not string.find(c_input, "GO") == nil then
  arg = string.sub(c_input, 3)
  if arg == "NORTH" then c_branch = 1 end
  if arg == "EAST" then c_branch = 2 end
  if arg == "SOUTH" then c_branch = 3 end
  if arg == "WEST" then c_branch = 4 end
 end
 c_stage = 1
 rpg ()
end

scrClear()
print([[_________          _______ ]])                                                     
print([[\__   __/|\     /|(  ____ \]])                                                     
print([[   ) (   | )   ( || (    \/]])                                                     
print([[   | |   | (___) || (__    ]])                                                     
print([[   | |   |  ___  ||  __)   ]])                                                     
print([[   | |   | (   ) || (      ]])                                                     
print([[   | |   | )   ( || (____/\]])                                                     
print([[   )_(   |/     \|(_______/]])                                                     
print()                                                                                 
print([[ _______  _______  _______  ______  _________ ______   ______   _______  _       ]])
print([[(  ____ \(  ___  )(  ____ )(  ___ \ \__   __/(  __  \ (  __  \ (  ____ \( (    /|]])
print([[| (    \/| (   ) || (    )|| (   ) )   ) (   | (  \  )| (  \  )| (    \/|  \  ( |]])
print([[| (__    | |   | || (____)|| (__/ /    | |   | |   ) || |   ) || (__    |   \ | |]])
print([[|  __)   | |   | ||     __)|  __ (     | |   | |   | || |   | ||  __)   | (\ \) |]])
print([[| (      | |   | || (\ (   | (  \ \    | |   | |   ) || |   ) || (      | | \   |]])
print([[| )      | (___) || ) \ \__| )___) )___) (___| (__/  )| (__/  )| (____/\| )  \  |]])
print([[|/       (_______)|/   \__/|/ \___/ \_______/(______/ (______/ (_______/|/    )_)]])
print()                                                                                 
print([[ _______  _______           _______  _______ ]])                                   
print([[(  ____ \(  ___  )|\     /|(  ____ \(  ____ \]])                                   
print([[| (    \/| (   ) || )   ( || (    \/| (    \/]])                                   
print([[| |      | (___) || |   | || (__    | (_____ ]])                                   
print([[| |      |  ___  |( (   ) )|  __)   (_____  )]])                                   
print([[| |      | (   ) | \ \_/ / | (            ) |]])                                   
print([[| (____/\| )   ( |  \   /  | (____/\/\____) |]])                                   
print([[(_______/|/     \|   \_/   (_______/\_______)]])   
io.read()
exiting = false
repeat
 scrClear()
 save = io.open("SAV.E", "RW")
 print("TFC MENU")
 print("-------------")
 print("NEW GAME")
 if not save == null then print("LOAD GAME") else print("NO SAVED GAME") end
 print("DELETE SAVE")
 print("QUIT")
 print("-------------")
 io.write("OPT: ")
 choice = io.read()

 if choice == "NEW" then
  c_stage = 0
  c_branch = 0
  clear()
  rpg(c_stage,c_branch)
 end

 if choice == "QUIT" then exiting = true end
until exiting == true
Well, I think you're getting caught up in a 0-indexing/1-indexing confusion. In Lua, strings are indexed from 1, not 0, so consider the command "GO NORTH":
Code:
GO NORTH
12345678
You want to take the substring from 4, not 3. Smile
Oh. That's interesting...
pcb_master wrote:
Oh. That's interesting...
I only found it by Googling "lua string sub" and reading through a paragraph of the first result. Wink Yup, Lua indexes everything from 1 instead of 0, just like Matlab.

Edit: You have another potential problem as well, I think:

Code:
if not string.find(c_input, "GO") == nil then
That will apply the not and then the ==, which may not work in all cases. You may want this instead:
Code:
if string.find(c_input,"GO") ~= nil then
Or even faster:
Code:
if string.sub(c_input,1,2) == "GO" then
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement