#Connect four, struggling with diagonal win check

1 messages · Page 1 of 1 (latest)

edgy cliff
#

The current code is supposed to check for the top left to the bottom right (and the other way around).

    private boolean checkDiagonal() {
        int leftRightCount = 0;

        for (int row = 0; row < HEIGHT; row++) {
            for (int col = 0; col < WIDTH; col++) {
                if (BOARD[row][col].getOwningPlayer() == GAME_STATE.getCurrentPlayer()) {
                    leftRightCount++;
                    
                    if (leftRightCount == 4) {
                        return true;
                    }
                    break;
                } else {
                    leftRightCount = 0;
                }
//                col += row;
            }
        }
        return false;
    }

These are the indices that need to be checked

row,col
0,0
1,1
2,2
3,3
4,4
soft bladeBOT
#

<@&987246399047479336> please have a look, thanks.

soft bladeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

stoic helm
#

hmm

#

how big is the board

edgy cliff
#

It should work with any size, so it needs to be dynamic

strong hamlet
#

@edgy cliff is the board always a square or it can be a rectangle?

edgy cliff
#

Only a square

strong hamlet
#

then just loop through one side and use matrix[i][i]

#

say ur matrix is 4x4

#

then it'll loop through
marix[0][0]
martix[1][1]
matrix[2][2]

#

...

edgy cliff
#

Right, not sure how I didn't think of that

#

What if the board could also be a rectangle? I would need to increment row and col after checking each diagonal position on the board, which is what I tried above but I can't figure it out.

strong hamlet
#

yea i wonder how, while ur name is "DSA"

edgy cliff
#

Haha, doesn't stand for what you think it is

strong hamlet
#

give me a min to think about it

#

@edgy cliff u can't do it on a rectangle

#

theres no way

#

it is not physically possible

edgy cliff
#

You're right lol. Will just implement the suggestion with the single loop

marble loom
#

extract it to a single method and feed it its coordinates to check 😉

sterile orbit
strong hamlet
sterile orbit
#

You can have a rectangular connect 4 board and still have combinations that would let you get a diagonal 4 in a row

#

From any given point, if there are 4 pieces to the diagonal, it's a win. In code, if we reach the edge of the board but have no connect 4, just break out of the loop

#

Same logic as the square varient but make sure you don't go outside of the array bounds in a specific column, or only check 4x4 squared regions within a rectangular area

strong hamlet
#

we are talking about diagonal of shapes here