The board would be set up as an int[][] where the first index is the horizontal position in the "board," and the second is the vertical. Let's say the bottom left is 0,0. Example:

 0 | 1 | 2
---+---+---
 0 | 1 | 0  
---+---+---
 1 | 0 | 0

The numbers are the values, and because 1,2 and 1,1 have values of 1, the checking method should return true.

I was thinking about maybe regex? What is the best way to handle this? Thanks so much.

4 Answers

0
Rob On

Start brute force with the first cell (index), check against all neighboring cells. Go to the next cell, check against neighboring cells, then the next, and so on.

When you get that working, you'll probably get some ideas for improving it. Eventually you will understand the process and can advance to better algorithms.

0
Laslos On

You will need to check each cell for a match by checking all surrounding cells (if they exist) for a total of 8 checks per cell. This is the basic idea of what you will want to do. I wrote the first condition to give you the idea.

bool checkBoard(int[,] boardArray) {
    for (int i = 0; i < boardArray.GetLength(0); i++) {
        for (int j = 0; j < boardArray.GetLength(1); j++) {
            // Above
            if ((j+1) < boardArray.GetLength(1) && boardArray[i][j] == boardArray[i][j+1])
                return true;
            // Above Right
            if ()
                return true;
            // Right
            if ()
                return true;
            // Below Right
            if ()
                return true;
            // Below
            if ()
                return true;
            // Below Left
            if ()
                return true;
            // Left
            if ()
                return true;
            // Above Left
            if ()
                return true;
        }
    }
    return false;
}
1
Rufus L On

I think you have to just traverse the rows and check if the value in the current cell matches one of it's neighbors. If you always move from left to right and top to bottom, then you only need to check a maximum of 4 other cells: the cell to the right and the three cells below (prev column, this column, and next column). As soon as you find a match, you return true. If no matches are found, return false:

private static bool HasRepeatingItem(int[][] board)
{
    if (board == null) return false;

    for (int row = 0; row < board.Length; row++)
    {
        for (int col = 0; col < board[row].Length; col++)
        {
            var thisValue = board[row][col];

            // If there's another column, check for horizontal match
            if (col < board[row].Length - 1 && thisValue == board[row][col + 1])
                return true;

            // If there's a row below, check for matches
            if (row < board.Length - 1)
            {
                // Check directly below for vertical match
                if (thisValue == board[row + 1][col]) return true;

                // Check below left for horizontal match
                if (col > 0 && thisValue == board[row + 1][col - 1]) return true;

                // Check below right for horizontal match
                if (col < board[row].Length - 1 && thisValue == board[row + 1][col + 1]) 
                    return true;
            }
        }
    }

    return false;
}
0
realharry On

Based on my understanding of the question (without any further constraints), the following straightforward solution should give what you need:

// Pseudo-code. Did not test it.
public static bool foundSameValuesInARow(int[,] board) { // board: NxN array. We assume it's a square array.
    int N = board.getLength(0);
    for(int i=0;i<N-1;i++) {
        for(int j=0;j<N-1;j++) {
            if(board[i,j] == board[i+1,j] || board[i,j] == board[i,j+1]) {
                return true;
            }
        }
    }
    for(int k=0;k<N-1;k++) {
        if(board[k, N-1] == board[k+1,N-1] || board[N-1,k] == board[N-1,k+1]) {
            return true;
        }
    }
    return false;
}