Will be updated soon..
///////////////////////////////////////////////////////////////
//minesweeper game.
iimport 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;i<mineloc.length;i++ )//reveal game..
{
for (int j=0;j<mineloc.length;j++ )
{
if (mineloc[i][j]<0)
buttons[i][j].setText("#");
else if (mineloc[i][j]==0)
buttons[i][j].setText(" ");
else
buttons[i][j].setText(""+mineloc[i][j]);
}
}
JOptionPane.showMessageDialog(null,"Game
over..!!!!","!!BOOM!!",JOptionPane.INFORMATION_MESSAGE);
}
private void fillfn(int x,int y) //resembles to flood fill..
{
if (x<0 || y<0 || x>8 || 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<mineloc.length;i++ )
{
for (int j=0;j<mineloc.length;j++ )
{
mineloc[i][j]=0;
}
}
for (int j=0;j<10;j++ )//10 mines
{
n1=random.nextInt(9);
n2=random.nextInt(9);
if (mineloc[n1][n2] < 0)// not already a mine.
{
j--;
continue;
}
mineloc[n1][n2]=-100;
try{mineloc[n1-1][n2]++;}catch(Exception e){}
try{mineloc[n1+1][n2]++;}catch(Exception e){}
try{mineloc[n1][n2-1]++;}catch(Exception e){}
try{mineloc[n1][n2+1]++;}catch(Exception e){}
try{mineloc[n1-1][n2-1]++;}catch(Exception e){}
try{mineloc[n1-1][n2+1]++;}catch(Exception e){}
try{mineloc[n1+1][n2-1]++;}catch(Exception e){}
try{mineloc[n1+1][n2+1]++;}catch(Exception e){}
}
}
public static void main(String[] args) {
JFrame f = new JFrame("minesweeper by mayur [mayurdange.t35.com]");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new mine().getContent());
f.setSize(450,300);
f.setLocation(200,200);
f.setVisible(true);
}
}
.java file.jar filealthough the game code looks lengthy, it is not much complex, the essential part is not..
I am not very good at java so the code may not be best or bug free..