C# 将空锯齿状数组与3x3锯齿状数组进行比较,以匹配不匹配但行匹配的数字序列
我正在做一个井字游戏。我使用两个锯齿状数组来确定获胜者。一个数组是空的,在switch语句中分配了网格号。另一个包含所有可能的组合(可能不是最好的方法) 这是另一个锯齿阵列,包含所有可能的获胜组合C# 将空锯齿状数组与3x3锯齿状数组进行比较,以匹配不匹配但行匹配的数字序列,c#,C#,我正在做一个井字游戏。我使用两个锯齿状数组来确定获胜者。一个数组是空的,在switch语句中分配了网格号。另一个包含所有可能的组合(可能不是最好的方法) 这是另一个锯齿阵列,包含所有可能的获胜组合 int[][] getsequence = { new int[] {1,2,3}, new int[] {4,5,6}, new int[] {7,8,9}, new int[] {1,4,7
int[][] getsequence =
{
new int[] {1,2,3},
new int[] {4,5,6},
new int[] {7,8,9},
new int[] {1,4,7},
new int[] {2,5,8},
new int[] {3,6,9},
new int[] {1,5,9},
new int[] {9,5,1},
new int[] {3,2,1},
new int[] {6,5,4},
new int[] {9,8,7},
new int[] {7,4,1},
new int[] {8,5,2},
new int[] {9,6,3},
new int[] {3,5,7},
new int[] {7,5,3}
下面是查看是否存在匹配的算法
public static void checkWinner(Player p)
{
foreach (int[] row in p.Sequence)
{
foreach (int[] row2 in p.GetSequence)
{
if (Enumerable.SequenceEqual(row, row2))
{
Console.WriteLine("Win");
}
}
}
}
这是一个switch语句,可以控制这些东西
public static string[,] updateGameBoard(string[,] matrix, int selection, Player p)
{
switch (selection)
{
case 1:
matrix[0, 0] = p.Type;
p.Sequence[0][0] = 1;
break;
case 2:
matrix[0, 1] = p.Type;
p.Sequence[0][1] = 2;
break;
case 3:
matrix[0, 2] = p.Type;
p.Sequence[0][2] = 3;
break;
case 4:
matrix[1, 0] = p.Type;
p.Sequence[1][0] = 4;
break;
case 5:
matrix[1, 1] = p.Type;
p.Sequence[1][1] = 5;
break;
case 6:
matrix[1, 2] = p.Type;
p.Sequence[1][2] = 6;
break;
case 7:
matrix[2, 0] = p.Type;
p.Sequence[2][0] = 7;
break;
case 8:
matrix[2, 1] = p.Type;
p.Sequence[2][1] = 8;
break;
case 9:
matrix[2, 2] = p.Type;
p.Sequence[2][2] = 9;
break;
default:
Console.WriteLine("Error");
break;
}
Console.WriteLine();
return matrix;
}
矩阵2D数组只是我将数字转换为“O”或“X”的一种方式
这就是p.Type
正在做的事情p
是播放器对象
string[,] matrix =
{
{"1","2","3" },
{"4","5","6" },
{"7","8","9" }
};
不管怎么说,它似乎几乎起作用了。像、123、456等行似乎起作用,玩家赢得了比赛。但如果我把所有的“O”都放在147,比如说,什么都不会发生。我调试了它,foreach循环中的行似乎就是问题所在。我会说,当我调试时,p.GetSequence
(第2行)将每个数组显示为123、456、789等。foreach循环中的p.Sequence
(第2行)显示为100,然后在下一次顶部迭代中显示400,然后是700。因此,我认为行可以工作,但列不能。因为他们不匹配。这是我的理论。如何将列作为行进行比较
最后,我不知道这是否重要,但我有一个版本的这个工作(sorta)Sequence
和getSequence
只是全局的,而不是在播放器对象中。问题是,这名球员最终会获胜,但这并不是按顺序进行的。加上每个玩家都需要自己的锯齿阵列,否则你怎么区分X和O呢
是的,我认为它,我需要将列转换成一行。正如我在调试getSequence时所说,它将值显示为行,sequence将值显示为列。当我把值作为列,但如果我输入123,它就是行。我看到关于如何做到这一点的文章。但是如果有人有一个简单的方法,请告诉我 我想出来了,可能不是最好的算法,但它很有效。即使找到匹配项,内循环仍然滚动16次,所以我只使用了bool
if (player1.Turn > 2)
{
foreach (int[] row in player1.Sequence)
{
foreach (int[] row2 in player1.GetSequence)
{
foreach (int[] col in player1.TrackCol)
{
if (Enumerable.SequenceEqual(row, row2))
{
winner = true;
}
else if(Enumerable.SequenceEqual(col, row2))
{
winner = true;
}
}
}
}
}
以下是switch语句:
public static string[,] updateGameBoard(string[,] matrix, int selection, Player p)
{
switch (selection)
{
case 1:
matrix[0, 0] = p.Type;
p.Sequence[0][0] = 1;
p.TrackCol[0][0] = 1;
break;
case 2:
matrix[0, 1] = p.Type;
p.Sequence[0][1] = 2;
p.TrackCol[0][0] = 2;
break;
case 3:
matrix[0, 2] = p.Type;
p.Sequence[0][2] = 3;
p.TrackCol[0][0] = 3;
break;
case 4:
matrix[1, 0] = p.Type;
p.Sequence[1][0] = 4;
p.TrackCol[0][1] = 4;
break;
case 5:
matrix[1, 1] = p.Type;
p.Sequence[1][1] = 5;
p.TrackCol[0][1] = 5;
break;
case 6:
matrix[1, 2] = p.Type;
p.Sequence[1][2] = 6;
p.TrackCol[0][1] = 6;
break;
case 7:
matrix[2, 0] = p.Type;
p.Sequence[2][0] = 7;
p.TrackCol[0][2] = 7;
break;
case 8:
matrix[2, 1] = p.Type;
p.Sequence[2][1] = 8;
p.TrackCol[0][2] = 8;
break;
case 9:
matrix[2, 2] = p.Type;
p.Sequence[2][2] = 9;
p.TrackCol[0][2] = 9;
break;
default:
Console.WriteLine("Error");
break;
如果你想聪明一点,使用3x3。当正确安排时,获胜玩家的单元格总是加在15,所以你不需要这种序列匹配。批评我的代码,请考虑放弃那个奇怪的java混合的案例命名(<代码>校验器{} /代码>,<代码> UpDeDigeMeals< /Co> >),并使用动词作为类或var名称。(
getsequence
-这是一个序列,而不是get…)。从网络框架中无数类和方法中使用的命名风格中汲取线索如果你有解决方案,你可以将其作为答案发布。自我回答是完全可以接受和鼓励的。请不要将答案编辑到问题中。
public static string[,] updateGameBoard(string[,] matrix, int selection, Player p)
{
switch (selection)
{
case 1:
matrix[0, 0] = p.Type;
p.Sequence[0][0] = 1;
p.TrackCol[0][0] = 1;
break;
case 2:
matrix[0, 1] = p.Type;
p.Sequence[0][1] = 2;
p.TrackCol[0][0] = 2;
break;
case 3:
matrix[0, 2] = p.Type;
p.Sequence[0][2] = 3;
p.TrackCol[0][0] = 3;
break;
case 4:
matrix[1, 0] = p.Type;
p.Sequence[1][0] = 4;
p.TrackCol[0][1] = 4;
break;
case 5:
matrix[1, 1] = p.Type;
p.Sequence[1][1] = 5;
p.TrackCol[0][1] = 5;
break;
case 6:
matrix[1, 2] = p.Type;
p.Sequence[1][2] = 6;
p.TrackCol[0][1] = 6;
break;
case 7:
matrix[2, 0] = p.Type;
p.Sequence[2][0] = 7;
p.TrackCol[0][2] = 7;
break;
case 8:
matrix[2, 1] = p.Type;
p.Sequence[2][1] = 8;
p.TrackCol[0][2] = 8;
break;
case 9:
matrix[2, 2] = p.Type;
p.Sequence[2][2] = 9;
p.TrackCol[0][2] = 9;
break;
default:
Console.WriteLine("Error");
break;