Four lines in a row

I am currently working on the base four consecutive games, but I rather adhere to the logic behind it.

I currently have this multidimensional array that represents a board

[
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0]
]

0will represent an empty slot, 1and 2represent the player. So let you get this array after a while:

[
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 1, 1, 0, 0],
    [0, 0, 0, 1, 1, 0, 0],
    [0, 0, 1, 2, 2, 2, 0],
    [0, 1, 2, 2, 1, 2, 0]
]

How can I write logic to check if there are four in a row? Calculating it for horizontal and vertical looks pretty easy (although it still works out best), but how would this be done for diagonal lines?

+5
source share
2 answers

It’s best to divide your search space into four:

  • verticals;
  • horizontal
  • right and down;
  • to the right and up.

.

, board[row=0-5][col=0-6] board[0][0] .

( ):

for row = 0 to 2:
    for col = 0 to 6:
        if board[row][col] != 0 and
           board[row][col] == board[row+1][col] and
           board[row][col] == board[row+2][col] and
           board[row][col] == board[row+3][col]:
               return board[row][col]

, , , , . , , 3, , 3, 4, 5 6 ( ).

, :

for row = 0 to 5:
    for col = 0 to 3:
        if board[row][col] != 0 and
           board[row][col] == board[row][col+1] and
           board[row][col] == board[row][col+2] and
           board[row][col] == board[row][col+3]:
               return board[row][col]

, :

for row = 0 to 2:
    for col = 0 to 3:
        if board[row][col] != 0 and
           board[row][col] == board[row+1][col+1] and
           board[row][col] == board[row+2][col+2] and
           board[row][col] == board[row+3][col+3]:
               return board[row][col]

for row = 3 to 5:
    for col = 0 to 3:
        if board[row][col] != 0 and
           board[row][col] == board[row-1][col+1] and
           board[row][col] == board[row-2][col+2] and
           board[row][col] == board[row-3][col+3]:
               return board[row][col]

, for col = 0 to 3 , , ( ), . , , :

for col = 0 to 3:
    for row = 0 to 2:
        if board[row][col] != 0 and
           board[row][col] == board[row+1][col+1] and
           board[row][col] == board[row+2][col+2] and
           board[row][col] == board[row+3][col+3]:
               return board[row][col]
    for row = 3 to 5:
        if board[row][col] != 0 and
           board[row][col] == board[row-1][col+1] and
           board[row][col] == board[row-2][col+2] and
           board[row][col] == board[row-3][col+3]:
               return board[row][col]

, , 0 1 2.

, , :

row
 0   [0, 0, 0, 0, 0, 0, 0]
 1   [0, 0, 0, 0, 0, 0, 0]
 2   [0, 0, 0, 1, 1, 0, 0]
 3   [0, 0, 0, 1, 1, 0, 0]
 4   [0, 0, 1, 2, 2, 2, 0]
 5 > [0, 1, 2, 2, 1, 2, 0]
         ^
      0  1  2  3  4  5  6 <- col

, {5,1}, {5,1}, {4,2}, {3,3} {2,4} 1.

+12

. , , : ( C)

int checkWinOrLose(int grid[][7],int result,int rowNum) {
//  For checking whether any win or lose condition is reached. Returns 1 if win or lose is reached. else returns 0
//  grid[][] is the 6X7 matrix
//  result is the column number where the last coin was placed
//  rowNum is the row number where the last coin was placed

    int player=grid[rowNum][result];
    if(rowNum<=2 && grid[rowNum+1][result]==player && grid[rowNum+2][result]==player && grid[rowNum+3][result]==player) // 4 in a row vertically
        return 1;
    else {
        int count=1,i,j;
        for(i=result+1;i<7;i++) { // 4 in a row horizontally
            if(grid[rowNum][i]!=player)
                break;
            count++;
        }
        for(i=result-1;i>=0;i--) { // 4 in a row horizontally
            if(grid[rowNum][i]!=player)
                break;
            count++;
        }
        if(count>=4)
            return 1;
        count=1;
        for(i=result+1,j=rowNum+1;i<7 && j<6;i++,j++) { // 4 in a row diagonally
            if(grid[j][i]!=player)
                break;
            count++;
        }
        for(i=result-1,j=rowNum-1;i>=0 && j>=0;i--,j--) { // 4 in a row diagonally
            if(grid[j][i]!=player)
                break;
            count++;
        }
        if(count>=4)
            return 1;
        count=1;
        for(i=result+1,j=rowNum-1;i<7 && j>=0;i++,j--) { // 4 in a row diagonally
            if(grid[j][i]!=player)
                break;
            count++;
        }
        for(i=result-1,j=rowNum+1;i>=0 && j<6;i--,j++) { // 4 in a row diagonally
            if(grid[j][i]!=player)
                break;
            count++;
        }
        if(count>=4)
            return 1;
    }
    return 0;
}
+2

All Articles