title: Finding 5 elements of the same color in a row
body: <p>I created a two dimensional array [19,19] of labels. On click a user changes the color of a label to either black or white depending on the iteration. I want a message box to pop up when there are five labels of the same color lined up horizontally, vertically, or diagonally. Only the diagonal check is not working and I am not sure why. Any help would be much appreciated.</p>
  
  <pre><code>class gamePlay
  {
      const int WinLength = 5;
      const int BoardWidth = 19;
      const int BoardHeight = 19;
  
      const int kwinningCount = 5;
  
      public Color? CheckWinner(Label[,] board)
      {
          int columnCount = board.GetLength(1), rowCount = board.GetLength(0);
  
          for (int row = 0; row &lt; rowCount; row++)
          {
              Color? lineResult = CheckWinnerForLine(board, 0, row, columnCount, 1, 0);
  
              if (lineResult != null)
              {
                  return lineResult;
              }
  
              if (rowCount - row &gt;= kwinningCount)
              {
                  lineResult = CheckWinnerForLine(board, 0, row, rowCount - row, 1, 1);
  
                  if (lineResult != null)
                  {
                      return lineResult;
                  }
              }
          }
  
          for (int column = 0; column &lt; columnCount; column++)
          {
              Color? lineResult = CheckWinnerForLine(board, column, 0, rowCount, 0, 1);
  
              if (lineResult != null)
              {
                  return lineResult;
              }
  
              if (column &gt; 0 &amp;&amp; columnCount - column &gt;= kwinningCount)
              {
                  lineResult =
                      CheckWinnerForLine(board, column, 0, columnCount - column, 1, 1);
  
                  if (lineResult != null)
                  {
                      return lineResult;
                  }
              }
          }
  
          return null;
      }
  
      Color? CheckWinnerForLine(Label[,] board,
          int column, int row, int count, int columnIncrement, int rowIncrement)
      {
          // Initialize from the first cell
          int colorCount = 1;
          Color currentColor = board[row, column].BackColor;
  
          while (--count &gt; 0)
          {
              column += columnIncrement;
              row += rowIncrement;
  
              Color cellColor = board[row, column].BackColor;
  
              if (currentColor != cellColor)
              {
                  // switched colors, so reset for this cell to be the first of the string
                  colorCount = 1;
                  currentColor = cellColor;
              }
              else if (++colorCount == kwinningCount &amp;&amp; cellColor != Color.Transparent)
              {
                  return cellColor;
              }
          }
  
          return null;
      }
  public partial class Form1 : Form
  {
      int iteration = 0;
      public Label[,] board = new Label[19,19];
      int count;
      int columnIncrement;
      int rowIncrement;
      const int WinLength = 5;
      const int BoardWidth = 19;
      const int BoardHeight = 19;
      gamePlay WinCheck = new gamePlay();
  
      public Form1()
      {
          InitializeComponent();
  
          int x = this.Location.X + 10;
          int y = this.Location.Y + 15;
  
          // create 361 labels, set their properties
          for (int i = 0; i &lt; 19; i++)
          {
              for (int j = 0; j &lt; 19; j++)
              {
                  board[i,j] = new Label();
                  board[i,j].Parent = this;
                  board[i,j].Name = "label" + i;
                  //board[i,j].BackColor = System.Drawing.ColorTranslator.FromHtml("#DBB262");
                  board[i,j].BackColor = Color.Transparent;
                  //board[i, j].StyleChanged = Opacity
                  //set size of labels
                  board[i,j].Size = new Size(30, 30);
                  //initialize click event handler
                  this.board[i,j].Click += new System.EventHandler(this.labelClick);
                  this.Controls.Add(board[i,j]);
                  board[i,j].BringToFront();
              }
  
          }
  
  
  
          // set the position of the label
          for (int i = 0; i &lt; 19; i++)
          {
              for (int j = 0; j &lt; 19; j++)
              {
                  board[i,j].Location = new Point(x, y);
                  //set distance between labels
                  if (x &gt;= 755)
                  {
                      x = this.Location.X + 10;
                      y += 42;
                  }
  
                  else
                  {
                      x += 43;
                  }
              }
  
          }
      }
  
          private void labelClick (object sender, EventArgs e)
          {
  
              Label clickedLabel = (Label)sender;
  
              if (clickedLabel.BackColor == Color.Transparent)
              {
                  if (iteration % 2 == 0)
                  {
                      clickedLabel.BackColor = Color.Black;
                  }
                  else
                  {
                      clickedLabel.BackColor = Color.White;
                  }
                  iteration++;
              }
              else
              {
  
              }
  
              for (int row = 0; row &lt; BoardHeight; row++)
              {
                  for (int column = 0; column &lt; BoardWidth; column++)
                  {
                      if (board[row, column] == clickedLabel)
                      {
                          Color? winner = WinCheck.CheckWinner(board);
                          if (winner == Color.Black)
                          {
                              MessageBox.Show("Black is the winner!");
                          }
                          else if (winner == Color.White)
                          {
                              MessageBox.Show("White is the winner!");
                          }
                      }
                  }
              }
          }
  
          private int[] FindClickedLabelCoordinates(Label[,] board, Label label)
          {
              for (int row = 0; row &lt; BoardHeight; row++)
              {
                  for (int column = 0; column &lt; BoardWidth; column++)
                  {
                      if (board[row, column] == label)
                          return new int[] { row, column };
                  }
              }
              return null;
          }
  </code></pre>