连连看地图生成算法

元素个数:n行*m列;元素种类有c种

数组 touchObject[n][m];  //连连看元素数组

数组 objectTypeBeenUsedCount[c] //连连看元素类型使用统计

 

方式1:末尾补全法

随机生成数组touchObject里前m*n-c个元素,对应的objectTypeBeenUsedCount[c]++

遍历objectTypeBeenUsedCount,找出其中为奇数的元素类型,补全在touchObject最后c个空余位置

补全touchObject空余的元素,为了简单,用同种元素补全,并打乱

 

方式2: 根据奇数*2 =偶数的原则

1:随机生成touchObject一半的元素,将其拷贝到另外一半(用于确保元素都为偶数)

2:遍历数组,打乱位置

 

下面给出方式1的lua实现

 

function generateRow(rowAmount,ColumnAmount,TypeAmount)

  math.randomseed(os.time())

  local touchObjectArray={}
  local typeUsedAmountArray = {}
  local totalAmount = rowAmount*ColumnAmount-TypeAmount
  local currentItemAmount=0
  local swapTempValue=0

  for i=1,rowAmount do
     for j=1,ColumnAmount do
        local currentType = math.random(TypeAmount)

        currentItemAmount = currentItemAmount+1
        touchObjectArray[currentItemAmount]= currentType

        local currentTypeUsedAmount = typeUsedAmountArray[currentType] or 0
        currentTypeUsedAmount = currentTypeUsedAmount+1
        typeUsedAmountArray[currentType] = currentTypeUsedAmount

        if currentItemAmount>totalAmount then
           break
        end
     end
  end

  for i,v in ipairs(typeUsedAmountArray) do

     if v %2 >0 then
       currentItemAmount = currentItemAmount+1
       touchObjectArray[currentItemAmount] = i
     end
  end

  local leftAmount = totalAmount+TypeAmount-currentItemAmount

  if leftAmount>0 then
     local paddingType = math.random(TypeAmount)

      for i=1, leftAmount do
       --remix the padding value
       local indexToSwap = math.random(currentItemAmount)
       swapTempValue =touchObjectArray[indexToSwap]
       touchObjectArray[indexToSwap]=paddingType

       currentItemAmount = currentItemAmount+1
       touchObjectArray[currentItemAmount]=swapTempValue
      end
  end

  return touchObjectArray
end


local lianLianKanMapArray =generateRow(4,5,4)

for i=1,#lianLianKanMapArray do
    print(lianLianKanMapArray[i])
end

 

转载于:https://www.cnblogs.com/kimmy/p/3622349.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值