14

I know there is a lot of of questions regarding connect 4 check for a win. The issue is that most of other algorithms make my program have runtime errors, because they try to access an index outside of my array. My algorithm is like this:

private int checkWin(int[][] gridTable,int rowNum,int colNum, int maxRow, int maxCol) 
{
//  For checking whether any win or lose condition is reached. Returns 1 if win or lose is reached. else returns 0
//  gridTable[][] is the game matrix(can be any number of rows and columns between 4 and 40)
//  colNum is the column number where the last token was placed
//  rowNum is the row number where the last token was placed
//  maxRow is the number of rows in my grid
//  maxCol is the number of columns in my grid

int player = gridTable[rowNum][colNum]; //player ID
int count=0;

// Horizontal check
for (int i=0;i<maxCol;i++)
{
    if (gridTable[rowNum][i]==player)
        count++;
    else
        count=0;

    if (count>=4)
        return 1;
}
//Vertical check
for (int i=0;i<maxRow;i++)
{
    if (gridTable[i][colNum]==player)
        count++;
    else
        count=0;

    if (count>=4)
        return 1;
} 
count=0;
// 4 in a row diagonally
for(int i=colNum+1,j=rowNum+1;i<maxRow && j<maxCol;i++,j++) 
{ 
    if(gridTable[j][i]!=player)
    {
        count=1;
        break;        
    }
    count++;
}
// 4 in a row diagonally
for(int i=colNum-1,j=rowNum-1;i>=0 && j>=0;i--,j--) 
{ 
    if(gridTable[j][i]!=player)
    {
        count=1;
        break;        
    }
    count++;
}
// 4 in a row diagonally
for(int i=colNum+1,j=rowNum-1;i<maxRow && j>=0;i++,j--) 
{ 
    if(gridTable[j][i]!=player)
    {
        count=1;
        break;        
    }
    count++;
}

for(int i=colNum-1,j=rowNum+1;i>=0 && j<maxCol;i--,j++) 
{ // 4 in a row diagonally
    if(gridTable[j][i]!=player)
    {
        count=1;
        break;        
    }
    count++;
}

if(count>=4)
    return 1;

return 0;
}

count is the variable that checks for a win if count is equal or more than 4 means they should be 4 or more consecutive tokens of the same player.

THE PROBLEM: sometimes the method checks for a win without being 4 tokens in order and other times does not check for a win when 4 tokens are in order.

11
  • 1
    so which line is the index bounds errors occuring on?
    – Marc B
    Sep 24, 2015 at 20:31
  • @MarcB this algorithm does NOT return any bound error, the issue is more of a logical mistake because sometimes doesn't return a win when 4 elements are in a row and sometimes it returns a win when less than 3 elements are in a row.
    – madeluccar
    Sep 24, 2015 at 20:32
  • Basically you have a 2D matrix, within which, you need to be able to start at a given point, and moving in a given direction, check to see if their are four matching elements. I did something like this for tic tac toe, but conceptually, it's the same thing Sep 24, 2015 at 20:41
  • @MadProgrammer I tried to do it like that, but then something happened when I had 3 tokens, a blank token and another token, and when I dropped the token that made 5 straight tokens it didn't return a win
    – madeluccar
    Sep 24, 2015 at 20:43
  • One problem I can see is, when you're checking a cell, you either increment the count or reset it to 0 and continue checking. You should probably break out of the loop instead and check the next direction instead (if you didn't find four matches). But then you start doing something different, in some case you don't even reset the count if the check fails ... Sep 24, 2015 at 20:44

6 Answers 6

26

For some reason I am not so fond of counters, so I did it this way (It works for boards with different sizes).

