using System.Collections.Generic;
const int MINIMUM_MATCH_COUNT = 3;
const int MAX_DIMENSION_X = 8;
const int MAX_DIMENSION_Y = 8;
const int MIN_BOARD_VALUE = 0;
const int MAX_BOARD_VALUE = 7;
public static void Main() {
Console.WriteLine("Board: ");
List<Match> matches = FindMatches(board);
foreach (Match match in matches)
Console.WriteLine(match.Direction + " match at (" + match.IndexX + "," + match.IndexY + ") Length: " + match.Length);
Console.WriteLine("No matches.");
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 = FindMatchForVertical(board, x, y);
match = FindMatchForHorizontal(board, x, y);
private static Match FindMatchForHorizontal(int[,] board, int xIndex, int yIndex) {
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)
return new Match(xIndex, yIndex, length, Match.MatchDirection.Horizontal);
private static Match FindMatchForVertical(int[,] board, int xIndex, int yIndex) {
int searchValue = board[xIndex, yIndex];
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)
return new Match(xIndex, yIndex, length, 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 Match(int IndexX, int IndexY, int Length, MatchDirection Direction) {
this.Direction = Direction;
public int IndexX { get; }
public int IndexY { get; }
public int Length { get; }
public MatchDirection Direction { get; }
private static int[,] CreateRandomFilledBoard() {
int[,] board = new int[MAX_DIMENSION_X, MAX_DIMENSION_Y];
Random random = new Random(Guid.NewGuid().GetHashCode() + Environment.TickCount);
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);