using System.Collections.Generic;
public static void Main()
int[,] board = CreateRandomFilledBoard();
Console.WriteLine("Board: ");
List<Match> matches = FindMatches(board);
foreach (Match match in matches)
Console.WriteLine(match.Direction + " match at (" + match.IndexX + "," + match.IndexY + ")");
private static List<Match> FindMatches(int[,] board)
List<Match> matches = new List<Match>();
for (int y = 0; y < board.GetLength(1); ++y)
for (int x = 0; x < board.GetLength(0); ++x)
Match match = FindMatchFor(board, x, y);
private static Match FindMatchFor(int[,] board, int xIndex, int yIndex)
const int MINIMUM_MATCH_COUNT = 3;
List<Match> matches = new List<Match>();
int searchValue = board[xIndex, yIndex];
int remainingIndexCount = (board.GetLength(0) - xIndex) - 1;
if (remainingIndexCount >= (MINIMUM_MATCH_COUNT - 1))
for (int i = 0; i < remainingIndexCount; ++i)
if (board[xIndex + 1 + i, yIndex] == searchValue)
if (length >= MINIMUM_MATCH_COUNT)
Direction = Match.MatchDirection.Horizontal,
int remainingIndexCount = (board.GetLength(1) - yIndex) - 1;
for (int y = yIndex; y < board.GetLength(1); ++y)
for (int i = 0; i < remainingIndexCount; ++i)
if (board[xIndex, yIndex + 1 + i] == searchValue)
if (length >= MINIMUM_MATCH_COUNT)
Direction = Match.MatchDirection.Vertical,
private static void PrintBoard(int[,] board)
for (int x = 0; x < board.GetLength(0); ++x)
for (int x = 0; x < board.GetLength(0); ++x)
for (int y = 0; y < board.GetLength(1); ++y)
for (int x = 0; x < board.GetLength(0); ++x)
Console.Write(" " + board[x, y]);
public enum MatchDirection
public MatchDirection Direction { get; set; }
public int IndexX { get; set; }
public int IndexY { get; set; }
public int Length { get; set; }
private static int[,] CreateRandomFilledBoard()
const int MAX_DIMENSION_X = 8;
const int MAX_DIMENSION_Y = 8;
const int MIN_BOARD_VALUE = 0;
const int MAX_BOARD_VALUE = 7;
int[,] board = new int[MAX_DIMENSION_X, MAX_DIMENSION_Y];
Random random = new Random(Guid.NewGuid().GetHashCode());
for (int i = 0; i < board.GetLength(0); ++i)
for (int j = 0; j < board.GetLength(1); ++j)
board[i, j] = random.Next(MIN_BOARD_VALUE, MAX_BOARD_VALUE + 1);