public boolean areFourConnected(int player){

    // horizontalCheck 
    for (int j = 0; j<getHeight()-3 ; j++ ){
        for (int i = 0; i<getWidth(); i++){
            if (this.board[i][j] == player && this.board[i][j+1] == player && this.board[i][j+2] == player && this.board[i][j+3] == player){
                return true;
            }           
        }
    }
    // verticalCheck
    for (int i = 0; i<getWidth()-3 ; i++ ){
        for (int j = 0; j<this.getHeight(); j++){
            if (this.board[i][j] == player && this.board[i+1][j] == player && this.board[i+2][j] == player && this.board[i+3][j] == player){
                return true;
            }           
        }
    }
    // ascendingDiagonalCheck 
    for (int i=3; i<getWidth(); i++){
        for (int j=0; j<getHeight()-3; j++){
            if (this.board[i][j] == player && this.board[i-1][j+1] == player && this.board[i-2][j+2] == player && this.board[i-3][j+3] == player)
                return true;
        }
    }
    // descendingDiagonalCheck
    for (int i=3; i<getWidth(); i++){
        for (int j=3; j<getHeight(); j++){
            if (this.board[i][j] == player && this.board[i-1][j-1] == player && this.board[i-2][j-2] == player && this.board[i-3][j-3] == player)
                return true;
        }
    }
    return false;
}
2
  • 4
    I like this solution because it's able to check an arbitrary board rather than needing to know what the last player's move was.
    – Stevoisiak
    Mar 12, 2017 at 16:54
  • Aren't ascendingDiagonal and descendingDiagonal? Other than that, finally a last-stone-independent solution!
    – techfly
    Dec 3, 2017 at 14:10
17

Looks like your code is correct for the horizontal and vertical cases. The tricky part is the diagonal case.

Let's try a picture:

red and green diagonal lines from left to right across the board

For the green lines, your starting row position is 0 ... maxRow - 4. The column would be 0 ... startingRow -

Pseudocode:

// top-left to bottom-right - green diagonals
for( rowStart = 0; rowStart < rowMax - 4; rowStart++){
    count = 0;
    int row, col;
    for( row = rowStart, col = 0; row < rowMax && col < colMax; row++, col++ ){
        if(gridTable[row][col] == player){
            count++;
            if(count >= 4) return 1;
        }
        else {
            count = 0;
        }
    }
}

// top-left to bottom-right - red diagonals
for( colStart = 1; colStart < colMax - 4; colStart++){
    count = 0;
    int row, col;
    for( row = 0, col = colStart; row < rowMax && col < colMax; row++, col++ ){
        if(gridTable[row][col] == player){
            count++;
            if(count >= 4) return 1;
        }
        else {
            count = 0;
        }
    }
}

You could do something similar for diagonals going the other way (from bottom-left to top-right).

3
  • could you help me with doing this from top right to bottom left or vice versa, I've been stuck for hours but don't want to create a new question when I've found this
    – DjoleRkc
    Apr 21, 2022 at 19:36
  • 1
    @DjoleRkc this isn't really the place for asking new questions, but I'll give you a hint. If you understand how to control the direction that a for loop traverses, you will have the answer. How could you change the inner loop here (col) to move down instead of up? What does "col++" do? What could you change "col++" to? If you change it, how would the starting point (col = colStart) and ending point (col < colMax) need to change?
    – GreenGiant
    Apr 25, 2022 at 15:45
  • the initial algorithm was good but I had a problem with memory deallocation which I didn't notice ... thanks for your answer nonetheless!
    – DjoleRkc
    Apr 26, 2022 at 16:08
6

So, having dug through your code, it would seem that the diagonal check can only win in a single direction (what happens if I add a token to the lowest row and lowest column?)

Instead, the basic check algorithm is always the same process, regardless of which direction you're checking in.

You need a start point (x/y) and x/y delta (direction of movement). You can summarise this down into a single method...

public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) {

    boolean win = true;
    for (int count = 0; count < 4; count++) {
        if (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) {
            int test = grid[row][col];
            if (test != check) {
                win = false;
                break;
            }
        }
        row += rowDelta;
        col += colDelta;
    }
    return win;

}

This will basically allow you to check in four directions, but also do them backwards

So, if we were to use something like...

int[][] gridTable = new int[ROWS][COLUMNS];

gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;

System.out.println("Vertical");

