import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random; public class mine implements ActionListener { JButton[][] buttons = new JButton[9][9]; int[][] mineloc = new int[9][9]; int count=71;//number of buttons to click to win game.. public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); Point gridLoc = getArrayLocation(button); if (mineloc[gridLoc.x][gridLoc.y]<0)//mine!!! so game over. { buttons[gridLoc.x][gridLoc.y].setText("#"); gameover(); } else if (mineloc[gridLoc.x][gridLoc.y]>0)// number.. only the button is visible. { if (buttons[gridLoc.x][gridLoc.y].getText()=="?") { buttons[gridLoc.x][gridLoc.y].setText(""+mineloc[gridLoc.x][gridLoc.y]); count--; if (count==0) JOptionPane.showMessageDialog(null,"you won..","Woooha!!",JOptionPane.INFORMATION_MESSAGE); } } else if (mineloc[gridLoc.x][gridLoc.y]==0)// is zero, so reveal area with 0/>0 values for 8-adjacent locations. { fillfn(gridLoc.x,gridLoc.y); } } private void gameover() { for (int i=0;i8 || y>8) return; //if out of bound.. if (buttons[x][y].getText()=="?")//if unprocessed { if (mineloc[x][y] > 0)//number { buttons[x][y].setText(""+mineloc[x][y]); count--; if (count==0) JOptionPane.showMessageDialog(null,"you won ..","Woooha!!",JOptionPane.INFORMATION_MESSAGE); } if (mineloc[x][y]==0 )//blank { buttons[x][y].setText(" "); count--; if (count==0) JOptionPane.showMessageDialog(null,"you won..","Woooha!!",JOptionPane.INFORMATION_MESSAGE); fillfn(x+1,y+1);fillfn(x-1,y-1);fillfn(x-1,y+1);fillfn(x+1,y-1); fillfn(x+1,y);fillfn(x-1,y);fillfn(x,y+1);fillfn(x,y-1);//recursively process all 8 adjacent buttons.. } } } private Point getArrayLocation(JButton target) { Point p = new Point(-1, -1); for(int j = 0; j < buttons.length; j++) { for(int k = 0; k < buttons[j].length; k++) { if(buttons[j][k] == target) { p.setLocation(j, k); return p; } } } return p; } private JPanel getContent() { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weighty = 1.0; gbc.weightx = 1.0; for(int j = 0; j < buttons.length; j++) { for(int k = 0; k < buttons[j].length; k++) { int n = j*buttons[j].length + k + 1; buttons[j][k] = new JButton("?"); buttons[j][k].addActionListener(this); gbc.gridwidth = (k < buttons[j].length-1) ? 1 :GridBagConstraints.REMAINDER; panel.add(buttons[j][k], gbc); } } return panel; } mine() { Random random = new Random(); int n1,n2; for (int i=0;i