Hello

Could anyone help us , we have a minesweeper's game code in java the game is good but has two problem

the first one is when you press a mine button the game should be over and you can not press on any other button but our code allow you to continue even if you loss

the second problem is the smiley face button which allow you to reset the game only once

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.util.Random;
import java.util.TimerTask;


public class MinesweeperNew2 extends JFrame implements MouseListener
{
	JButton table[] [];      //Array of Buttons
    boolean bomb[] [];      //array true if bomb is present on button [x][y]
    boolean flag[] [];      //array true if flag is present at button [x][y]
    boolean exposed[] [];   //used for exposing 0's. If true then a 0 has been exposed
    boolean checkwinbool[] [];  // if [x][y] = true then the button has a number on it or it is a bomb (used for checking if game is over)
    int count = 0, bombsremaining;          //counting the number of bombs placed
    String surbombs;        //number of bombs surrounding button [x][y] (is a string so that we can setLabel for the button)
    int randx, randy;       //random ints for bombs
    int row = 9, col = 9, numbombs = 10; //number of rows columns, and bombs
    int sizex = 450, sizey = 400;
    Font font;
    JPanel pane_up,pane_but,pane_all;
    JButton smile;
    Icon smile_pic=new ImageIcon("smile.gif");
    JLabel Bombs, TimeRemaning,empty1,empty2;
    Random but_randx=new Random();
    Random but_randy=new Random();
    public TimerDisplay t;
    GridLayout	g1= new GridLayout (row, col);
    
    public MinesweeperNew2()
    {
    	super("Minesweeper Game");

  		pane_but=new JPanel(g1);
		pane_up =new JPanel();
		pane_up.setLayout(new FlowLayout());
		bombsremaining = numbombs;
		Bombs = new JLabel (Integer.toString (bombsremaining));
		smile=new JButton(smile_pic);
		smile.addMouseListener (this);
		TimeRemaning = new JLabel ();
		empty1 = new JLabel ("              ");
		empty2 = new JLabel ("              ");
		pane_up.add(Bombs);
		pane_up.add(empty1);
		pane_up.add(smile);
		pane_up.add(empty2);
		pane_up.add(TimeRemaning);
		font = new Font ("ComicSans", Font.BOLD, 40);
		setFont (font);
		table = new JButton [row] [col];
		bomb = new boolean [row] [col];
		flag = new boolean [row] [col];
		exposed = new boolean [row] [col];
		checkwinbool = new boolean [row] [col];
		

		for (int x = 0 ; x < row ; x++)
		{
		    for (int y = 0 ; y <col ; y++)
		    {
			table [x] [y] = new JButton ();
			table [x] [y].addMouseListener (this);
			pane_but.add (table [x] [y]);
		    }
    	}
    	
    	getContentPane().add (pane_up, "North");
		getContentPane().add (pane_but, "Center");

		pane_up.setBackground(Color.lightGray);
		Bombs.setForeground(Color.white);
		TimeRemaning.setForeground(Color.white);
		Restart_Game (row, col, numbombs,sizex, sizey);
		
	
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(sizex,sizey);
		setVisible(true);
		setLocation(200,100);
		setResizable (false);
		
			t = new TimerDisplay();
				t.start();
	}
	
   
    public void Restart_Game (int row, int col, int numbombs,int sizex, int sizey)
    {
		setSize (sizex, sizey);
		invalidate();
		validate();
		g1.setRows(row);
		g1.setColumns(col);
		int count = 0;
		bombsremaining = numbombs;
		Bombs.setText (Integer.toString (bombsremaining));
		for (int x = 0 ; x < row ; x++)
		{
		    for (int y = 0 ; y < col ; y++)
		    {
			pane_but.remove (table [x] [y]);
		    }
		}
		for (int x = 0 ; x < row ; x++)
		{
		    for (int y = 0 ; y < col ; y++)
		    {
	
			table [x] [y].setEnabled (true);
			table [x] [y].setLabel ("");
			table [x] [y].setBackground (Color.gray);
			table [x] [y].setForeground (Color.white);
			pane_but.add (table [x] [y]);
			bomb [x] [y] = false;
			flag [x] [y] = false;
			exposed [x] [y] = false;
			checkwinbool [x] [y] = false;
		    }
		}
		setSize (sizex, sizey);
		invalidate();
		validate();
		//adds the bombs to random places on the grid
		while (count < numbombs)
		{
		    randx = but_randx.nextInt(row);
		    randy = but_randx.nextInt(col);
		    if (bomb [randx] [randy] == false)
		    {
			bomb [randx] [randy] = true;
			checkwinbool [randx] [randy] = true;
			count++;
		    }
		}
    }

