Search code examples
javaminesweeper

minesweeper java


I am working on a minesweeper program in Java. I have my bombs distributed throughout the field, and I have my actionlisteners responding to clicks and mouselistener, responding to right clicks. I also have each square that is clicked check to see how many bombs are adjacent to it and print the number on the square just like in the game.

The only part I don't understand is how minesweeper opens up field when clicking a square whether it be a number or a blank square. Please help me understand how this works.


Solution

  • The only part I don't understand is how minesweeper opens up field when clicking a square whether it be a number or a blank square.

    If any of its neighboring squares has a mine, it will show a number with the number of mines around it.

    It's blank if there are no mines around it (ie: it would show the number 0 if it had to). When it's blank it also recursively opens all its neighbors (eg: opens all neighbors and their neighbors if they are blank too, and so forth).

    And if it's a mine you lose of course. An example:

    X 2 . .
    X 2 . .
    2 2 1 .
    1 X 1 .
    

    (let X denote a mine).

    If you open any of the squares with marked as . (blank), automatically expand all of them and the numbers next to them:

    - 2 . .
    - 2 . .
    - - 1 .
    - - 1 .
    

    (let - denote a hidden square).