斗地主算法(3)

前面写完牌型的判断下面写几个后面牌型比较要用的方法

牌型的排序(带花色)

function compareFunc(c1, c2)
  if CardUtils.getValue(c1) == CardUtils.getValue(c2) then
    return CardUtils.getSuit(c1) > CardUtils.getSuit(c2)
  end
  return CardUtils.getValue(c1) > CardUtils.getValue(c2)
end

--对牌进行排序 包括花色的排序
function sortPoker(cards)
  table.sort(cards, compareFunc)
end

--将table里面的元素倒过来
function ascendingTable(table)
  local tmp = {}
  for i = #table, 1, -1 do
    tmp[#tmp + 1] = table[i]
  end
  return tmp
end
function subTable(tab1, tab2) --两个table相减
  local tmpcards = copyTab(tab1)
  for k,v in pairs(tab2) do
      for i = 1, #tmpcards do
        if tmpcards[i] == tab2[k] then
          table.remove(tmpcards, i)
        end
      end
  end
  return tmpcards
end

function getCardsTabValue(cards)
  local tmp = {}
  for k, v in pairs(cards or {}) do
        if type(v) ~= "table" then
            tmp[k] = v % 100
        else
            tmp[k] = CardUtils.getCardsTabValue(v)
        end
    end
  return tmp 
end

-- 自定义复制table函数
function copyTab(st)
    local tab = {}
    for k, v in pairs(st or {}) do
        if type(v) ~= "table" then
            tab[k] = v
        else
            tab[k] = CardUtils.copyTab(v)
        end
    end
    return tab
end

下面是比牌的算法

比牌规则

只要将带牌的牌型单独比较 其他的都可以直接比较

3带1 3带2 4带2 4带2对 飞机带单 飞机带对子

-- 普通比较 比较单牌 对子 炸弹 顺子 飞机不带 3不带
-- true 表示第一个大 false 表示第二个大
function normalCompare(cards1, cards2)
  table.sort(cards1)
  table.sort(cards2)
  if getValue(cards1[#cards1]) > getValue(cards2[#cards2]) then
    return true
  else 
    return false
  end
 
end

--适合三带和飞机带  3带的比较  飞机带牌的比较  飞机中大牌的任何三张都会比小牌的大
-- true 表示第一个大 false 表示第二个大
function threeAndAircraftTakeCompare(cards1, cards2)
  sortPoker(cards1)
  sortPoker(cards2)
  local value1 = 0
  local value2 = 0

  local oneCards1, twoCards1, threeCards1, fourCards1, kingBomb1 = CardUtils.getAllType(cards1)
  local oneCards2, twoCards2, threeCards2, fourCards2, kingBomb2 = CardUtils.getAllType(cards2)
  value1 = getValue(threeCards1[1][1])
  value2 = getValue(threeCards2[1][1])

  if value1 > value2 then
    return true
  else 
    return false
  end
end

-- 四带的比较
-- true 表示第一个大 false 表示第二个大
function fourTakeCompare(cards1, cards2)
  sortPoker(cards1)
  sortPoker(cards2)
  local value1 = 0
  local value2 = 0
  local oneCards1, twoCards1, threeCards1, fourCards1, kingBomb1 = getAllType(cards1)
  local oneCards2, twoCards2, threeCards2, fourCards2, kingBomb2 = getAllType(cards2)
  value1 = getValue(fourCards1[1][1])
  value2 = getValue(fourCards2[1][1])

  if value1 > value2 then
    return true
  else 
    return false
  end
end

-- playcards,prePlayCards
function compareCards(cards1, cards2)
  -- print("牌型:--------"..CardUtils.getType(playCardsValue))
  if #cards2 == 0 and CardUtils.getType(cards1) ~= 0 then
    return true
  end
  if CardUtils.getType(cards1) == 0 then
    return false
  end
  if CardUtils.getType(cards1) == KINGBOMB_CARD then
    return true
  elseif CardUtils.getType(cards2) == KINGBOMB_CARD then
    return false
  end
  if CardUtils.getType(cards1) == CardUtils.getType(cards2) then
    local typecard = CardUtils.getType(cards1)
    if typecard == CardUtils.BOMB_TWO_CARD or typecard == CardUtils.BOMB_FOUR_CARD then
      if CardUtils.fourTakeCompare(cards1, cards2) then
          return true
      end
    elseif typecard == CardUtils.AIRCRAFT_WING or typecard == CardUtils.THREE_ONE_CARD or typecard == CardUtils.THREE_TWO_CARD then
      if CardUtils.threeAndAircraftTakeCompare(cards1, cards2) then
          return true
      end
    else 
      if CardUtils.normalCompare(cards1, cards2) then
          return true
      end
    end
  elseif CardUtils.getType(cards1) == CardUtils.BOMB_CARD and CardUtils.getType(cards2) ~= CardUtils.BOMB_CARD then
    return true
  end
    return false
end


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值