    public void mouseClicked (MouseEvent e)
    {
		boolean gameover = false;
		if (e.getSource () == smile)
		{
		    Restart_Game (row, col, numbombs, sizex, sizey);
		    t.reset();
		    t.start(); 
		}
	
		for (int x = 0 ; x < row ; x++)
		{
		    for (int y = 0 ; y < col ; y++)
		    {
			if (e.getSource () == table [x] [y])
			{
			//if left click, and there is no flag on the button
			    if (e.getButton () == e.BUTTON1 && flag [x] [y] == false) 
			    {
	if (bomb [x] [y] == true)  // if you you click on a bomb, results in game over
				{
				    table [x] [y].setLabel ("*");
				    gameover ();
				    table [x] [y].setBackground (Color.black);
				    table [x] [y].setEnabled(false);
				    gameover = true;
				    break;
	
				}
				exposed [x] [y] = true;
				checkwinbool [x] [y] = true; // these set to true mean that the button has been clicked
				surbombs = Integer.toString (surroundingBombs (x, y)); //gets the number of surrounding buttons with bombs and sets it to a string so that it is possible to setLabel
				table [x] [y].setLabel (surbombs); // sets the label to be the number of bombs in the 8 surrounding buttons
				if (surroundingBombs (x, y) == 0)
				{
				    check (x, y);          //calls a recursive method so that if a "0" is there the surrounding 8 buttins must be exposed and if one of those is "0" it too must be exposed and so on
				}
			    }
			    // if it is a right click
			    else if (e.getButton () == e.BUTTON3)  
			    {
	  //if there is a flag (second press)already present set it so that there is no flag
				if (flag [x] [y] == true) 
				{
				    table [x] [y].setLabel ("");
				    table [x] [y].setForeground (Color.white);
				    flag [x] [y] = false;
				    checkwinbool [x] [y] = false;
				    bombsremaining++;
				}
	//(first time assign buton to flage)if there is no flag, set it so there is a flag
				else if (checkwinbool [x] [y] == false || bomb [x] [y] == true) 
				{
				    table [x] [y].setLabel ("|>");
				    table [x] [y].setForeground (Color.red);
				    flag [x] [y] = true;
				    checkwinbool [x] [y] = true;
				    bombsremaining--;
				}
				Bombs.setText (Integer.toString (bombsremaining));
	
			    }
			    //if both left and right click at the same time
			    else if (e.getButton () == e.BUTTON2 && flag [x] [y] == false && checkwinbool [x] [y] == true && bomb [x] [y] == false) 
			    { 
			   //only executes if there is no flag, it has been exposed, and no bomb
				if (flags (x, y) == surroundingBombs (x, y)) //checks that the number of flags around [x][y] = the number of bombs around [x][y]
				{
				    for (int q = x - 1 ; q <= x + 1 ; q++)
				    {
					for (int w = y - 1 ; w <= y + 1 ; w++)
					{
					    if (q < 0 || w < 0 || q >= row || w >= col) // makes sure that it wont have an error for buttons next to the wall
						break;
					    if (bomb [q] [w] == false && flag [q] [w] == true) //if there is no bomb, but there is a flag its game over
					    {
						gameover ();
						gameover = true;
						break;
					    }
					}
				    }
				    if (gameover == false)
				    {
					expose (x, y);     //eposes the surrounding 8 buttons
					check (x, y);      //checks if any of those are "0" and calls the recursive method
				    }
				}
			    }
			    if (gameover == false) //this just calls the method for changing the colours of the buttons if they have been clicked
				clicked ();
			}
	
		    }
		}
		checkwin ();
	    }
	    
