韩国美女连连看核心算法

精选的韩国美女,看着一个个沉鱼落雁,闭月羞花之貌的美人,着实令人心神俱醉,目不暇接。“媚眼含羞合,丹唇逐笑开”,“俊眉修眼,顾盼神飞”,如此一个小游戏,却让你赏尽天下美人,岂不快哉。


一饱眼福之余,也让人好奇这背后的逻辑。欲知背后的算法如何,且听我细细分析。


大神说,talk is cheap, show me the code.


这个世界是由代码构成的,一切基于code。

local LinkManager = {}

local LevelMap = require("src.app.data.LevelMap")
local itemsArray = nil
local gameScene = nil

function LinkManager:init(scene)
    gameScene = scene
    itemsArray = gameScene.itemsArray
end

function LinkManager:canLink(item1, item2)
    if item1:getID() ~= item2:getID() then
        return false
    end
    local r1, c1 = item1:getRow(), item1:getCol()
    local r2, c2 = item2:getRow(), item2:getCol()
    if self:canLinkWithNoCorner(r1, c1, r2, c2) then
        return true, {{r1, c1}, {r2, c2}}
    end
    local b, t = self:canLinkWith1Corner(r1, c1, r2, c2)
    if b then
        return true, {{r1, c1}, t, {r2, c2}}
    end
    b, t = self:canLinkWith2Corner(r1, c1, r2, c2)
    if b then
        return true, {{r1, c1}, t[1], t[2], {r2, c2}}
    end
    return false
end

function LinkManager:isWin()
    local sz = 0;
    for k, v in pairs(itemsArray) do
        sz = 1
    end
    return sz == 0
end

function LinkManager:canPass(row, col)
    if row < 0 or row > LevelMap.maxRow-1 then return true end
    if col < 0 or col > LevelMap.maxCol-1 then return true end
    return gameScene:getItem(row, col) == nil
end

function LinkManager:canLinkWithNoCorner(r1, c1, r2, c2)
    if r1 ~= r2 and c1 ~= c2 then return false end
    if r1 == r2 then
        local dc = c1 > c2 and -1 or 1
        local c = c1 + dc
        while (c ~= c2) do
            if not self:canPass(r1, c) then
                return false
            end
            c = c + dc
        end
    else
        local dr = r1 > r2 and -1 or 1
        local r = r1 + dr
        while r ~= r2 do
            if not self:canPass(r, c1) then
                return false
            end
            r = r + dr
        end
    end
    return true
end

function LinkManager:canLinkWith1Corner(r1, c1, r2, c2)
    local bpass = false
    if r1 == r2 or c1 == c2 then
        return false
    end
    if self:canPass(r1, c2) then
        bpass = self:canLinkWithNoCorner(r1, c1, r1, c2) and
            self:canLinkWithNoCorner(r1, c2, r2, c2)
    end
    if bpass then
        return true, {r1, c2}
    end
    if self:canPass(r2, c1) then
        bpass = self:canLinkWithNoCorner(r1, c1, r2, c1) and
            self:canLinkWithNoCorner(r2, c1, r2, c2)
    end
    if bpass then
        return true, {r2, c1}
    end
    return false
end

function LinkManager:canLinkWith2Corner(r1, c1, r2, c2)
    local tempr = r1
    while tempr ~= -2 do
        tempr = tempr - 1
        if self:canPass(tempr, c1) then
            local b, t = self:canLinkWith1Corner(tempr, c1, r2, c2)
            if b then
                return true, {{tempr, c1}, {t[1], t[2]}}
            end
        else
            break
        end
    end
    tempr = r1
    while tempr ~= LevelMap.maxRow+2 do
        tempr = tempr + 1
        if self:canPass(tempr, c1) then
            local b, t = self:canLinkWith1Corner(tempr, c1, r2, c2)
            if b then
                return true, {{tempr, c1}, {t[1], t[2]}}
            end
        else
            break
        end
    end
    local tempc = c1
    while tempc ~= -2 do
        tempc = tempc - 1
        if self:canPass(r1, tempc) then
            local b, t = self:canLinkWith1Corner(r1, tempc, r2, c2)
            if b then
                return true, {{r1, tempc}, {t[1], t[2]}}
            end
        else
            break
        end
    end
    tempc = c1
    while tempc ~= LevelMap.maxCol+2 do
        tempc = tempc + 1
        if self:canPass(r1, tempc) then
            local b, t = self:canLinkWith1Corner(r1, tempc, r2, c2)
            if b then
                return true, {{r1, tempc}, {t[1], t[2]}}
            end
        else
            break
        end
    end
    return false
end

return LinkManager


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值