0

I'm trying to check if a player won diagonally (going from the top left to bottom right). The top left portion of the grid is (0,0). I'm not checking if a player won based on the last tile placed. Instead, I'm checking all the spots on the grid to check if someone won diagonally. This is what I came up with.

public class TestWin {
private int count_piece;
private char[][] board;

public void myBoard(char[][] x){
    this.board = x;
    //vertWin();
    //horizontalWin();
    diagDownWin();
}

public boolean diagDownWin(){
    for(int row = 0; row < board.length; row++){
        for(int col = 0; col < board[0].length; col++){
            if(board[row][col] == board[row+1][col+1]){
                count_piece++;
                if(count_piece >= 4){
                    System.out.println("You win");
                    return true;
                }
            }else{
                count_piece = 0;
            }
        }
    }
    System.out.println("No win");
    return false;
}
public static void main(String[] args) {

    char[][] myGrid = { 
            { 'X', 'O', 'O', 'O'}, 
            { 'X', 'X', 'O', 'O'}, 
            { 'O', 'X', 'X', 'O'},
            { 'O', 'O', 'X', 'X'},
            { 'O', 'O', 'O', 'O'}
    };

    TestWin m1 = new TestWin();

    m1.myBoard(myGrid);

}

}

Because there are two ways to win from a diagonal-- I'm checking moving from top-left to bottom-right. I want to go down one and over one (row + 1 and col +1), but I run into an array out of bounds error on line where I write:

if(board[row][col] == board[row+1][col+1])

I assume this happens because when I reach the last row and last col I'm adding one which is greater the board.length and board[0].length

What can I do to prevent the error?

2 Answers2

1

In your loops, only iterate over the number of positions that could validly be the top-left position of a down-and-right four-in-a-row (so, for your example grid, the position (0, 0) and (0, 1)). Then, evaluate if a four-in-a-row occurred from that point. Something along the lines of:

for(int row = 0; row < grid .length - 3; row++){
    for(int col = 0; col < grid [row].length - 3; col++){
        if((grid [row][col] == grid [row+1][col+1]) &&
                (grid [row+1][col+1] == grid [row+2][col+2]) &&
                (grid [row+2][col+2] == grid [row+3][col+3])){
            System.out.println("Someone won down-right!");
        }
    }
}

This is relatively brute force, but should serve for simple purposes.

Note that, since you aren't iterating diagonally in your proposed example (rather, by columns in a row, then by row), your count_piece will not perform as expected. If you insist on iterating:

for(int row = 0; row < grid .length - 3; row++){
    for(int col = 0; col < grid [row].length - 3; col++){
        int solution = true;
        for (int index = 0; index <= 2; ++index) {
            int rowIndex = row + index;
            int colIndex = col + index;
            if (grid [rowIndex][colIndex] != grid [rowIndex+1][colIndex+1]) {
                solution = false;
                break;
            }
        }
        if (solution) {
            System.out.println("Someone won down-right!");
        }
    }
}

Seems unnecessary though.

Ironcache
  • 1,719
  • 21
  • 33
0

I would modify the bounds of the row, col loop to

for(int row = 0; row < board.length-1; row++){
    for(int col = 0; col < board[0].length-1; col++){

and subsequently modify the success condition to

                if(count_piece >= 3){

You may then need to manually check the values at the "edges" afterwards.

  • Tried this but, .length will count from 1 - whatever the number of rows are and arrays are indexed by 0, so this doesn't help. – Construct0r Nov 30 '16 at 22:19
  • I'm not sure why that matters. If you iterate from 0 to length-1, the iteration will stop at index 2, and check index 3, rather than stopping at index 3, and overflowing into index 4, which isn't defined. – Nathan Whitney Nov 30 '16 at 22:38