Lua “卢阿”;或;声明问题

Lua “卢阿”;或;声明问题,lua,Lua,我对Lua很陌生,我正在做一个非常简单的基于文本的冒险,但它不起作用。我的代码如下: while input ~= ("leave cave" or "leave") do print("What do you want to do?") input = io.read() if input == "inspect" then print("You are in a cave") elseif input == "leave cave" or "leave" the

我对Lua很陌生,我正在做一个非常简单的基于文本的冒险,但它不起作用。我的代码如下:

while input ~= ("leave cave" or "leave") do
print("What do you want to do?")
input = io.read()

if input == "inspect" then 
        print("You are in a cave") 
    elseif input == "leave cave" or "leave" then
        print("You leave the cave")
    elseif input == "inv" then
        for i,v in pairs(inv) do
        print(i, v)
    end
  else
    print("You didn't write a valid command...")
  end
end

-- leave cave

input = ""
print("What do you want to do?")
input = io.read()
while input ~= "follow path" do 
if input == "inspect" then 
        print("You are at the base of a hill. There is a path.") 
    elseif input ==  "follow path" then
        print("You follow the path. There is a gate.") 
     elseif input == "inv" then
        for i,v in pairs(inv) do
        print(v)
        end
    else 
        print("That's not a valid command...")
    end 
end
我试图做到的是,每当用户键入“离开”或“离开洞穴”时,它都会进入下一段(路径1),然而,当我键入“离开”,然后再次键入“检查”时,它会说“我在洞穴中”,而不是它应该说的话,也就是说你离开了,然后你会看到一条路径。当我输入“离开洞穴”,然后检查时,它会一遍又一遍地发出“你在山脚下,有一条小路”,无限期地


当我输入“inv”时,它不会打印我的库存,而是打印“你离开了洞穴”,但实际上并没有离开。

a或b
不能生成表示“a或b”的值——这太复杂了

事实上,如果您让它在两个字符串中进行选择,它只会选择第一个:

print("leave cave" or "leave") --> leave cave
仅用于布尔型——您必须在多个完整条件下组合它:

while(输入~=“离开洞穴”)和(输入~=“离开”)do

在这种情况下,
重复。。。。。。。直到
循环可能更好地为您服务:

repeat
    print("What do you want to do?")
    input = io.read()

    -- <do stuff>
until input == "leave" or input == "leave cave"
重复
打印(“您想做什么?”)
输入=io.read()
-- 
直到输入==“离开”或输入==“离开洞穴”

虽然
无法完成如此复杂的操作,但可以使用一些粗糙的元表代码自己重新创建效果

请注意,我不建议在任何严肃的专业或商业程序中使用此代码,或者根本不建议在这方面使用此代码,因为此代码效率低下且不必要,但是它是一段有趣的代码,可以完全按照您的要求执行。这只是一个有趣的方式来试验Lua的力量

local iseither
iseither = setmetatable({},{
   __sub = function(arg1,arg2)
      if arg2 == iseither then
         arg2.Value = arg1
         return arg2
      else
         if type(arg2) ~= "table" then
            error("Second operator is -iseither- was not a table",2)
         else
            for i,v in ipairs(arg2) do
               if arg1.Value == v then
                  arg1.Value = nil
                  return true
               end
            end
            arg1.Value = nil
            return false
         end
      end
   end
})

print(1 -iseither- {1,2,3,4,5})

这是可行的,但这对不是布尔型的东西有效吗?如本代码所示
如果输入==“打开闸门”或“检查”,则打印(“闸门已锁定。您发现一把钥匙伸出草地。”)否则如果输入==“inv”或“库存”或“showinv”,则对于i,v成对(inv)进行打印(i,v)结束else打印(“这不是有效命令…”)结束
我最好输入==“打开门”和输入==“检查”,然后……你总是需要明确地重复你的
==
或任何你想说的话。如果您经常需要这样做,可以编写一个
matchesAny
函数,允许您编写
If matchesAny(输入,“打开门”,“检查”)