Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 洗牌2D纸牌阵列_C#_Arrays - Fatal编程技术网

C# 洗牌2D纸牌阵列

C# 洗牌2D纸牌阵列,c#,arrays,C#,Arrays,我有一个二维的“卡片”数组,我需要将这个数组按行和列洗牌 我的数组是这样的: private CardType[,] card = { {CardType.Basic, CardType.Basic2, CardType.Basic4 }, {CardType.Basic, CardType.Basic2, CardType.Basic30 }, {CardType.Basic, CardType.Basic10, CardType.Basic5 }, {Card

我有一个二维的“卡片”数组,我需要将这个数组按行和列洗牌

我的数组是这样的:

private CardType[,] card =
{
    {CardType.Basic, CardType.Basic2, CardType.Basic4 },
    {CardType.Basic, CardType.Basic2, CardType.Basic30 },
    {CardType.Basic, CardType.Basic10, CardType.Basic5 },
    {CardType.Basic, CardType.Basic20, CardType.Basic30 },
};

我需要一种按行和列洗牌的方法。

使用
类Random
生成源行和dest行、源列和dest-col

交换这两个元素(如果不相同)


经常这样做。

使用
类Random
生成源行和目的行、源列和目的列

交换这两个元素(如果不相同)


经常这样做。

使用Fisher-Yates算法:

public static void Shuffle<T>(Random random, T[,] array)
{
    int lengthRow = array.GetLength(1);

    for (int i = array.Length - 1; i > 0; i--)
    {
        int i0 = i / lengthRow;
        int i1 = i % lengthRow;

        int j = random.Next(i + 1);
        int j0 = j / lengthRow;
        int j1 = j % lengthRow;

        T temp = array[i0, i1];
        array[i0, i1] = array[j0, j1];
        array[j0, j1] = temp;
    }
}
请注意,您应该尝试重用
rnd
,而不是重新创建它


请注意
array.Length
是数组的总
长度
,X*Y,以及如何从“全局索引”
i
将其拆分为
i0
i1
X
Y
),方法是将模除以一行的长度(
lengtrow
)。

使用Fisher-Yates算法:

public static void Shuffle<T>(Random random, T[,] array)
{
    int lengthRow = array.GetLength(1);

    for (int i = array.Length - 1; i > 0; i--)
    {
        int i0 = i / lengthRow;
        int i1 = i % lengthRow;

        int j = random.Next(i + 1);
        int j0 = j / lengthRow;
        int j1 = j % lengthRow;

        T temp = array[i0, i1];
        array[i0, i1] = array[j0, j1];
        array[j0, j1] = temp;
    }
}
请注意,您应该尝试重用
rnd
,而不是重新创建它


请注意
array.Length
是数组的总
长度
,X*Y,以及如何从“全局索引”
i
将其拆分为
i0
i1
X
Y
),方法是将模数除以一行的长度(
lengtrow
).

您可以使用标准的随机播放来执行此操作。只需将单个索引转换为行/列值,如下所示:

/// <summary>Used to shuffle collections.</summary>

public class Shuffler
{
    public Shuffler()
    {
        _rng = new Random();
    }

    /// <summary>Creates the shuffler with a specific random number generator.</summary>

    public Shuffler(Random rng)
    {
        _rng = rng;
    }

    /// <summary>Shuffles the specified array.</summary>

    public void Shuffle<T>(IList<T> array)
    {
        for (int n = array.Count; n > 1; )
        {
            int k = _rng.Next(n);
            --n;
            T temp = array[n];
            array[n] = array[k];
            array[k] = temp;
        }
    }

    /// <summary>Shuffles the specified 2D array.</summary>

    public void Shuffle<T>(T[,] array)
    {
        int w = array.GetUpperBound(1)+1;

        for (int n = array.Length; n > 1; )
        {
            int k = _rng.Next(n);
            --n;

            int dr = n/w;
            int dc = n%w;
            int sr = k/w;
            int sc = k%w;

            T temp = array[dr,dc];
            array[dr,dc] = array[sr,sc];
            array[sr,sc] = temp;
        }
    }

