Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5990923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:18:49+00:00 2026-05-22T23:18:49+00:00

I am creating a mine sweeper program in java for school, and am having

  • 0

I am creating a mine sweeper program in java for school, and am having trouble with the clearing of squares that don’t have any mines next to them, the square is supposed to be disabled, and all surrounding squares revealed, if there is another square that is touching no bombs, it will perform the same operation. I am getting a stack overflow error, I know this has to do with an infinite loop, but I can’t find where it is getting stuck in my code.

I have no idea what’s going wrong so any suggestions would be helpful.

Here is the relative piece of code (the if statements are for special cases, if the square clicked is on the edge of the board)

 private void doClear(int y, int x, JButton[][] bArray2, int gridy,int gridx)
{
    clicked--;
    bArray2[y][x].setBackground(lighterGray);
    bArray2[y][x].setEnabled(false);

    if (x > 0 && x<gridx-1 && y> 0 && y<gridy-1)
    {
        clearSquare(y-1, x-1,gridy, gridx, bArray2);
        clearSquare(y-1, x,gridy, gridx, bArray2);
        clearSquare(y-1, x+1,gridy, gridx, bArray2);
        clearSquare(y, x-1,gridy, gridx, bArray2);
        clearSquare(y, x+1,gridy, gridx, bArray2);
        clearSquare(y+1, x-1,gridy, gridx, bArray2);
        clearSquare(y+1, x,gridy, gridx, bArray2);
        clearSquare(y+1, x+1,gridy, gridx, bArray2);
    }
     if(y == 0 && x != 0 && x != gridx-1)  // top row check
                       {
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                           clearSquare(y, x+1,gridy, gridx, bArray2);
                           clearSquare(y+1, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);
                           clearSquare(y+1, x+1,gridy, gridx, bArray2);

                       } // ends top row check

                        if (y == 0 && x == 0) // corner check top left
                        {
                           clearSquare(y, x+1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);
                           clearSquare(y+1, x+1,gridy, gridx, bArray2);
                        } // ends top left corner check

                        if (y == 0 && x == gridx-1) // corner check top right row
                         {     
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);                               
                        } // ends top right corner check

                        if (x == 0 && y != 0 && y != gridy-1)  //left column check
                        {
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y-1, x+1,gridy, gridx, bArray2);
                           clearSquare(y, x+1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);
                           clearSquare(y+1, x+1,gridy, gridx, bArray2);  
                            } // ends left column check

                            if (x == gridx-1 && y != 0 && y != gridy-1)  // right column check
                            {
                           clearSquare(y-1, x-1,gridy, gridx, bArray2);
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x-1,gridy, gridx, bArray2);
                           clearSquare(y+1, x,gridy, gridx, bArray2);
                            }// ends right column check

                       if(y == gridy-1 && x != 0 && x != gridx-1)
                       {
                           clearSquare(y-1, x-1,gridy, gridx, bArray2);
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y-1, x+1,gridy, gridx, bArray2);
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                           clearSquare(y, x+1,gridy, gridx, bArray2);
                       } // ends bottom row check

                       if (y == gridy-1 && x == 0)  // left bottom corner check
                       {
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y-1, x+1,gridy, gridx, bArray2);
                           clearSquare(y, x+1,gridy, gridx, bArray2);

                        } // ends left bottom corner check

                        if (y == gridy-1 && x == gridx-1) // right bottom corner check
                        {
                           clearSquare(y-1, x-1,gridy, gridx, bArray2);
                           clearSquare(y-1, x,gridy, gridx, bArray2);
                           clearSquare(y, x-1,gridy, gridx, bArray2);
                        }//ends right bottom corner check
}

private void clearSquare(int y,int x, int gridy, int gridx, JButton[][] bArray2)
{
        int value = array[y][x];
        System.out.println(value);
        String text = bArray2[y][x].getText();
        if (text == "")
        {
        if (value == 0)
        {
            doClear(y, x, bArray2, gridy, gridx);
        }
        else{
            clicked--;
            bArray2[y][x].setText(""+value);
           }
       }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:18:49+00:00Added an answer on May 22, 2026 at 11:18 pm
    private void doClear(int y, int x, JButton[][] bArray2, int gridy,int gridx)
    {
        if (...already cleared...) {
           return;
        }
    
        ...
    }
    

    Without that check, cell A will clear neighbour cell B, which will clear neighbour cell A, which will clear neighbour cell B, which …

    The code you posted can be replaced with the following:

    private void handleClick(JButton[][] bGrid, int gridy, int gridx, int y, int x, bool realClick) {
        if (x < 0 || x >= gridx || y < 0 || y >= gridx) {
            return;
        }
    
        JButton button = bGrid[y][x];
        if (!button.isEnabled()) {
            return;
        }
    
        if (realClick) {
            --clicked;
        }
    
        button.setBackground(lighterGray);
        button.setEnabled(false);
    
        if (...is a mine...) {
            ...
        } else {
            button.setText(array[y][x]);
    
            if (value == 0) {
                handleClick(bGrid, gridy, gridx, y-1, x-1, false);
                handleClick(bGrid, gridy, gridx, y-1, x,   false);
                handleClick(bGrid, gridy, gridx, y-1, x+1, false);
                handleClick(bGrid, gridy, gridx, y,   x-1, false);
                handleClick(bGrid, gridy, gridx, y,   x+1, false);
                handleClick(bGrid, gridy, gridx, y+1, x-1, false);
                handleClick(bGrid, gridy, gridx, y+1, x,   false);
                handleClick(bGrid, gridy, gridx, y+1, x+1, false);
            }
        }
    }
    

    “array” needs to be renamed to something sensical!

    Update: Added simplified code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stumbled upon a problem when creating a program of mine. I have
In the process of creating an auto-updater for my program, and I'm having trouble
Before starting, note that I have to update a website that is not mine,
I have this app of mine that contains a section that will allow the
Most often when creating multiple classes inside a program that use each other, I
So I am creating a program that changes the JTextField depending on what the
I'm creating an Updater program in C# for my PC game that basically sends
I am creating and application that need to save images. I don't want to
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a JApplet I have 2 Text Fields, a button and a Text Area.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.