在2D数组上实现类似于"Bejeweled"的逻辑。

我正在努力理解逻辑,但我想不到最好的做法。

我有一个2D数组array[10][6],其中10是列数,6是行数。我想检查数组中是否有3个或更多匹配项。有没有人知道一个好的例子或者有一个解决方案的想法?我看了一些在线资源,但我看到的所有资源都缺乏评论,难以阅读。

如果在一行中有3个或更多连续的项,或在一列中有3个以上项重叠,则这些项会通过匹配。

点赞
用户912446
用户912446

我不确定这是否是最好的方法,但这是我得以工作的方式。这是lua代码

function checkWinningsFunction()
    --检查x轴的匹配
    for i=1,6 do
        for n=1,8 do

            if(reelImages[n][i].value == reelImages[n+1][i].value and reelImages[n+1][i].value == reelImages[n+2][i].value) then
                --匹配
            end

        end
    end

    --检查y轴的匹配
    for i=1,4 do
        for n=1,10 do

            if(reelImages[n][i].value == reelImages[n][i+1].value and reelImages[n][i+1].value == reelImages[n][i+2].value) then
                --匹配
            end

        end
    end
end
2013-10-22 05:25:30
用户204011
用户204011

这与 @Dave 的想法相同,可能更容易理解( t 是你的二维数组):

for y=1,6 do -- 对每一行
  for x=1,10-2 do -- 对每个可能的水平三元组
    if t[x+1][y] == t[x][y] and t[x+2][y] == t[x][y] then
      -- 匹配
    end
  end
end

for x=1,10 do -- 对每一列
  for y=1,6-2 do -- 对每个可能的垂直三元组
    if t[x][y+1] == t[x][y] and t[x][y+2] == t[x][y] then
      -- 匹配
    end
  end
end

这可能不是最快的算法,但它是最简单的,根据我个人认为,任何其他潜在性能的提高都不值得增加复杂性。

编辑:我没有意识到 Dave 是原帖作者 :) 所以,那个方法是有效的。

2013-10-22 09:41:09