    private readonly Random _rng;
}
///用于洗牌集合。
公共类洗牌者
{
公众洗牌者()
{
_rng=新随机数();
}
///使用特定的随机数生成器创建洗牌器。
公共洗牌器(随机rng)
{
_rng=rng;
}
///洗牌指定的数组。
公共无效洗牌(IList数组)
{
对于(int n=array.Count;n>1;)
{
int k=_rng.Next(n);
--n;
温度=阵列[n];
数组[n]=数组[k];
数组[k]=温度;
}
}
///洗牌指定的2D数组。
公共无效洗牌(T[,]数组)
{
intw=array.GetUpperBound(1)+1;
对于(int n=array.Length;n>1;)
{
int k=_rng.Next(n);
--n;
int dr=不适用;
int dc=n%w;
int sr=k/w;
int sc=k%w;
温度=阵列[dr,dc];
阵列[dr,dc]=阵列[sr,sc];
阵列[sr,sc]=温度;
}
}
私有只读随机;
}
您可以这样使用它:

int[,] array = new int[5, 7];
int w = array.GetUpperBound(1)+1;

// Fill array with 0, 1, 2, ... , 5*7-1

for (int i = 0; i < array.Length; ++i)
{
    int sr = i/w;
    int sc = i%w;

    array[sr, sc] = i;
}

var shuffler = new Shuffler();
shuffler.Shuffle(array);

Console.WriteLine(array[0,0]);
int[,]数组=新的int[5,7];
intw=array.GetUpperBound(1)+1;
//用0,1,2,…,填充数组,5*7-1
for(int i=0;i
您可以使用标准的随机播放来执行此操作。只需将单个索引转换为行/列值,如下所示:

/// <summary>Used to shuffle collections.</summary>

public class Shuffler
{
    public Shuffler()
    {
        _rng = new Random();
    }

    /// <summary>Creates the shuffler with a specific random number generator.</summary>

    public Shuffler(Random rng)
    {
        _rng = rng;
    }

    /// <summary>Shuffles the specified array.</summary>

    public void Shuffle<T>(IList<T> array)
    {
        for (int n = array.Count; n > 1; )
        {
            int k = _rng.Next(n);
            --n;
            T temp = array[n];
            array[n] = array[k];
            array[k] = temp;
        }
    }

    /// <summary>Shuffles the specified 2D array.</summary>

    public void Shuffle<T>(T[,] array)
    {
        int w = array.GetUpperBound(1)+1;

        for (int n = array.Length; n > 1; )
        {
            int k = _rng.Next(n);
            --n;

            int dr = n/w;
            int dc = n%w;
            int sr = k/w;
            int sc = k%w;

            T temp = array[dr,dc];
            array[dr,dc] = array[sr,sc];
            array[sr,sc] = temp;
        }
    }

    private readonly Random _rng;
}
///用于洗牌集合。
公共类洗牌者
{
公众洗牌者()
{
_rng=新随机数();
}
///使用特定的随机数生成器创建洗牌器。
公共洗牌器(随机rng)
{
_rng=rng;
}
///洗牌指定的数组。
公共无效洗牌(IList数组)
{
对于(int n=array.Count;n>1;)
{
int k=_rng.Next(n);
--n;
温度=阵列[n];
数组[n]=数组[k];
数组[k]=温度;
}
}
///洗牌指定的2D数组。
公共无效洗牌(T[,]数组)
{
intw=array.GetUpperBound(1)+1;
对于(int n=array.Length;n>1;)
{
int k=_rng.Next(n);
--n;
int dr=不适用;
int dc=n%w;
int sr=k/w;
int sc=k%w;
温度=阵列[dr,dc];
阵列[dr,dc]=阵列[sr,sc];
阵列[sr,sc]=温度;
}
}
私有只读随机;
}
您可以这样使用它:

int[,] array = new int[5, 7];
int w = array.GetUpperBound(1)+1;

// Fill array with 0, 1, 2, ... , 5*7-1

for (int i = 0; i < array.Length; ++i)
{
    int sr = i/w;
    int sc = i%w;

    array[sr, sc] = i;
}

var shuffler = new Shuffler();
shuffler.Shuffle(array);

Console.WriteLine(array[0,0]);
int[,]数组=新的int[5,7];
intw=array.GetUpperBound(1)+1;
//用0,1,2,…,填充数组,5*7-1
for(int i=0;i
一个想法是创建另一个具有相同维度的数组,并在每个元素上设置一个优先级,即随机数。然后使用优先级对卡片阵列进行排序。这意味着什么“我需要将此阵列按行和列洗牌”?@xanatos这意味着他想洗牌。:)一个想法是创建另一个具有相同维度的数组,并在每个元素上设置一个优先级,即随机数。然后使用优先级对卡片阵列进行排序。这意味着什么“我需要将此阵列按行和列洗牌”?@xanatos这意味着他想洗牌。:)