有人能帮我解决这个随机矩阵吗?

本文关键字:随机 解决 | 更新日期: 2023-09-27 18:07:21

问题是这个矩阵显示一个符号重复3次,我只需要显示2次符号,如果有人能帮我就太好了。我需要这个做记忆游戏。

Random rand = new Random();
string[,] Matrix = { { "!", "!", "@", "@" }, { "$", "$", "#", "#" }, { "%", "%", "^", "^" }, { "&", "&", "*", "*" } };
int row = 0;
int column = 0;
int row2 = 0;
int column2 = 0;
for (int inc = 0; inc < 51; inc++)
{
    row2 = rand.Next(4);
    column2 = rand.Next(4);
    Matrix[row, column] = Matrix[row2, column2];
    row = row2;
    column = column2;
}

Console.WriteLine("'n'n'n       {0} |  {1} |  {2} |  {3}", Matrix[0, 0], Matrix[0, 1], Matrix[0, 2], Matrix[0, 3]);
Console.WriteLine("     ----|----|----|----");
Console.WriteLine("       {0} |  {1} |  {2} |  {3}", Matrix[1, 0], Matrix[1, 1], Matrix[1, 2], Matrix[1, 3]);
Console.WriteLine("     ----|----|----|----");
Console.WriteLine("       {0} | {1} | {2} | {3}", Matrix[2, 0], Matrix[2, 1], Matrix[2, 2], Matrix[2, 3]);
Console.WriteLine("     ----|----|----|----");
Console.WriteLine("       {0}|  {1}|  {2}|  {3} 'n", Matrix[3, 0], Matrix[3, 1], Matrix[3, 2], Matrix[3, 3]);
Console.ReadLine();

有人能帮我解决这个随机矩阵吗?

作为快速修复,您可以替换以下代码:

Matrix[row, column] = Matrix[row2, column2];

char c = Matrix[row, column];
Matrix[row, column] = Matrix[row2, column2];
Matrix[row2, column2] = c;

在您的原始代码中,您失去了Matrix[row, column]的值,而您必须将其与[row2, column2]的值交换。

据我所知,你们正在为记忆训练开发"寻对"游戏?我想推荐你的返工算法。你可以使用这个语句来生成小随机数:1)int randomNumber = DateTime.Now.Ticks % 4或2)int randomNumber = rand.Next(100000) % 4;

注意:如果你想获得返工算法的帮助,我可以帮助你。