Geekpedia Programming Tutorials






Minesweeper Game In Java

A fully functional Minesweeper game in Java that is similar to the one bundled with Windows, and that can be easily enhanced.

On Friday, August 8th 2008 at 11:01 AM
By Andrew Pociu (View Profile)
*****   (Rated 5 with 1 votes)
Contextual Ads
More Java Resources
Advertisement
  1. import java.awt.BorderLayout;
  2. import java.awt.GridLayout;
  3. import java.awt.event.InputEvent;
  4. import java.util.Random;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JPanel;
  10. import javax.swing.JProgressBar;
  11. import javax.swing.JToggleButton;
  12. import javax.swing.JToolBar;
  13.  
  14. public class Minesweeper extends JFrame {
  15.  
  16.         private int columns = 10;
  17.         private int rows = 10;
  18.         private static final long serialVersionUID = 1L;
  19.         // Stores which cells are bombs and which are not
  20.         boolean jBombs[][] = new boolean[rows][columns];
  21.         // Stores information on which cell has been displayed
  22.         boolean jShown[][] = new boolean[rows][columns];
  23.         // Stores information on surrounding cells
  24.         int jCells[][] = new int[rows][columns];
  25.         private int currX, currY = 0;
  26.         JToggleButton jButtons[] = new JToggleButton[columns*rows];
  27.         private JPanel jPanel = null;
  28.         private JToolBar jToolBar = null;
  29.         private JPanel jContentPane = null;
  30.         private JButton jBtnNewGame = null;
  31.         private JProgressBar jProgressBar = null;
  32.         /**
  33.          * This is the default constructor
  34.          */
  35.         public Minesweeper() {
  36.                 super();
  37.                 initialize();
  38.         }
  39.  
  40.         /**
  41.          * This method initializes this
  42.          *
  43.          * @return void
  44.          */
  45.         private void initialize() {
  46.                 this.setSize(436, 315);
  47.                 this.setContentPane(getJPanel());
  48.                 this.setTitle("Minesweeper");
  49.         }
  50.  
  51.         /**
  52.          * This method initializes jPanel       
  53.          *      
  54.          * @return javax.swing.JPanel   
  55.          */
  56.         private JPanel getJPanel() {
  57.                 if (jPanel == null) {
  58.                         jPanel = new JPanel();
  59.                         jPanel.setLayout(new BorderLayout());
  60.                         jPanel.add(getJToolBar(), BorderLayout.NORTH);
  61.                         jPanel.add(getJContentPane(), BorderLayout.CENTER);
  62.                         jPanel.add(getJProgressBar(), BorderLayout.SOUTH);
  63.                 }
  64.                 return jPanel;
  65.         }
  66.  
  67.         /**
  68.          * This method initializes jToolBar     
  69.          *      
  70.          * @return javax.swing.JToolBar 
  71.          */
  72.         private JToolBar getJToolBar() {
  73.                 if (jToolBar == null) {
  74.                         jToolBar = new JToolBar();
  75.                         jToolBar.setFloatable(false);
  76.                         jToolBar.add(getJBtnNewGame());
  77.                 }
  78.                 return jToolBar;
  79.         }
  80.  
  81.         /**
  82.          * This method initializes jContentPane 
  83.          *      
  84.          * @return javax.swing.JPanel   
  85.          */
  86.         private JPanel getJContentPane() {
  87.                 if (jContentPane == null)
  88.                 {
  89.                         GridLayout gridLayout = new GridLayout();
  90.                         gridLayout.setRows(rows);
  91.                         gridLayout.setColumns(columns);
  92.                         jContentPane = new JPanel();
  93.                         jContentPane.setLayout(gridLayout);
  94.                        
  95.                         BuildBoard();
  96.                 }
  97.                
  98.                 return jContentPane;
  99.         }
  100.        
  101.         private void BuildBoard()
  102.         {
  103.                 // JContentPane will call this function before jProgressBar was built when first started
  104.                 if(jProgressBar != null) // So we check to see if the progress bar has been initialized
  105.                 {
  106.                         jProgressBar.setValue(0);
  107.                 }
  108.                 jContentPane.removeAll();
  109.                 int i = 0;
  110.                 for(int x = 0; x < rows; x++)
  111.                 {
  112.                         for(int y = 0; y < columns; y++)
  113.                         {
  114.                                 currX = x;
  115.                                 currY = y;
  116.                                 Random randBomb = new Random();
  117.                                 jBombs[x][y] = randBomb.nextBoolean() && randBomb.nextBoolean() && randBomb.nextBoolean(); // 13% chances of a bomb
  118.                                 jButtons[i] = new JToggleButton("");
  119.                                 jButtons[i].addMouseListener(new java.awt.event.MouseAdapter(){
  120.                                         public void mouseReleased(java.awt.event.MouseEvent e) {
  121.                                                 if(e.getModifiers() == InputEvent.BUTTON3_MASK)
  122.                                                 {
  123.                                                         markCell(e);
  124.                                                 }
  125.                                                 else if(e.getModifiers() == InputEvent.BUTTON1_MASK)
  126.                                                 {
  127.                                                         showCell(e);
  128.                                                 }
  129.                                         }
  130.                                 });
  131.                                 jContentPane.add(jButtons[i]);
  132.                                 i++;
  133.                         }
  134.                 }
  135.                
  136.                 // Build the board
  137.                 for(int x = 0; x < rows; x++)
  138.                 {
  139.                         for(int y = 0; y < columns; y++)
  140.                         {
  141.                                 jCells[x][y] = bombCount(x, y);
  142.                                 jShown[x][y] = false; // Reset previous values
  143.                         }
  144.                 }
  145.                 jContentPane.setEnabled(true);
  146.                 this.repaint();
  147.                 this.validate();
  148.         }
  149.  
  150.         /**
  151.          * This method initializes jBtnNewGame 
  152.          *      
  153.          * @return javax.swing.JButton 
  154.          */
  155.         private JButton getJBtnNewGame() {
  156.                 if (jBtnNewGame == null) {
  157.                         jBtnNewGame = new JButton();
  158.                         jBtnNewGame.setText("New Game");
  159.                         jBtnNewGame.addMouseListener(new java.awt.event.MouseAdapter() {
  160.                                 public void mouseReleased(java.awt.event.MouseEvent e) {
  161.                                         BuildBoard();
  162.                                 }
  163.                         });
  164.                 }
  165.                 return jBtnNewGame;
  166.         }
  167.  
  168.         /**
  169.          * This method initializes jProgressBar 
  170.          *      
  171.          * @return javax.swing.JProgressBar     
  172.          */
  173.         private JProgressBar getJProgressBar() {
  174.                 if (jProgressBar == null) {
  175.                         jProgressBar = new JProgressBar();
  176.                         jProgressBar.setMaximum(columns * rows);
  177.                 }
  178.                 return jProgressBar;
  179.         }
  180.  
  181.         public static void main(String args[])
  182.         {
  183.                
  184.         }
  185.        
  186.         // Displays all cells marked as bombs
  187.         private void showAllBombs()
  188.         {
  189.                 for(int x = 0; x < rows; x++)
  190.                 {
  191.                         for(int y = 0; y < columns; y++)
  192.                         {
  193.                                 if(jBombs[x][y] == true)
  194.                                 {
  195.                                         JToggleButton jButton = findButton(x,y);
  196.                                         if(jButton.isEnabled()) // Don't go over the ones that were already counted
  197.                                         {
  198.                                                 jProgressBar.setValue(jProgressBar.getValue() + 1);
  199.                                         }
  200.                                         jButton.setText("X");
  201.                                         jButton.setSelected(true);
  202.                                         jButton.setEnabled(false);
  203.                                 }
  204.                         }
  205.                 }
  206.         }
  207.        
  208.         private void clearCells(int x, int y)
  209.         {
  210.                 // If the cell is in bounds