private int[] GetIntLists(int stackNum)  // stackNum 即为n(数的种类)
{
int[] bubbleType = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};  // 这是个池子,我们从这里选出我们需要的n个数
List<int> intLists = new List<int>();
List<int> resultList = new List<int>();
for (int i = 0; i < bubbleType.Length; i++)
{
intLists.Add(bubbleType[i]);  //我们这里将数存放到list种,
}
for (int i = 0; i < stackNum; i++) // 然后随机stackNum次(n次),选出我们的n种数来
{
int index = Random.Range(0, intLists.Count); // 随机
resultList.Add(intLists[index]);
intLists.RemoveAt(index);// 避免重复这里删掉
}

int[] bubbles = new int[stackNum * 4]; // n 种数,每种m个,所以我们这里开始构建我们的待乱序数组一共是(n * m个数),这里的m等于4等于下面的  PlayingConfig.Instance.GetConfig().stack_full_content
int indexGroup = 0;
foreach (int item in resultList)
{
for (int i = 0; i < PlayingConfig.Instance.GetConfig().stack_full_content; i++)
{
bubbles[indexGroup * PlayingConfig.Instance.GetConfig().stack_full_content + i] = item;
}
indexGroup++;
}

//  到这里我们构建出了我们的数组

/*

例如我们之前选出了2, 5, 9这里我们数组会是{2, 2, 2, 2, 5, 5, 5, 5, 9, 9, 9, 9} 这里即bubbles

*/

int[][] groups = new int[stackNum][];
for (int i = 0; i < stackNum; i++)
{
groups[i] = new int[PlayingConfig.Instance.GetConfig().stack_full_content];
}

/*

这里构建了一个n * m 的数组我们需要将{2, 2, 2, 2, 5, 5, 5, 5, 9, 9, 9, 9}变成

{

  {......},

  {......},

  {......}

}  n 行 m列

*/

int resIndex = bubbles.Length;
int resCount = bubbles.Length;
for (int i = 0; i < resCount; i++)
{
int random = Random.Range(0, resIndex);// 从数组中随机数,
int _bubbleType = bubbles[random];
bubbles[random] = bubbles[resIndex - 1];
resIndex--; 

// resIndex-- 是这样

/*

对于一个数组{0, 1, 2, 3},由于我们不能像list那样动态改变其大小,这里我的思路是,

假如我们随机的下标是1,此时我们得到的是1,那么下一次我们将从{0, '1没了',2, 3}中获取,这时我们用数组的[resIndex - 1]代替原来1的位置,然后,范围缩小1

即数组变成了{0, 3, 2, 3} 但由于我们resIndex-- 了,这时的末尾的3就不会再随机到了,以此类推

*/
int groupNum = (i / PlayingConfig.Instance.GetConfig().stack_full_content);
int groupIndex = (i % PlayingConfig.Instance.GetConfig().stack_full_content);
groups[groupNum][groupIndex] = _bubbleType;
}

 

/*

这里我们的n * m列数组构建好了,且 每一行都是随机的,

*/

int rowSame = -1;
while ((rowSame = RowAllSame(groups)) >= 0)    // 当某一行的m个数都一样时,再次进行打散
{
int randomIndex = Random.Range(1, groups.Length);
if(randomIndex == rowSame)
{
randomIndex = (randomIndex + 1) % stackNum;
}
int exRow1 = Random.Range(0, groups[rowSame].Length);
int exRow2 = Random.Range(0, groups[randomIndex].Length);
int tmp = groups[rowSame][exRow1];
groups[rowSame][exRow1] = groups[randomIndex][exRow2];
groups[randomIndex][exRow2] = tmp;
}
indexGroup = 0;
for (int i = 0; i < groups.Length; i++)
{
for (int j = 0; j < groups[i].Length; j++)
{
bubbles[indexGroup] = groups[i][j];
indexGroup++;
}
}
return bubbles;
}

 

// 第一个方法结束了

private int RowAllSame(int[][] group)
{
int result = -1;
for (int i = 0; i < group.Length; i++)
{
bool rowResult = false;
for (int j = 0; j < group[i].Length - 1; j++)
{
if(group[i][j] != group[i][j + 1])
{
rowResult = true;
break;
}
}
if (!rowResult)
{
result = i;
break;
}
}
return result;
}

posted on 2021-01-15 16:00  百晓灵狐  阅读(155)  评论(0编辑  收藏  举报