📄 minegame.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MineGame extends JFrame{
private MyButton buttons[];
private Container container;
private Label label; //show in the container
private int sum; //show Mine Num in the label
private int findMine; // The num of Mine you find in fact
private int numOfMine;
private int numOfButton;
private int row;
private int colum;
private JPanel panel; //The panel in the top.
private JPanel panel1; // hold the GridLayout
boolean over; //Label Game Over
public MineGame(int num,int row,int col)
{
super( "Game" );
findMine=0;
panel=new JPanel();
panel1=new JPanel();
numOfMine=num;
this.row=row;
colum=col;
showInitialButton(numOfMine,this.row,colum);
}
// Initial The Button and the label in the graphic
public void showInitialButton(int numOfMine,int row,int colum)
{
//Layout manager application
numOfButton=row*colum;
sum=numOfMine;
panel.setLayout(new BorderLayout(5,5));
label=new Label("Mine Game "+""+numOfMine );
label.setAlignment(Label.CENTER);
panel.add(label,BorderLayout.NORTH);
panel1.setLayout( new GridLayout(row,colum,1,1));
buttons=new MyButton[numOfButton];
over =false;
for(int j=0;j<numOfButton;j++)
{
buttons[j] = new MyButton();
buttons[j].SetStatus(true); // all the button have no mines
buttons[j].SetPos(j); // the positon of the button
panel1.add( buttons[j]);
}
panel.add(panel1,BorderLayout.CENTER);
container = getContentPane();
container.add(panel);
//Initial the Mine
initMiner();
//ButtonLisener,eventHandler
for(int i=0;i<numOfButton;i++)
{
buttons[i].addActionListener(new ButtonListener());
buttons[i].addMouseListener(new MouseClickHandler());
}
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize(500, 500);
setVisible(true);
setLocationRelativeTo(getParent());
}
// inner Class ButtonListener
private class ButtonListener implements ActionListener {
public void actionPerformed( ActionEvent event ) {
checkButton((MyButton)event.getSource(),false);
}
}
//inner Class MouseClickHandler isMetaDown()
private class MouseClickHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
if(over==false)
{
if(event.isMetaDown())
{
MyButton button=(MyButton) event.getSource();
if(button.getVisit()==true);
else if(button.getLabel()=="#")
{
button.setLabel("");
sum++;
label.setText("Mine Game "+sum);
if(button.GetStatus()==false)
{
findMine--;
}
}
else
{
button.setLabel("#");
sum--;
label.setText("Mine Game "+sum);
if(button.GetStatus()==false)
findMine++;
}
if(sum==0)
{
if(findMine==numOfMine)
{
JOptionPane.showMessageDialog( null, "You WIN! " +
"Congratulations!!",
"Game",JOptionPane.INFORMATION_MESSAGE );
over=true;
Restart restart=new Restart();
}
else if(findMine<numOfMine)
{
JOptionPane.showMessageDialog( null, "You Missed some Mine!",
"Game",JOptionPane.INFORMATION_MESSAGE );
}
}
}
}
}
}
//Game over
public void showBomb()
{
for(int i=0;i<numOfButton;i++)
{
if(buttons[i].GetStatus()==false)
{
buttons[i].setBackground(Color.GREEN);
}
}
}
// check whether click the Mine
public void checkButton(MyButton button,boolean param)
{
if(over==false)
{
int location=button.GetPos();
if(buttons[location].getVisit()==true);
else if(buttons[location].getLabel()=="#");
else if(buttons[location].GetStatus()==false)
{
if(param==false)
{
JOptionPane.showMessageDialog( null, "You Fail! " ,
"Game",JOptionPane.INFORMATION_MESSAGE );
showBomb();
over=true;
Restart restart=new Restart();
}
else
{};
}
else
{ //Calculate the Number of the Mine in 8 direction
/*if((buttons[location].getLabel()=="#")&&(param==true))
{
sum++;
label.setText(""+sum);
buttons[location].setLabel("");
}*/
//if(buttons[location].getLabel()=="")
buttons[location].setBackground(Color.pink);
buttons[location].setVisit();
if((location%colum!=colum-1)&&(!buttons[location+1].GetStatus()))
{
buttons[location].increment();
}
if((location%colum!=0)&&(!buttons[location-1].GetStatus()))
{
buttons[location].increment();
}
if((location<colum*(row-1))&&(!buttons[location+colum].GetStatus()))
{
buttons[location].increment();
}
if((location>=colum)&&(!buttons[location-colum].GetStatus()))
{
buttons[location].increment();
}
if((location%colum!=0)&&(location>colum)
&&(!buttons[location-1-colum].GetStatus()))
{
buttons[location].increment();
}
if((location%colum!=colum-1)&&(location>=colum)
&&(!buttons[location-colum+1].GetStatus()))
{
buttons[location].increment();
}
if((location%colum!=colum-1)&&(location<colum*(row-1))
&&(!buttons[location+1+colum].GetStatus()))
{
buttons[location].increment();
}
if((location%colum!=0)&&(location<colum*(row-1))
&&(!buttons[location+colum-1].GetStatus()))
{
buttons[location].increment();
}
if(buttons[location].getCount()!=0)
{
if((buttons[location].getLabel()=="#")&&(param=true));
else
buttons[location].setLabel(""+buttons[location].getCount());
}
else
{ // Recursion the button to find the num of Mine in 8 Dimension
if(location%colum!=colum-1)checkButton(buttons[location+1],true);
if(location%colum!=0)checkButton(buttons[location-1],true);
if(location<colum*(row-1)) checkButton(buttons[location+colum],true);
if(location>=colum)checkButton(buttons[location-colum],true);
if((location%colum!=0)&&(location>colum))
checkButton(buttons[location-1-colum],true);
if((location%colum!=colum-1)&&(location>=colum))
checkButton(buttons[location-colum+1],true);
if((location%colum!=colum-1)&&(location<colum*(row-1)))
checkButton(buttons[location+1+colum],true);
if((location%colum!=0)&&(location<colum*(row-1)))
checkButton(buttons[location+colum-1],true);
}
}
}
}
//Initial the Mine
public void initMiner()
{
int count=1;
int val;
while(count<=numOfMine)
{
val = ( int )(Math.random() * numOfButton); //random() (0-1)
if(buttons[val].GetStatus()==false);
else
{
buttons[val].SetStatus(false);
count++;
}
}
}
public static void main(String args[])
{
CheckBox box=new CheckBox();
box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -