Problems with AI battleship bot implementation

I need help with my battleship AI code that keeps crashing after a few rounds

I’m building an AI bot to play against my instructor’s battleship game. The problem happens when I use a 2D boolean array to track which spots I’ve already fired at. After the program runs automatically for just a few turns, it stops working and I have to restart it manually.

I think there might be some kind of memory issue or array bounds problem but I can’t figure out what’s wrong. Here’s my code:

public class BattleshipBot {
    public String botName = "BattleshipBot";
    // tracking array for fired positions
    public boolean[][] fired = new boolean[10][10];
    
    @Override
    public void execute() {
        while (match.isActive()) {
            int x = (int) (Math.random() * 10);
            int y = (int) (Math.random() * 10);
            boolean success;
            
            // fire at checkerboard pattern positions
            if (x % 2 == 0 && y % 2 != 0 && fired[x][y] == false) {
                success = match.fire(x, y);
                fired[x][y] = true;
                
                if (success) {
                    searchUp(x, y, success);
                    searchDown(x, y, success);
                    searchRight(x, y, success);
                    searchLeft(x, y, success);
                }
            } else if (x % 2 != 0 && y % 2 == 0 && fired[x][y] == false) {
                success = match.fire(x, y);
                fired[x][y] = true;
                
                if (success) {
                    searchUp(x, y, success);
                    searchDown(x, y, success);
                    searchRight(x, y, success);
                    searchLeft(x, y, success);
                }
            }
        }
    }
    
    public boolean searchUp(int x, int y, boolean success) {
        try {
            y--;
            while (success) {
                success = match.fire(x, y--);
                fired[x][y] = true;
            }
            return success;
        } catch (Exception ex) {
            return false;
        }
    }
    
    public boolean searchDown(int x, int y, boolean success) {
        try {
            y++;
            while (success) {
                success = match.fire(x, y++);
                fired[x][y] = true;
            }
            return success;
        } catch (Exception ex) {
            return false;
        }
    }
}

Any ideas what could be causing this to freeze up?

Your search methods have an indexing bug that’s crashing your program. Both searchUp and searchDown use post-decrement and post-increment operators incorrectly. In searchUp, you decrement y first, then you call match.fire(x, y--) and fired[x][y] = true in the loop. After match.fire(x, y--) runs, y gets decremented again, but fired[x][y] still uses the old value, causing you to mark the wrong spot as fired. searchDown has the same issue. Moreover, you’re not checking bounds before accessing the array, which will throw an ArrayIndexOutOfBoundsException when you exceed the 10x10 grid. I encountered this precise problem while building my first battleship bot. Address it by adding explicit bounds checks and refining the indexing logic in your search methods.