Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: GUI minesweeper help

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GUI minesweeper help

    I have to make a GUI version of minesweeper for my class.

    I think I have everything done, but when I execute the program the JFrame pops up but with no content.
    Can anyone see what is wrong with my code? Because I am stumped.

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.InputEvent;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JToggleButton;
     
     
    public class mineSweeperGame extends JFrame{
     
    	private static final long serialVersionUID = 1L;
     
    	private int rows, columns = 10;
     
    	private int cR, cC = 0;
     
    	JToggleButton buttons[][] = new JToggleButton[rows][columns];
     
        int cells[][] = new int[rows][columns];
     
    	boolean bombs[][], shown[][] = new boolean[rows][columns];
     
    	private Container container = null;
     
    	public mineSweeperGame(){
     
    		super("Minesweeper");
    		startGame();
     
    	}
     
    	private void startGame(){
     
    	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(500, 500);
     
    		   container = this.getContentPane();
     
    		   for(int r = 0; r < rows; r++){
    	    		for(int c = 0; c < columns; c++){
     
    	    			cR= r;
    	    			cC = c;
     
    	    			Random b = new Random();
    	    			bombs[r][c] = b.nextBoolean() && b.nextBoolean() && b.nextBoolean();
     
    	    			buttons[r][c] = new JToggleButton("");
     
    	    			buttons[r][c].addMouseListener(new java.awt.event.MouseAdapter(){
    	    				public void mouseReleased(java.awt.event.MouseEvent e){
    	    					if(e.getModifiers() == InputEvent.BUTTON3_MASK){
    	    						markCell(e);
    	    					}
    	    					else if(e.getModifiers() == InputEvent.BUTTON1_MASK){
    	    						showCell(e);
    	    					}
    	    				}
    	    			});
     
    	    			container.add(buttons[r][c], BorderLayout.CENTER);
     
    	    			cells[r][c] = bombCount(r,c);
    	    			shown[r][c] = false;
     
     
    	    		}
    	        }
     
     
    		this.setVisible(true);
    	    this.validate();
    	    this.repaint();
     
       }
     
       public void markCell(java.awt.event.MouseEvent e){
     
     
    	      JToggleButton button = (JToggleButton)e.getSource();
     
    	      if(button.isEnabled()){
    	    	  if(button.getText() != "!"){
    	    		  if(button.getText() != "!"){
    	    			  button.setText("!"); 
    	    		  }
     
    	    	  else{
    	    		   button.setText("");
    	    	   }
    	       }
    	    }
     
    	}
     
    	public void showCell(java.awt.event.MouseEvent e){
     
    		JToggleButton buttons = (JToggleButton)e.getSource();
     
        	if(buttons.isEnabled()){
        		buttons.setEnabled(false);
     
        		if(isBomb(buttons)){
     
        			endGame();
        			buttons.setEnabled(false);
        			disableBoard();
     
        		}
     
        		else{
     
        			if(cells[cR][cC] > 0){
        				buttons.setText(Integer.toString(cells[cR][cC]));
        			}
     
        			else if(cells[cR][cC] == 0){
        				clearCells(cR, cC);
        			}
        		}
        	}
     
    	}
     
    	private int bombCount(int x, int y){
     
    		int bombCount = 0;
    		for(int r = -1; r <= 1; r++){
    			for(int c = -1; c <= -1; c++){
     
    				int i = x + r;
    				int j = y + c;
     
    				if(0 <= i && i < cells.length && 0 <= j && j < cells[i].length){
    					bombCount++;
    				}
    			}
    		}
     
    		return bombCount;
    	}
     
    	private void clearCells(int cR, int cC){
    		for(int r = 0; r < rows; r++){
    			for(int c = 0; c < columns; c++){
    				if(!shown[r][c] == false && bombs[r][c] == false){
     
    					shown[r][c] = true;
     
    					JToggleButton button = findButton(r, c);
     
    					if(cells[r][c] > 0){
    						button.setText(Integer.toString(cells[r][c]));
    					}
     
    					else{
    						button.setText("");
    					}
     
    					button.setSelected(true);
    					button.setEnabled(false);
     
    					if(cells[r][c] == 0){
    						for(int i = -1; i <= 1; i++){
    							for(int j = -1; j <= 1; j++){
     
    								clearCells(i+r, j+c);
     
    							}
    						}
    					}
    				}
    			}
    		}
    	}
     
    	private JToggleButton findButton(int r, int c){
    		return buttons[r][c];
    	}
     
    	private boolean isBomb(JToggleButton button){
     
    	   for(int r = 0; r < rows; r++){
    		   for(int c = 0; c < columns; c++){
    			   if(button == buttons[r][c]){
    				   cR = r;
    				   cC = c;
    				   return bombs[r][c];
    			   }
    		   }
    	   }
     
    	   return false;
     
    	}
     
    	private void endGame(){
     
    		for(int r = 0; r < rows; r++){
    			for(int c = 0; c < columns; c++){
    				if(bombs[r][c] == true){
    					JToggleButton button = findButton(r, c);
     
    					button.setText("*");
    					button.setSelected(true);
    					button.setEnabled(false);
    				}
    			}
    		}
    	}
     
    	private void disableBoard(){
     
    		for(int r = 0; r < rows; r++ ){
    			for(int c = 0; c < columns; c++){
    				JToggleButton button = findButton(r,c);
     
    				button.setEnabled(false);
    			}
    		}
    	}
     
    	public static void main(String[] args){
     
    		mineSweeperGame go = new mineSweeperGame();
     
    	}
     
     
     
     
    }


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: GUI minesweeper help

    Try adding some print statements in your for loop. You will not be getting into your loop at all.

    private int rows, columns = 10; <--This does not work like you think it does.

    private int rows=10, columns=10; <--This is what you want it to look like.

    You will probably also want to use setPreferredSize on the buttons. Probably around 40x40 so that you have a 10x10 board.

    Also it is normal convention to name your classes with Capital letters. This is not really a big deal it is just not conventional to use a lowercase starting letter for a class name.

Similar Threads

  1. recursion for identifying amount of zeroes in minesweeper demo
    By Nightingale in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 28th, 2011, 02:33 PM
  2. Can anyone please help me with this java code for minesweeper game!
    By Mahela in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2011, 07:10 AM
  3. Minesweeper java problem
    By bluez_exe in forum Paid Java Projects
    Replies: 1
    Last Post: March 3rd, 2010, 07:55 PM
  4. [SOLVED] minesweeper game creation problem
    By kaylors in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 27th, 2009, 04:06 PM