2

I want to check a multidimensional-array which size is 8x8 if 4 in a row have the same value and then call another Method.IF as a example the positions 0/0, 0/1, 0/2, 0/3 have the value 1 then the Method finish(1) gets Called. 'But that should also work on Diagonal like 0/0, 1/1, 2/2, 3/3.'

Simon B
  • 179
  • 1
  • 1
  • 9
  • Whats means `Example 0/0, 0/1, 0/2, 0/3 have the value 1` ? I don't see that every tuple contains value 1. – matiii Aug 31 '16 at 12:36
  • I'm not really sure what you are doing here. Perhaps some code would make it clearer? In particular I don't actually understand your example... – Chris Aug 31 '16 at 12:36
  • Is this a game matrix where you are trying ti line up values in a line or diagonally? – Swagata Prateek Aug 31 '16 at 12:37
  • @mati 0/0 = array[0, 0] – Simon B Aug 31 '16 at 12:38
  • I have this: https://upload.wikimedia.org/wikipedia/commons/2/2a/Connect_Four.jpg as a multidimensional-array and I want to check if someone has won – Simon B Aug 31 '16 at 12:40
  • 1
    Obviously I don't know how the rest of your program works but if you are making this game and checking after every move you don't want to check the entire board every time since any new four in a row would have to include the just played piece. And once you are looking at it like that then there's not much to be done apart from just checking all the possibilities. – Chris Aug 31 '16 at 12:46
  • http://stackoverflow.com/questions/7033165/algorithm-to-check-a-connect-four-field – lordkain Aug 31 '16 at 12:51
  • [Here's](http://codereview.stackexchange.com/questions/96545/connect-4-glorified-tic-tac-toe-implementation) some code reviews you can look to get some ideas but if you're not checking after every input and checking the full board at a time, there's not much to do except check every possibility. – Swagata Prateek Aug 31 '16 at 12:52
  • Your question is missing a problem statement. I suspect you tried to solve this yourself and run into some sort of issue? As it is now, you could post it on a freelancer project page just as well, because you basically ask for a solution to a task. – grek40 Aug 31 '16 at 13:13

1 Answers1

1

You could create a function and call it for every element of the array. On success it would return the value, call the finish function and end the loop. Something like this:

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
}
jambonick
  • 716
  • 1
  • 6
  • 12