我想检查一个大小为8x8的多维数组,如果一行中的4个有相同的值,然后调用另一个方法。IF as a example the positions 0/0, 0/1, 0/2, 0/3 have the value 1
,然后调用finish(1)
方法。但这也应该适用于对角线,如0/0,1/1,2/2,3/3。
发布于 2016-09-01 19:59:01
您可以创建一个函数,并为数组的每个元素调用它。如果成功,它将返回值,调用finish函数并结束循环。如下所示:
int Check(int[,] a,int x, int y)
{
for(int n =-1;n<2;n++)
{
for(int m =-1;m<2;m++)
{
if(n!= 0 || m!= 0)
{
int previousX = x;
int previousY = y;
int nextX = previousX+n;
int nextY = previousY+m;
int counter = 0;
while((nextX >= 0 && nextX < 8 && nextY >= 0 && nextY < 8)
&& (a[previousX,previousY] ==a[nextX,nextY]))
{
counter++;
previousX += n;
previousY += m;
nextX += n;
nextY += m;
if(counter >= 3)
return a[x,y];
}
}
}
}
return -1; //That means nothing was found
}
https://stackoverflow.com/questions/39249749
复制相似问题