?? mine.java
字號:
// File Name: Mine.java
// Author: TianBaideng Student Number: 3005218107
// Class Day: thursday Class Number: 4 Class Year: 2005
// Email: tian_bai_deng@126.com
// Assignment number: 4
// Description: this class is the major class which create a window,buttons,
// and add icon on the button,and has a iner class,Buttonhandler,
// which is to deal with mouse click event,in addition,the main
// arithmetic is in method clear()
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Mine extends JFrame{
//store buttons
private Button buttons[][];
//the mine map,include 0: none mine,1:mine
private Map map[][];
private Container container;
private Icon icon [];
private JLabel label;
//the number of the remaining mines
private int remain ;
//record the buttons position marked with flag
private int record[];
//count for record
private int r;
//for remain mine
private boolean result;
//class Mine constructor which will display the window and create 10*10
//buttons which will add a addMouseListener then call method initial()
//that I have define below to initialize the window
public Mine()
{
super("Mine Sweeper");
//record the butons that is marked with flag
record = new int [20];
container = getContentPane();
//the whole container is BorderLayout
container.setLayout(new BorderLayout());
//create label
label = new JLabel();
//contain the buttons
JPanel pan = new JPanel();
//contain the label
JPanel flow = new JPanel();
flow.setLayout(new FlowLayout());
flow.add(label);
pan.setLayout(new GridLayout(10,10,0,0));
//add the label in the top and buttons in the bottom
container.add(flow,BorderLayout.NORTH);
container.add(pan,BorderLayout.CENTER);
//image sets
String[] name = {"none.gif","1.gif","2.gif","3.gif","4.gif","5.gif","6.gif",
"7.gif","8.gif","mine.gif","flags.gif","d.gif","problem.gif"};
//create icons
icon = new Icon[13];
for(int i=0;i<13;i++)
icon[i] = new ImageIcon(name[i]);
//create 10*10 buttons
buttons = new Button [10][10];
//create map
map = new Map[10][10];
Buttonhandler handler = new Buttonhandler();
//add 100 buttons
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
map[i][j] = new Map();
buttons[i][j] = new Button();
//every button add a listener
buttons[i][j].addMouseListener(handler);
//add the button in the pan which style is grid
pan.add(buttons[i][j]);
}
}
//initialize
initial();
setSize(430,480);
//set the window not resizable
setResizable(false);
setVisible(true);
}// end construct method
//initialize the window such as set button icon ,randomly set mines
void initial()
{
result = true;
remain = 10;
r = 0;
label.setText("Mines remaining"+remain);
label.setForeground(Color.BLUE);
//initialize buttons with icon and buttons with flags
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
map[i][j].mine = 0;
map[i][j].flag = 0;
buttons[i][j].setIcon(icon[11]);
buttons[i][j].Lclick = 0;
buttons[i][j].Rclick = 0;
buttons[i][j].flag = 0;
}
//Randomly add 10 Mines
Random random = new Random();
for(int i=0;i<10;i++)
{
int n= random.nextInt(100);
while(map[n/10][n%10].mine!=0)
{
n=random.nextInt(100);
}
map[n/10][n%10].mine=1;
}
}// end method initial
//spread the none mine area,this method is a recursion method
public void clear(int x,int y)
{
//guarantee the area in the map
if(x>=0&&x<10&&y>=0&&y<10)
{
//insure the area is not referenced
if(map[x][y].flag ==0&&buttons[x][y].flag == 0)
{
//mark the area that can not be clicked
map[x][y].flag = 1;
buttons[x][y].flag =1;
buttons[x][y].Lclick = 1;
//get the number of mines surrounding the area
int c = search(x,y);
//if no mine,then to spread this area and to check its surrounding area
if(c == 0)
{
buttons[x][y].setIcon(icon[0]);
for(int i=x-1;i<=x+1;i++)
for(int j=y-1;j<=y+1;j++)
{
clear(i,j);
}
}
//if has mines,mark the number of mines,then ends
else
buttons[x][y].setIcon(icon[c]);
}
}
}// end method clear
// exposure the mine area
void sweeper()
{
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
if(map[i][j].mine == 1)
buttons[i][j].setIcon(icon[9]);
}// end method sweeper
//found the number of mines in 8 directions
int search(int x,int y)
{
int count =0;
for(int i=x-1;i<=x+1;i++)
for(int j=y-1;j<=y+1;j++)
{
if(i>=0&&i<10&&j>=0&&j<10)
if(map[i][j].mine == 1)
count ++;
}
return count;
} //end method search
//deal with right button event
void rbutton(int i,int j)
{
//deal with which icon is displayed
//0: the original icon is displayed
//1: the flag icon is displayed and mark this button
// the left button can not click
//2: the problem icon is displayed and cancel the mark
// that the left button can click
//left button is not clicked
//the right click number increase
buttons[i][j].Rclick++;
buttons[i][j].flag = 1;
switch(buttons[i][j].Rclick%3)
{
case 0:
buttons[i][j].flag = 0;
buttons[i][j].setIcon(icon[11]);
break;
case 1:
if(result)
{
if(remain>0)
{
record[r] = i;
r++;
record[r] = j;
r++;
buttons[i][j].flag = 1;
buttons[i][j].setIcon(icon[10]);
remain--;
label.setText("Mines remaining"+remain);
//remaining mine is none
if(remain == 0 )
{
//check whether all of the flag buttons is mine
for(int k=0;k<10;k++)
{
if(map[record[2*k]][record[2*k+1]].mine ==0)
{
result = false;
break;
}
}
if(result)
{
JOptionPane.showMessageDialog(null,"You Win!!" );
//reset
initial();
}
else
JOptionPane.showMessageDialog(null,"Miss mark!!!!" );
}
}
}
else
{
buttons[i][j].Rclick--;
JOptionPane.showMessageDialog(null,"Miss mark!!!!" );
}
break;
case 2:
if(remain>0 || !result)
{
r--;
r--;
buttons[i][j].flag = 0;
remain++;
label.setText("Mines remaining"+remain);
buttons[i][j].setIcon(icon[12]);
result = true;
}
break;
}
//if(remain == 0)
} //end method rbutton
//deal with left button event
void lbutton(int i,int j)
{
//this button has not been clicked
if(buttons[i][j].flag == 0)
{
//mark the button
buttons[i][j].Lclick = 1;
//mine
if(map[i][j].mine == 1)
{
//map[i][j].flag =1;
//all mines is displayed
sweeper();
//game is over and reset
JOptionPane.showMessageDialog(null,"You are die!!" );
initial();
}
//none mine
else
clear(i,j);
}
}//end method lbutton
//mouse handler to deal with the mouse event
class Buttonhandler extends MouseAdapter
{
public void mouseClicked (MouseEvent event)
{
//to check which mouse event
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
//if the event is the button event,then......
if(event.getSource() == buttons[i][j])
{
//right button is clicked
if(event.isMetaDown())
{
if(buttons[i][j].Lclick == 0)
rbutton(i,j);
}
//left button is clicked
else
lbutton(i,j);
}
}// end iner for
} // end outer for
} // end class Buttonhandler
}
} //end class Mine
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -