C# 有人能帮我修一下这个随机矩阵吗
问题是这个矩阵显示一个符号重复3次,我只需要显示符号2次, 如果有人能帮助我,那就太好了。 我需要这个来做记忆游戏C# 有人能帮我修一下这个随机矩阵吗,c#,memory,matrix,C#,Memory,Matrix,问题是这个矩阵显示一个符号重复3次,我只需要显示符号2次, 如果有人能帮助我,那就太好了。 我需要这个来做记忆游戏 Random rand = new Random(); string[,] Matrix = { { "!", "!", "@", "@" }, { "$", "$", "#", "#" }, { "%", "%", "^", "^" }, { "&", "&", "*", "*" } }; int row = 0; int column = 0; int r
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();
Random rand=new Random();
字符串[,]矩阵={{“!”、“!”、“@”、“@”}、{“$”、“$”、“#”、“#”}、{“%”、“%”、“^”、“^”}、{“&”、“*”、“*”、“*”};
int行=0;
int列=0;
int row2=0;
int column2=0;
对于(int inc=0;inc<51;inc++)
{
row2=下一个随机数(4);
第2列=下一个随机数(4);
矩阵[行,列]=矩阵[行2,列2];
行=行2;
列=列2;
}
WriteLine(“\n\n\n{0}{1}{2}{3}”,矩阵[0,0],矩阵[0,1],矩阵[0,2],矩阵[0,3]);
Console.WriteLine(“----|-------|----”);
WriteLine({0}{1}{2}{3}),矩阵[1,0],矩阵[1,1],矩阵[1,2],矩阵[1,3]);
Console.WriteLine(“----|-------|----”);
WriteLine(“{0}{1}{2}{3}”,矩阵[2,0],矩阵[2,1],矩阵[2,2],矩阵[2,3]);
Console.WriteLine(“----|-------|----”);
WriteLine(“{0}{1}{2}{3}\n”,矩阵[3,0],矩阵[3,1],矩阵[3,2],矩阵[3,3]);
Console.ReadLine();
据我所知,你正在开发“寻找配对”的记忆训练游戏?
我想重新编译你的返工算法。
您可以使用此语句生成小的随机数:
1)
int randomNumber=DateTime.Now.Ticks%4
或
2)
int randomNumber=rand.Next(100000)%4代码>
注意:如果您想获得有关返工算法的帮助,我可以帮助您。作为快速修复,您可以替换以下代码:
Matrix[row, column] = Matrix[row2, column2];
与
在原始代码中,您正在丢失矩阵[row,column]
处的值,而您必须将其与[row2,column2]
处的值交换?你说一个符号重复三次是什么意思?如何修复它,我只需要显示两次符号。非常感谢你解决了我的问题。再次感谢你是的,我正在开发“寻找配对”,谢谢你的帮助和提议,但如果可能的话,我想在没有帮助的情况下创作。
char c = Matrix[row, column];
Matrix[row, column] = Matrix[row2, column2];
Matrix[row2, column2] = c;