I am trying to make a java minesweeper game. As you may know, if an empty cell is revealed in minesweeper, it reveals its surrounding cells. I believe making a recursive method should allow this feature to work. Here is my source code:

The minesX array contains the x positions of all the mines and minesY array for the y positions.Code Java:

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class GameClass extends JApplet implements ActionListener, MouseListener, MouseMotionListener{ Random r = new Random(); Timer t = new Timer(5, this); Graphics g2; Image offscreen; boolean inGame = true, mouse1 = false, mouse2 = false; int mouseX, mouseY; boolean flags[] = new boolean[100]; boolean blockers[] = new boolean[100]; int minesX[] = new int[10]; int minesY[] = new int[10]; public void init(){ t.start(); setSize(400, 400); addMouseListener(this); addMouseMotionListener(this); setFocusable(true); setup(); offscreen = createImage(400, 400); g2 = offscreen.getGraphics(); } public void update(Graphics g){ paint(g); } public void setup(){ setMines(); setBlockers(); setFlags(); } public void paint(Graphics g){ g2.clearRect(0, 0, 400, 400); g2.setColor(Color.BLACK); g2.fillRect(0, 0, 400, 400); if(inGame){ g2.setColor(Color.RED); for(int z = 0; z < 10; z++){ g2.fillRect(minesX[z], minesY[z], 40, 40); } for(int a = 0; a < 10; a++){ for(int b = 0; b < 10; b++){ if(!checkMine(a*40, b*40)){ g2.setColor(Color.WHITE); if(mines(a*40, b*40) != 0){ g2.drawString(""+mines(a*40, b*40), a*40+17, b*40+25); } } if(blockers[(b*10)+a]){ g2.setColor(Color.BLUE); g2.fillRect((a*40)+2, (b*40)+2, 36, 36); } if(flags[(b*10)+a]){ g2.setColor(Color.GREEN); g2.fillRect((a*40)+2, (b*40)+2, 36, 36); } g2.setColor(Color.WHITE); g2.fillRect(a*40, b*40, 40, 2); g2.fillRect(a*40, b*40, 2, 40); g2.fillRect((a*40)+38, b*40, 2, 40); g2.fillRect(a*40, (b*40)+38, 40, 2); } } } else{ g2.setColor(Color.WHITE); g2.drawString("GAME OVER", 0, 10); } g.drawImage(offscreen, 0, 0, this); } public void actionPerformed(ActionEvent e){ repaint(); } public int mines(int x, int y){ int a = 0; if(checkMine(x-40, y-40)){a++;} if(checkMine(x-40, y)){a++;} if(checkMine(x-40, y+40)){a++;} if(checkMine(x, y-40)){a++;} if(checkMine(x, y+40)){a++;} if(checkMine(x+40, y-40)){a++;} if(checkMine(x+40, y)){a++;} if(checkMine(x+40, y+40)){a++;} return a; } public void setMines(){ for(int z = 0; z < 10; z++){ minesX[z] = r.nextInt(10)*40; minesY[z] = r.nextInt(10)*40; for(int a = z-1; a >= 0; a--){ if(minesX[a] == minesX[z] && minesY[a] == minesY[z]){ setMines(); } } } } public void setBlockers(){ for(int z = 0; z < 100; z++){ blockers[z] = true; } } public void setFlags(){ for(int z = 0; z < 100; z++){ flags[z] = false; } } public void clearBlockers(int x, int y){ int c = (int) Math.floor(x); int d = (int) Math.floor(y); if(c < 9){ blockers[(d*10)+c+1] = false; if(mines(x+40, y) == 0){ clearBlockers(x+40, y); } } if(c > 0){ blockers[(d*10)+c-1] = false; if(mines(x+-40, y) == 0){ clearBlockers(x-40, y); } } if(d < 9){ blockers[(d*10)+c+10] = false; if(mines(x, y+40) == 0){ clearBlockers(x, y+40); } } if(d > 0){ blockers[(d*10)+c-10] = false; if(mines(x, y-40) == 0){ clearBlockers(x, y-40); } } if(c < 9 && d < 9){ blockers[(d*10)+c+11] = false; if(mines(x+40, y+40) == 0){ clearBlockers(x+40, y+40); } } if(c > 0 && d > 0){ blockers[(d*10)+c-11] = false; if(mines(x-40, y-40) == 0){ clearBlockers(x-40, y-40); } } if(c < 9 && d > 0){ blockers[(d*10)+c+9] = false; if(mines(x-40, y+40) == 0){ clearBlockers(x-40, y+40); } } if(c > 0 && d < 9){ blockers[(d*10)+c-9] = false; if(mines(x+40, y-40) == 0){ clearBlockers(x+40, y-40); } } } public boolean checkMine(int x, int y){ boolean a = false; for(int z = 0; z < 10; z++){ if(minesX[z] == x && minesY[z] == y){ a = true; } } return a; } public void mousePressed(MouseEvent e){ int code = e.getButton(); if(code == MouseEvent.BUTTON1){ mouse1 = true; } if(code == MouseEvent.BUTTON3){ mouse2 = true; } } public void mouseClicked(MouseEvent e){} public void mouseReleased(MouseEvent e){ int code = e.getButton(); if(code == MouseEvent.BUTTON1){ if(mouse1){ int a = (int) Math.floor(mouseX/40); int b = (int) Math.floor(mouseY/40); if(flags[(b*10)+a] == false){ blockers[(b*10)+a] = false; if(mines(a*40, b*40) == 0){ clearBlockers(mouseX, mouseY); } for(int z = 0; z < 10; z++){ if(minesX[z] == a*40 && minesY[z] == b*40){ inGame = false; } } } mouse1 = false; } } if(code == MouseEvent.BUTTON3){ int a = (int) Math.floor(mouseX/40); int b = (int) Math.floor(mouseY/40); if(flags[(b*10)+a]){ flags[(b*10)+a] = false; } else if(blockers[(b*10)+a]){ flags[(b*10)+a] = true; } mouse2 = false; } } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseMoved(MouseEvent e){ mouseX = e.getX(); mouseY = e.getY(); } public void mouseDragged(MouseEvent e){} }

The blockers array contains the information for whether each cell is revealed or not.

The flags array contains the information for whether each cell is flagged or not.

The mouseX and mouseY are the mouse's x and y positions.

The mouse1 indicates whether the left mouse button is pressed or not, and mouse2 for the right button.

The mines method returns how many mines surround a specific cell which requires its x and y coordinates.

The setMines method randomly places 10 mines on the board. It is recursive in case two mines end up spawning in the same cell.

The setBlockers method makes all the cells be not revealed.

The setFlags method makes all the cells not flagged.

The checkMine method checks if given x and y coordinates are where a mine is placed.

The clearBlockers method is what I'm trying to fix. It clears surrounding cells and checks if they are empty. If they are, it recalls the method with the new cell's coordinates. It is not working right now and I can't figure out why.

By the way, the cells are numbered in the blockers and flags arrays like so:

0 1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19

and so on until it reaches 99.

The x and y positions can be converted to these numbers like so if a is the number and x and y are the coordinates:

a = (y*10)+x

I thought all this information may be asked about due to the lack of comments in my code. So far I also do not have any way for the player to win or reset the game. I really hope someone can fix this and whoever can, thank you.