System.out.println(didWin(gridTable, 1, ROWS - 4, 3, 1, 0) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, ROWS - 1, 3, -1, 0) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 0, 3, 1, 0) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[3][1] = 1;
gridTable[3][2] = 1;
gridTable[3][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Horizontal");
System.out.println(didWin(gridTable, 1, 3, 1, 0, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4, 0, -1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 0, 0, 1) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[0][1] = 1;
gridTable[1][2] = 1;
gridTable[2][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Diag");
System.out.println(didWin(gridTable, 1, 0, 1, 1, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4, -1, -1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 1, 2, 1, 1) ? "Win" : "Lose");

Which outputs...

Vertical
Win
Win
Lose

Horizontal
Win
Win
Lose

Diag
Win
Win
Lose

Now, you could just summarise it down to...

public boolean didWin(int[][] grid, int check, int row, int col) {
    return didWin(grid, check, row, col, 1, 0) ||
                    didWin(grid, check, row, col, -1, 0) ||
                    didWin(grid, check, row, col, 0, 1) ||
                    didWin(grid, check, row, col, 0, -1) ||
                    didWin(grid, check, row, col, 1, 1) ||
                    didWin(grid, check, row, col, -1, -1) ||
                    didWin(grid, check, row, col, -1, 1) ||
                    didWin(grid, check, row, col, 1, -1);
}

So, using something like...

int[][] gridTable = new int[ROWS][COLUMNS];

gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;

System.out.println("Vertical");

System.out.println(didWin(gridTable, 1, ROWS - 1, 3) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, ROWS - 4, 3) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[3][1] = 1;
gridTable[3][2] = 1;
gridTable[3][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Horizontal");
System.out.println(didWin(gridTable, 1, 3, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[0][1] = 1;
gridTable[1][2] = 1;
gridTable[2][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Diag");
System.out.println(didWin(gridTable, 1, 0, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4) ? "Win" : "Lose");

Which prints out something like...

Vertical
Win
Win

Horizontal
Win
Win

Diag
Win
Win

I would add that this approach does only work if you provide the correct start of the 4 chips on a row. For example didWin(gridTable, 1, 3, 3) will provide false instead of true for your horizontal check, because the loop can only check one direction.

The intention wasn't to provide a "full fledged, out of the box" solution, but a concept from which a broader solution could be developed (I mean, I'd hate for people to actually have to think ;)). I also designed the solution based on the idea that the OP would know where the last piece was placed, ie, the starting point ;)

By modifying the didWin method ever so slightly, it's possible to check a n by n grid from any point...

public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) {
    boolean match = false;
    int matches = 0;
    while (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) {
        int test = grid[row][col];
        if (test != check && match) {
            break;
        } else if (test == check) {
            match = true;
            matches++;
        }
        row += rowDelta;
        col += colDelta;
    }
    return matches == 4;
}

So, I used...

public static final int ROWS = 8;
public static final int COLUMNS = 8;
//...
int[][] gridTable = new int[ROWS][COLUMNS];

gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;
for (int[] row : gridTable) {
    StringJoiner sj = new StringJoiner("|", "|", "|");
    for (int col : row) {
        sj.add(Integer.toString(col));
    }
    System.out.println(sj);
}
System.out.println(didWin(gridTable, 1, 3, 3));

and was able to get it to work. Sometimes an answer isn't a complete solution, but a seed for an idea which takes someone to a new place ;)

A further enhancement would include providing the number of expected conjoined pieces, but I'm pretty sure that's an enhancement I really don't need to demonstrate ;)

8
  • thank you very much. Is there any book you recommend me? OOP(?)
    – madeluccar
    Sep 24, 2015 at 22:30
  • Consider having a look at Effective Java Sep 24, 2015 at 23:27
  • This is a very robust idea that could be applied in many areas. Thanks for sharing this!
    – Slvrfn
    Mar 9, 2017 at 21:46
  • 1
    @Slvrfn It's a wonderful idea which could be applied to nxn matrix, could also be retrofitted into something like battleship :P Mar 9, 2017 at 21:47
  • I would add that this approach does only work if you provide the correct start of the 4 chips on a row. For example didWin(gridTable, 1, 3, 3) will provide false instead of true for your horizontal check, because the loop can only check one direction. Apr 19, 2017 at 12:30
1

I did my own version in the C language and I think that it's quite easy to reinterpret in another language.

//Return values: 1 for Player 1, 2 for Player 2 and 0 for a tie.
// '-' represents an empty tile, 'X' Player 1, 'O' Player 2
#include <stddef.h>

int connect4(char *game[], size_t columns, size_t lines)
{
    int winner = -1;
    for (size_t l = 0; l < lines; l++)
    {
        for (size_t c = 0; c < columns; c++)
        {
            char player = game[l][c];
            if (player == '-')
                continue;
            if (c + 3 < columns && player == game[l][c + 1]
                && player == game[l][c + 2] && player == game[l][c + 3])
                winner = winner < 0 ? player : 0;
            if (l + 3 < lines && player == game[l + 1][c]
                && player == game[l + 2][c] && player == game[l + 3][c])
                winner = winner < 0 ? player : 0;
            if (c + 3 < columns && l + 3 < lines && player == game[l + 1][c + 1]
                && player == game[l + 2][c + 2] && player == game[l + 3][c + 3])
                winner = winner < 0 ? player : 0;
            if (c >= 3 && l + 3 < lines && player == game[l + 1][c - 1]
                && player == game[l + 2][c - 2] && player == game[l + 3][c - 3])
                winner = winner < 0 ? player : 0;
        }
    }
    if (winner < 1)
        return 0;
    else
        return winner == 88 ? 1 : 2;
}
0
0

If someone still needs the solution, I write a function in c# and put in GitHub repo.

/// <summary>
    /// WinnerCalc check if blue or red win the game.
    /// </summary>
    /// <returns>Return 1 if 1 win and 2 if 2 win and -1 if no one win.</returns>
    /// <param name="matrix">2d array</param>
    /// <param name="lastRow">The row number.</param>
    /// <param name="lastColumn">The column number.</param>
    public static int WinnerCalc(int[,] matrix, int lastRow, int lastColumn)
    {
        int lastValue = matrix[lastRow, lastColumn];
        Console.WriteLine("drop in row: " + (lastRow) + " and column: " + (lastColumn) + " , the value is: " + lastValue);
        int rows = matrix.GetLength(0); //6
        int columns = matrix.GetLength(1); //7
        Console.WriteLine("number of rows is " + rows + ", and number of colums is " + columns);

        int numToWin = 4;
        int winner = -1;//is now one win tha game
        int match;

        match = 0;
        //check Horizontal
        for (int c = 0; c < columns; c++)
        {
            int currentValue = matrix[lastRow, c];
            if (currentValue == lastValue)
                match++;
            else match = 0;
            if(match == numToWin)
            {
                winner = lastValue;
                break;
            }
        }
        if (winner != -1)
        {
            Console.WriteLine("win Horizontal !");
            return winner;
        }

        match = 0;
        //check Vertical
        for (int r = 0; r < rows; r++)
        {
            int currentValue = matrix[r, lastColumn];
            if (currentValue == lastValue)
                match++;
            else match = 0;
            if (match == numToWin)
            {
                winner = lastValue;
                break;
            }
        }
        if (winner != -1)
        {
            Console.WriteLine("win Vertical !");
            return winner;
        }

        //check diagonal top-left to bottom-right - include middle
        match = 0;
        for (int r = 0; r <= rows - 4; r++)
        {
            int rowPosition = r;
            for (int column = 0; column < columns && rowPosition < rows; column++)
            {
                int currentValue = matrix[rowPosition, column];
                if (currentValue == lastValue)
                    match++;
                else match = 0;
                if (match == numToWin)
                {
                    winner = lastValue;
                    break;
                }
                rowPosition++;
            }
            if (winner != -1) break;
        }
        if (winner != -1)
        {
            Console.WriteLine("win Diagonal Top left! - include middle");
            return winner;
        }

        //check diagonal top-left to bottom-right - after middle
        match = 0;
        for (int c = 1; c <= columns - 4; c++)
        {
            int columnPosition = c;
            for (int row = 0; row < rows && columnPosition < columns; row++)
            {
                int currentValue = matrix[row, columnPosition];
                if (currentValue == lastValue)
                    match++;
                else match = 0;
                if (match == numToWin)
                {
                    winner = lastValue;
                    break;
                }
                columnPosition++;
            }
            if (winner != -1) break;
        }
        if (winner != -1)
        {
            Console.WriteLine("win Diagonal Top left! - after middle");
            return winner;
        }


        //check diagonal bottom-left to top-right - include middle
        match = 0;
        for (int r = rows - 1; r >= rows - 4; r--)
        {
            int rowPosition = r;
            for (int column = 0; column < columns && rowPosition < rows && rowPosition >= 0; column++)
            {
                int currentValue = matrix[rowPosition, column];
                if (currentValue == lastValue)
                    match++;
                else match = 0;
                if (match == numToWin)
                {
                    winner = lastValue;
                    break;
                }
                rowPosition--;
            }
            if (winner != -1) break;
        }
        if (winner != -1)
        {
            Console.WriteLine("win Diagonal Bottom left! - include middle");
            return winner;
        }


        //check diagonal bottom-left to top-right - after middle
        match = 0;
        for (int c = 1; c < columns; c++)
        {
            int columnPosition = c;
            for (int row = rows - 1; row < rows && columnPosition < columns && columnPosition >= 1; row--)
            {
                int currentValue = matrix[row, columnPosition];
                if (currentValue == lastValue)
                    match++;
                else match = 0;
                if (match == numToWin)
                {
                    winner = lastValue;
                    break;
                }
                columnPosition++;
            }
            if (winner != -1) break;
        }
        if (winner != -1)
        {
            Console.WriteLine("win Diagonal Bottom left! - after middle");
            return winner;
        }



        return winner; // no winner return -1
    }

}

and this is the repo: https://github.com/JoshK2/connect-four-winner

-14

this is what worked for me, it also did not take as long as it seems:
these are methods with row, column, diagonal, and anti-diagonal for x and o ;

public static void checkVertO(){
    if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' && board[3][0] == 'O' || board[1][0] == 'O' && board[2][0] == 'O' && board[3][0] == 'O' && board[4][0] == 'O' ||
    board[2][0] == 'O' && board[3][0] == 'O' && board[4][0] == 'O' && board[5][0] == 'O' || board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' && board[3][1] == 'O' || 
    board[1][1] == 'O' && board[2][1] == 'O' && board[3][1] == 'O' && board[4][1] == 'O' || board[2][1] == 'O' && board[3][1] == 'O' && board[4][1] == 'O' && board[5][1] == 'O' || 
    board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' && board[3][2] == 'O' || board[1][2] == 'O' && board[2][2] == 'O' && board[3][2] == 'O' && board[4][2] == 'O' ||
    board[2][2] == 'O' && board[3][2] == 'O' && board[4][2] == 'O' && board[5][2] == 'O' || board[0][3] == 'O' && board[1][3] == 'O' && board[2][3] == 'O' && board[3][3] == 'O' || 
    board[1][3] == 'O' && board[2][3] == 'O' && board[3][3] == 'O' && board[4][3] == 'O' || board[2][3] == 'O' && board[3][3] == 'O' && board[4][3] == 'O' && board[5][3] == 'O' ||
    board[0][4] == 'O' && board[1][4] == 'O' && board[2][4] == 'O' && board[3][4] == 'O' || board[1][4] == 'O' && board[2][4] == 'O' && board[3][4] == 'O' && board[4][4] == 'O' ||
    board[2][4] == 'O' && board[3][4] == 'O' && board[4][4] == 'O' && board[5][4] == 'O' || board[0][5] == 'O' && board[1][5] == 'O' && board[2][5] == 'O' && board[3][5] == 'O' || 
    board[1][5] == 'O' && board[2][5] == 'O' && board[3][5] == 'O' && board[4][5] == 'O' || board[2][5] == 'O' && board[3][5] == 'O' && board[4][5] == 'O' && board[5][5] == 'O' ||
    board[0][6] == 'O' && board[1][6] == 'O' && board[2][6] == 'O' && board[3][6] == 'O' || board[1][6] == 'O' && board[2][6] == 'O' && board[3][6] == 'O' && board[4][6] == 'O'||
    board[2][6] == 'O' && board[3][6] == 'O' && board[4][6] == 'O' && board[5][6] == 'O'){
        System.out.println("Game over, O won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkHorzO(){
    if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' && board[0][3] == 'O' || board[0][1] == 'O' && board[0][2] == 'O' && board[0][3] == 'O' && board[0][4] == 'O' ||
    board[0][2] == 'O' && board[0][3] == 'O' && board[0][4] == 'O' && board[0][5] == 'O' || board[0][3] == 'O' && board[0][4] == 'O' && board[0][5] == 'O' && board[0][6] == 'O' ||
    board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' && board[1][3] == 'O' || board[1][1] == 'O' && board[1][2] == 'O' && board[1][3] == 'O' && board[1][4] == 'O' ||
    board[1][2] == 'O' && board[1][3] == 'O' && board[1][4] == 'O' && board[1][5] == 'O' || board[1][3] == 'O' && board[1][4] == 'O' && board[1][5] == 'O' && board[1][6] == 'O' ||
    board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' && board[2][3] == 'O' || board[2][1] == 'O' && board[2][2] == 'O' && board[2][3] == 'O' && board[2][4] == 'O' ||
    board[2][2] == 'O' && board[2][3] == 'O' && board[2][4] == 'O' && board[2][5] == 'O' || board[2][3] == 'O' && board[2][4] == 'O' && board[2][5] == 'O' && board[2][6] == 'O' ||
    board[3][0] == 'O' && board[3][1] == 'O' && board[3][2] == 'O' && board[3][3] == 'O' || board[3][1] == 'O' && board[3][2] == 'O' && board[3][3] == 'O' && board[3][4] == 'O' ||
    board[3][2] == 'O' && board[3][3] == 'O' && board[3][4] == 'O' && board[3][5] == 'O' || board[3][3] == 'O' && board[3][4] == 'O' && board[3][5] == 'O' && board[3][6] == 'O' ||
    board[4][0] == 'O' && board[4][1] == 'O' && board[4][2] == 'O' && board[4][3] == 'O' || board[4][1] == 'O' && board[4][2] == 'O' && board[4][3] == 'O' && board[4][4] == 'O' ||
    board[4][2] == 'O' && board[4][3] == 'O' && board[4][4] == 'O' && board[4][5] == 'O' || board[4][3] == 'O' && board[4][4] == 'O' && board[4][5] == 'O' && board[4][6] == 'O' ||
    board[5][0] == 'O' && board[5][1] == 'O' && board[5][2] == 'O' && board[5][3] == 'O' || board[5][1] == 'O' && board[5][2] == 'O' && board[5][3] == 'O' && board[5][4] == 'O' ||
    board[5][2] == 'O' && board[5][3] == 'O' && board[5][4] == 'O' && board[5][5] == 'O' || board[5][3] == 'O' && board[5][4] == 'O' && board[5][5] == 'O' && board[5][6] == 'O' ){
        System.out.println("Game over, O won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkHorzX(){
    if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' && board[0][3] == 'X' || board[0][1] == 'X' && board[0][2] == 'X' && board[0][3] == 'X' && board[0][4] == 'X' ||
    board[0][2] == 'X' && board[0][3] == 'X' && board[0][4] == 'X' && board[0][5] == 'X' || board[0][3] == 'X' && board[0][4] == 'X' && board[0][5] == 'X' && board[0][6] == 'X' ||
    board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' && board[1][3] == 'X' || board[1][1] == 'X' && board[1][2] == 'X' && board[1][3] == 'X' && board[1][4] == 'X' ||
    board[1][2] == 'X' && board[1][3] == 'X' && board[1][4] == 'X' && board[1][5] == 'X' || board[1][3] == 'X' && board[1][4] == 'X' && board[1][5] == 'X' && board[1][6] == 'X' ||
    board[2][0] == 'X' && board[2][3] == 'X' && board[2][4] == 'X' && board[2][5] == 'X' || board[2][3] == 'X' && board[2][4] == 'X' && board[2][5] == 'X' && board[2][6] == 'X' ||
    board[3][0] == 'X' && board[3][1] == 'X' && board[3][2] == 'X' && board[3][3] == 'X' || board[3][1] == 'X' && board[3][2] == 'X' && board[3][3] == 'X' && board[3][4] == 'X' ||
    board[3][2] == 'X' && board[3][3] == 'X' && board[3][4] == 'X' && board[3][5] == 'X' || board[3][3] == 'X' && board[3][4] == 'X' && board[3][5] == 'X' && board[3][6] == 'X' ||
    board[4][0] == 'X' && board[4][3] == 'X' && board[4][4] == 'X' && board[4][5] == 'X' || board[4][3] == 'X' && board[4][4] == 'X' && board[4][5] == 'X' && board[4][6] == 'X' ||
    board[5][0] == 'X' && board[5][1] == 'X' && board[5][2] == 'X' && board[5][3] == 'X' || board[5][1] == 'X' && board[5][2] == 'X' && board[5][3] == 'X' && board[5][4] == 'X' ||
    board[5][2] == 'X' && board[5][3] == 'X' && board[5][4] == 'X' && board[5][5] == 'X' || board[5][3] == 'X' && board[5][4] == 'X' && board[5][5] == 'X' && board[5][6] == 'X' ){
        System.out.println("Game over, X won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkDiagX(){
    if (board[2][0] == 'X' && board[3][1] == 'X' && board[4][2] == 'X' && board[5][3] == 'X'|| board[1][0] == 'X' && board[2][1] == 'X' && board[3][2] == 'X' && board[4][3] == 'X'||
    board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' && board[3][3] == 'X'|| board[0][1] == 'X' && board[1][2] == 'X' && board[2][3] == 'X' && board[3][4] == 'X'||
    board[1][1] == 'X' && board[2][2] == 'X' && board[3][3] == 'X' && board[4][4] == 'X'|| board[2][1] == 'X' && board[3][2] == 'X' && board[4][3] == 'X' && board[5][4] == 'X'||
    board[0][2] == 'X' && board[1][3] == 'X' && board[2][4] == 'X' && board[3][5] == 'X'|| board[1][2] == 'X' && board[2][3] == 'X' && board[3][4] == 'X' && board[4][5] == 'X'||
    board[2][2] == 'X' && board[3][3] == 'X' && board[4][4] == 'X' && board[5][5] == 'X'|| board[0][3] == 'X' && board[1][4] == 'X' && board[2][5] == 'X' && board[3][6] == 'X'||
    board[1][3] == 'X' && board[2][4] == 'X' && board[3][5] == 'X' && board[4][6] == 'X'|| board[2][3] == 'X' && board[3][4] == 'X' && board[4][5] == 'X' && board[5][6] == 'X'){
        System.out.println("Game over, X won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkDiagO(){
    if (board[2][0] == 'O' && board[3][1] == 'O' && board[4][2] == 'O' && board[5][3] == 'O'|| board[1][0] == 'O' && board[2][1] == 'O' && board[3][2] == 'O' && board[4][3] == 'O'||
    board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' && board[3][3] == 'O'|| board[0][1] == 'O' && board[1][2] == 'O' && board[2][3] == 'O' && board[3][4] == 'O'||
    board[1][1] == 'O' && board[2][2] == 'O' && board[3][3] == 'O' && board[4][4] == 'O'|| board[2][1] == 'O' && board[3][2] == 'O' && board[4][3] == 'O' && board[5][4] == 'O'||
    board[0][2] == 'O' && board[1][3] == 'O' && board[2][4] == 'O' && board[3][5] == 'O'|| board[1][2] == 'O' && board[2][3] == 'O' && board[3][4] == 'O' && board[4][5] == 'O'||
    board[2][2] == 'O' && board[3][3] == 'O' && board[4][4] == 'O' && board[5][5] == 'O'|| board[0][3] == 'O' && board[1][4] == 'O' && board[2][5] == 'O' && board[3][6] == 'O'||
    board[1][3] == 'O' && board[2][4] == 'O' && board[3][5] == 'O' && board[4][6] == 'O'|| board[2][3] == 'O' && board[3][4] == 'O' && board[4][5] == 'O' && board[5][6] == 'O'){
        System.out.println("Game over, O won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkAntiDiagX(){
    if (board[3][0] == 'X' && board[2][1] == 'X' && board[1][2] == 'X' && board[0][3] == 'X'|| board[4][0] == 'X' && board[3][1] == 'X' && board[2][2] == 'X' && board[1][3] == 'X'||
    board[3][1] == 'X' && board[2][2] == 'X' && board[1][3] == 'X' && board[0][4] == 'X'|| board[5][0] == 'X' && board[4][1] == 'X' && board[3][2] == 'X' && board[2][3] == 'X'||        
    board[4][1] == 'X' && board[3][2] == 'X' && board[2][3] == 'X' && board[1][4] == 'X'|| board[3][2] == 'X' && board[2][2] == 'X' && board[1][4] == 'X' && board[0][5] == 'X'||
    board[5][1] == 'X' && board[4][2] == 'X' && board[3][3] == 'X' && board[2][4] == 'X'|| board[4][2] == 'X' && board[3][3] == 'X' && board[2][4] == 'X' && board[1][5] == 'X'||
    board[3][3] == 'X' && board[2][4] == 'X' && board[1][5] == 'X' && board[0][6] == 'X'|| board[5][2] == 'X' && board[4][3] == 'X' && board[3][4] == 'X' && board[2][5] == 'X'||
    board[4][3] == 'X' && board[3][4] == 'X' && board[2][5] == 'X' && board[1][6] == 'X'|| board[5][3] == 'X' && board[4][4] == 'X' && board[3][5] == 'X' && board[2][6] == 'X'){
        System.out.println("Game over, X won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

 public static void checkAntiDiagO(){
    if (board[3][0] == 'O' && board[2][1] == 'O' && board[1][2] == 'O' && board[0][3] == 'O'|| board[4][0] == 'O' && board[3][1] == 'O' && board[2][2] == 'O' && board[1][3] == 'O'||
    board[3][1] == 'O' && board[2][2] == 'O' && board[1][3] == 'O' && board[0][4] == 'O'|| board[5][0] == 'O' && board[4][1] == 'O' && board[3][2] == 'O' && board[2][3] == 'O'||        
    board[4][1] == 'O' && board[3][2] == 'O' && board[2][3] == 'O' && board[1][4] == 'O'|| board[3][2] == 'O' && board[2][2] == 'O' && board[1][4] == 'O' && board[0][5] == 'O'||
    board[5][1] == 'O' && board[4][2] == 'O' && board[3][3] == 'O' && board[2][4] == 'O'|| board[4][2] == 'O' && board[3][3] == 'O' && board[2][4] == 'O' && board[1][5] == 'O'||
    board[3][3] == 'O' && board[2][4] == 'O' && board[1][5] == 'O' && board[0][6] == 'O'|| board[5][2] == 'O' && board[4][3] == 'O' && board[3][4] == 'O' && board[2][5] == 'O'||
    board[4][3] == 'O' && board[3][4] == 'O' && board[2][5] == 'O' && board[1][6] == 'O'|| board[5][3] == 'O' && board[4][4] == 'O' && board[3][5] == 'O' && board[2][6] == 'O'){
        System.out.println("Game over, O won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}
1
  • this is really funny
    – dcts
    Jul 22, 2022 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.