	        public void clicked ()     //changes the color of the buttons and if [x][y] is "0" set the label to nothing
    {
	for (int x = 0 ; x < row ; x++)
	{
	    for (int y = 0 ; y < col ; y++)
	    {
		if (checkwinbool [x] [y] == true && flag [x] [y] == false && bomb [x] [y] == false)
		    table [x] [y].setBackground (Color.darkGray);
		if (flag [x] [y] == false && surroundingBombs (x, y) == 0)
		    table [x] [y].setLabel ("");
	    }
	}
    }


    public int flags (int x, int y)  // get the number of surrounding flags
    {
	int surFlags = 0;
	for (int q = x - 1 ; q <= x + 1 ; q++)
	{
	    for (int w = y - 1 ; w <= y + 1 ; w++)
	    {
		while (true)
		{
		    if (q < 0 || w < 0 || q >= row || w >= col) // makes sure that it wont have an error for buttons next to the wall
			break;

		    if (flag [q] [w] == true)
		    {
			surFlags++;
		    }
		    break;
		}
	    }
	}
	return surFlags;
    }


    public int surroundingBombs (int x, int y)  // checks surrounding 8 squares for number of bombs (it does include itself, but has already been checked for a bomb so it won't matter)
    {
	int surBombs = 0;
	for (int q = x - 1 ; q <= x + 1 ; q++)
	{
	    for (int w = y - 1 ; w <= y + 1 ; w++)
	    {
		while (true)
		{
		    if (q < 0 || w < 0 || q >= row || w >= col) // makes sure that it wont have an error for buttons next to the wall
			break;
		    if (bomb [q] [w] == true)
			surBombs++;
		    break;
		}
	    }
	}
	return surBombs;
    }


    public void expose (int x, int y)  // exposes the surrounding 8 buttons
    {
	String surrbombs;
	exposed [x] [y] = true;
	for (int q = x - 1 ; q <= x + 1 ; q++)
	{
	    for (int w = y - 1 ; w <= y + 1 ; w++)
	    {
		while (true)
		{
		    if (q < 0 || w < 0 || q >= row || w >= col) // makes sure that it wont have an error for buttons next to the wall
			break;
		    if (flag [q] [w] == true)
			break;

		    checkwinbool [q] [w] = true;
		    surrbombs = Integer.toString (surroundingBombs (q, w));
		    table [q] [w].setLabel (surrbombs);
		    		    table [q] [w].setEnabled(false);
		    break;

		}
	    }
	}
    }


    public void surr (int x, int y)  //this is what checks if a surrounding button has "0" is so expose it and check around the exposed buttons until there is no more "0"'s
    {
	String surrbombs;
	for (int q = x - 1 ; q <= x + 1 ; q++)
	{
	    for (int w = y - 1 ; w <= y + 1 ; w++)
	    {
		while (true)
		{
		    if (q < 0 || w < 0 || q >= row || w >= col) // makes sure that it wont have an error for buttons next to the wall
			break;
		    if (flag [q] [w] == true)
			break;
		    if (exposed [q] [w] == false && surroundingBombs (q, w) == 0)
		    {
			expose (q, w);
			check (q, w);
		    }
		    break;
		}
	    }
	}
    }


    public void check (int x, int y)  //calls the surr method but is neccesary because of the expose first
    {
	expose (x, y);
	surr (x, y);
    }


