Lua 斗地主算法实现

--服务器定义的牌值  16进
Cards =
{
	0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,	--方块 A - K
	0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,	--梅花 A - K
	0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,	--红桃 A - K
	0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,	--黑桃 A - K
	0x4E,0x4F,
}
--获取花色
function GetColor( serverIndex )
    -- body
    return math.ceil(serverIndex / 16)  --1-方块   2-梅花   3-红桃    4-黑桃
end
--获取数字
function GetNumber( serverIndex )
    -- body
    return serverIndex % 16             --大王 15  小王14
end
--获取卡牌优先级 15 ~ 1(王、2、1、K、Q、J、10 ~ 3)
function GetGrad( serverIndex )
    -- body
    local num = serverIndex % 16 
    if num == 1 then
        return 12;
    elseif num == 2 then
        return 13;
    elseif num > 2 and num < 14 then
        return num - 2;
    else
        return num
    end
end
function SortCards( cardsData )
    -- body
    --卡牌排序(先优先级,后花色)
    local cachePos = #cardsData - 1;
    while cachePos > 0 do
        local pos = 0;
        for i=1,cachePos do
            if this.GetGrad(cardsData[i])  < this.GetGrad(cardsData[i+1])  then  --判断优先级
                pos = i;
                local tmp = cardsData[i];       --缓存小值
                cardsData[i] = cardsData[i+1];
                cardsData[i+1] = tmp;
            end
            if this.GetGrad(cardsData[i]) == this.GetGrad(cardsData[i+1]) then   --判断花色
                if this.GetColor(cardsData[i]) < this.GetColor(cardsData[i+1]) then
                    pos = i;
                    local tmp = cardsData[i];
                    cardsData[i] = cardsData[i+1];
                    cardsData[i+1] = tmp;
                end
            end
            cachePos = pos;
        end
    end
    
    return cardsData;
end
--u3d 滑动选牌
function OnGUI()
	-- body
	if UnityEngine.Event.current.type == UnityEngine.EventType.MouseDown then
		this.touchFirst = UnityEngine.Event.current.mousePosition;                --记录第一次点击位置
	end
	if UnityEngine.Event.current.type == UnityEngine.EventType.MouseDrag then
        this.timer = this.timer + UnityEngine.Time.deltaTime;  --计时器  
        if this.timer > this.offsetTime then                   --间隔时间进入
            this.timer = 0;                                    --时间重置
            this.touchSecond = UnityEngine.Event.current.mousePosition;           --记录结束下的位置  
            local slideDirection = this.touchFirst - this.touchSecond;            --开始减去结束
            local x = slideDirection.x;
            local y = slideDirection.y;
            --处理范围内的牌,加入选牌列表
            this.chooseCard = {};                               --选中牌清空
            for i=1,#this.selfInfo.cardsInfo do
            	if this.selfInfo.cardsInfo[i].parent.gameObject.activeSelf == true then
            		local lossyScaleX = this.selfInfo.cardsInfo[i].image.transform.lossyScale.x;
            		local leftPiont = UnityEngine.RectTransformUtility.WorldToScreenPoint(nil, this.selfInfo.cardsInfo[i].image.transform.position).x - this.cardWidth * lossyScaleX / 2; 
            		local rightPoint = UnityEngine.RectTransformUtility.WorldToScreenPoint(nil, this.selfInfo.cardsInfo[i].image.transform.position).x + this.cardWidth * lossyScaleX / 2;
            		local lossyScaleY = this.selfInfo.cardsInfo[i].image.transform.lossyScale.y;
            		local topPoint = UnityEngine.Screen.height - UnityEngine.RectTransformUtility.WorldToScreenPoint(nil, this.selfInfo.cardsInfo[i].image.transform.position).y + this.cardHeight * lossyScaleX / 2; 
            		local bottomPoint = UnityEngine.Screen.height - UnityEngine.RectTransformUtility.WorldToScreenPoint(nil, this.selfInfo.cardsInfo[i].image.transform.position).y - this.cardHeight * lossyScaleX / 2; 
            		if x > 0 then --向左
            			if leftPiont > this.touchSecond.x and leftPiont < this.touchFirst.x then
            				if this.touchSecond.y > bottomPoint and this.touchSecond.y < topPoint then
                                this.selfInfo.cardsInfo[i].image.color = Color.gray;                    --选中牌置灰
                                this.chooseCard[#this.chooseCard +1] = i;                               --记录选中牌下标
            				end
            			end
            		else          --向右
            			if leftPiont < this.touchSecond.x and leftPiont > this.touchFirst.x then
            				if this.touchSecond.y > bottomPoint and this.touchSecond.y < topPoint then
								this.selfInfo.cardsInfo[i].image.color = Color.gray;
								this.chooseCard[#this.chooseCard +1] = i;                               --记录选中牌下标
            				end
            			end
            		end
            	end
            end
        end
    elseif UnityEngine.Event.current.type == UnityEngine.EventType.MouseUp then
        --选中的牌操作
        for i=1,#this.chooseCard do
        	if this.selfInfo.cardsInfo[this.chooseCard[i]].isUp then
        		this.selfInfo.cardsInfo[this.chooseCard[i]].image.transform.localPosition = Vector3(0, 0, 0);
        		this.selfInfo.cardsInfo[this.chooseCard[i]].isUp = false;
        	else
        		this.selfInfo.cardsInfo[this.chooseCard[i]].image.transform.localPosition = Vector3(0, 50, 0);
        		this.selfInfo.cardsInfo[this.chooseCard[i]].isUp = true;
        	end
        end
        for i=1,#this.selfInfo.cardsInfo do
        	this.selfInfo.cardsInfo[i].image.color = Color.white;
        end
        this.chooseCard = {};
    end
end



猜你喜欢

转载自blog.csdn.net/Momo_Da/article/details/80014314