Files
ft/Client/Assets/Scripts/EventCubeMelt/Utils/UniqueRandom99.cs
2026-06-29 21:18:33 +08:00

51 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// using System;
using System.Collections.Generic;
namespace EventCubeMelt.Utils
{
class UniqueRandom99
{
private readonly List<(int r, int c)> _pool = new List<(int, int)>();
// private readonly Random _rng = new Random();
private int _idx = 0;
public UniqueRandom99(int[,] arr)
{
int m = arr.GetLength(0);
int n = arr.GetLength(1);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (arr[i, j] == 99)
_pool.Add((i, j));
Shuffle(); // 初始随机化
}
/// <summary>
/// 返回一个从未返回过的 99 坐标;若数组里本来就没有 99则一直返回 (-1,-1)
/// 所有 99 用完后自动洗牌进入下一轮。
/// </summary>
public (int r, int c) Next()
{
if (_pool.Count == 0) return (-1, -1);
if (_idx == _pool.Count) // 本轮用完,重置并洗牌
{
// _idx = 0;
// Shuffle();
return (-1, -1);
}
return _pool[_idx++];
}
private void Shuffle()
{
for (int i = _pool.Count - 1; i > 0; i--)
{
var j = UnityEngine.Random.Range(0, i + 1);
(_pool[i], _pool[j]) = (_pool[j], _pool[i]);
}
}
}
}