    public void checkwin ()    //checks if all the button without bombs have been pressed
    {
	boolean allexposed = true;
	for (int x = 0 ; x < row ; x++)
	{
	    for (int y = 0 ; y < col ; y++)
	    {
		if (flag [x] [y] == true && bomb [x] [y] == false)
		    allexposed = false;
		if (checkwinbool [x] [y] == false)
		{
		    allexposed = false;
		    break;
		}
	    }
	}
/*	if (allexposed != false)
	{
	    gameover ();
	    int x2 = (int) col / 2;
	    int y2 = (int) row / 2;
	    table [y2] [x2 - 4].setLabel ("Y");
	    table [y2] [x2 - 3].setLabel ("O");
	    table [y2] [x2 - 2].setLabel ("U");
	    table [y2] [x2 - 1].setLabel ("");
	    table [y2] [x2].setLabel ("W");
	    table [y2] [x2 + 1].setLabel ("I");
	    table [y2] [x2 + 2].setLabel ("N");
	    table [y2] [x2 + 3].setLabel ("!");
	    table [y2] [x2 + 4].setLabel ("!");
	    for (int i = -4 ; i < 5 ; i++)
	    {
		table [y2] [x2 + i].setBackground (Color.black);
		table [y2] [x2 + i].setForeground (Color.white);
	    }
	}*/
    }

// is called if bomb is clicked or on the double click if flag is not on a bomb
    public void gameover ()  
    {
	for (int x = 0 ; x < row ; x++)
	{
	    for (int y = 0 ; y < col ; y++)
	    {
		if (bomb [x] [y] == true)
		{
		    table [x] [y].setLabel ("*"); //exposes all bombs
		    table [x] [y].setBackground (Color.red);
		}
		table [x] [y].setEnabled (false); //disable all buttons
	    }
	}
	t.stop();
/*	int x2 = (int) col / 2;
	int y2 = (int) row / 2;
	table [y2] [x2 - 4].setLabel ("Y");
	table [y2] [x2 - 3].setLabel ("O");
	table [y2] [x2 - 2].setLabel ("U");
	table [y2] [x2 - 1].setLabel ("");
	table [y2] [x2].setLabel ("L");
	table [y2] [x2 + 1].setLabel ("O");
	table [y2] [x2 + 2].setLabel ("S");
	table [y2] [x2 + 3].setLabel ("E");
	table [y2] [x2 + 4].setLabel ("!");
	for (int i = -4 ; i < 5 ; i++)
	{
	    table [y2] [x2 + i].setBackground (Color.black);
	    table [y2] [x2 + i].setForeground (Color.white);
	}*/
    }


    /////////////////////////////////////////////////////////////////
    //These are not used but are necessary for mouseListener
    public void mouseEntered (MouseEvent e)
    {
    }

    public void mouseExited (MouseEvent e)
    {
    }

    public void mousePressed (MouseEvent e)
    {
    }

    public void mouseReleased (MouseEvent e)
    {
    }

	public static void main (String[] args) {
		
		new MinesweeperNew2();
    }
	
class TimerDisplay {
  private int nbSeconds=-1;
  private java.util.Timer timer;
  
  
  public void start() {
    timer=new java.util.Timer();
    TimerTask task=new TimerTask() {
      public void run() {
        nbSeconds++;
        TimeRemaning.setText(""+nbSeconds);
      }
    };
    timer.scheduleAtFixedRate(task,0,1000);
  }
  
  public void stop() {
    timer.cancel();
  }
  
  public void reset() {
    timer.cancel();
    nbSeconds=-1;
    TimeRemaning.setText(""+0);    
  }
  
  public int getSeconds() {return nbSeconds;}
    
}

}

Please help us

Try using an ActionListener for your buttons instead of MouseListener. The disabled buttons still receive the click event and you don't check whether the game is over. Also, add a repaint() call at the end or your Restart_Game method so the buttons refresh their appearance.

I didn't have time to go through the whole thing, so I'm not sure if the smile button behavior is also related to using mouse listener instead of action listener, but it might be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.