斗地主算法(3)

来源:互联网 发布:山西公共频道网络直播 编辑:程序博客网 时间:2024/05/04 01:57

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

牌型的排序(带花色)

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 tmpend
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 tmpcardsendfunction 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 tabend

下面是比牌的算法

比牌规则

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

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  endend-- 四带的比较-- 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  endend-- playcards,prePlayCardsfunction 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 falseend

0 0
原创粉丝点击