5

我正在尝试将两个长度相等的表与一个函数进行比较,因为我不知道有任何其他方法可以做到这一点。但是,使用以下功能,它无法注册,我不知道为什么。我希望有人可以提供对此问题的见解,或者有更好的方法来比较这两个表。

这些表正在填充以下代码:

str = "parameters determined by program (all digits)"
tableone = {}
for word in str:gmatch("%d") do table.insert(tableone,word) end

这两个表是相同的,当然,除了各个表的名称。表格已正确填充,并在我打印时正确显示。为了这个问题,这里有两张表:

tableone = {}
tabletwo = {}
for i=1,4 do table.insert(tableone, i) end
for i=1,4 do table.insert(tabletwo, i) end

显然,这两个表将彼此相等。我写的比较索引表的函数如下:

function comparetables(t1, t2)
matchct = 0
 for i=1,#t1 do
    if t1[i] == t2[i] then
    matchct = matchct + 1
    end
if matchct == #t1 then
return true
end
end

我试着做

print(comparetables(tableone,tabletwo))

看看它是否会打印“true”但没有运气。对我来说,它似乎应该没有问题。然而事实并非如此。我错过了什么?我已经尝试过搜索类似 table.compare 函数的东西,有人可能已经写过,但找不到这样的运气。感谢您的任何建议!

附加信息:

我比较表格的原因是主控类型的游戏。这意味着在比较表格时必须适用以下三个规则。我创建的功能只是让我开始,以为我可以从那里开始工作。

  1. 比较表格时,如果数字匹配,则 Ccount 增加 1。
  2. 比较表时,如果值存在于不同的索引位置,则将 Pcount 加 1

例如,对于值 {1, 3, 3, 4} 和猜测值 {4, 4, 3, 1} 的表,它将返回 Pcount 2(一个 4 和 1)和一个 Ccount 1 (第三位的三个)。我认为最难的部分之一是通过比较来识别猜测中的第二个 4 根本不应该增加 Pcount。

4

2 回答 2

5

应该工作的代码的一个轻微变体是:

function comparetables(t1, t2)
  if #t1 ~= #t2 then return false end
  for i=1,#t1 do
    if t1[i] ~= t2[i] then return false end
  end
  return true
end

但是我使用更像这样的东西:它检查参数的类型、它们的元表和其他一些情况。

-- This is not clever enough to find matching table keys
-- i.e. this will return false
--   recursive_compare( { [{}]:1 }, { [{}]:1 } )
-- but this is unusual enough for me not to care ;)
-- It can also get stuck in infinite loops if you use it on 
-- an evil table like this:
--     t = {}
--     t[1] = t

function recursive_compare(t1,t2)
  -- Use usual comparison first.
  if t1==t2 then return true end
  -- We only support non-default behavior for tables
  if (type(t1)~="table") then return false end
  -- They better have the same metatables
  local mt1 = getmetatable(t1)
  local mt2 = getmetatable(t2)
  if( not recursive_compare(mt1,mt2) ) then return false end

  -- Check each key-value pair
  -- We have to do this both ways in case we miss some.
  -- TODO: Could probably be smarter and not check those we've 
  -- already checked though!
  for k1,v1 in pairs(t1) do
    local v2 = t2[k1]
    if( not recursive_compare(v1,v2) ) then return false end
  end
  for k2,v2 in pairs(t2) do
    local v1 = t1[k2]
    if( not recursive_compare(v1,v2) ) then return false end
  end

  return true  
end

这是它的一个使用示例:

print( recursive_compare( {1,2,3,{1,2,1}}, {1,2,3,{1,2,1}} ) ) -- prints true
print( recursive_compare( {1,2,3,{1,2,1}}, {2,2,3,{1,2,3}} ) ) -- prints false
于 2012-01-04T06:26:21.990 回答
3

如果您在面向对象的意义上比较比表格更客观的对象,那么我会考虑以 lua OO 方式实现这些功能。

这样的事情应该可以解决问题:

GameState = {}
GameState.mt = {}
GameState.mt.fns = {}
GameState.mt.__index =  GameState.mt.fns

function GameState.new(a,b,c,d)
-- TODO: put argument checks here...
  local retval = {}
  retval[1] = a
  retval[2] = b
  retval[3] = c
  retval[4] = d
  setmetatable(retval, GameState.mt)
  return retval
end

function GameState.mt.fns.print( self )
  print(" GameState: ", self[1], self[2], self[3], self[4] )
end

function GameState.mt.__tostring( self )
  return "GameState: "..self[1].." "..self[2].." "..self[3].." "..self[4]
end

function GameState.mt.__eq(self, other)
  -- Check it's actually a GameState, and all its bits match
  return getmetatable(other)==GameState.mt and
    (self[1] == other[1]) and 
    (self[2] == other[2]) and 
    (self[3] == other[3]) and 
    (self[4] == other[4])
end

然后你会像这样使用它:

state1 = GameState.new(1,2,3,4)
state2 = GameState.new(1,2,3,4)

print("State 1 is:")
state1:print()

print("State 2 is:")
print(state2)

print( "state1 == state2 : ", state1 == state2 )

print( "Changing state 2") 
state2[1]=2

print( "state1 == state2 : ", state1 == state2 )
于 2012-01-04T06:54:25.923 回答