当前位置:编程学习 > JAVA >>

扫雷

import java.awt.BorderLayout;


import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;


public class JMineFrame extends JFrame{
JMine jmine;
JLabel leil1;
JLabel leil2;
JLabel leil3;
JLabel timeL1;
JLabel timeL2;
JLabel timeL3;
JLabel timeL4;
JButton[][] buts;
JButton start;
int col;
int row;
int leiCount;
int leiCounts1;//右键以后地雷数量
int countTime;
JMenuItem s1,s2,s3,exit;
//声明数组,存放图片资源
ImageIcon[] imgs={new ImageIcon("c0.gif"),new ImageIcon("c1.gif"),new ImageIcon("c2.gif"),new ImageIcon("c3.gif"),new ImageIcon("c4.gif"),new ImageIcon("c5.gif"),new ImageIcon("c6.gif"),new ImageIcon("c7.gif"),
new ImageIcon("c8.gif"),new ImageIcon("c9.gif"),new ImageIcon("c10.gif"),new ImageIcon("b0.gif"),new ImageIcon("b1.gif")};
ImageIcon[] leiimgs={new ImageIcon("0.gif"),new ImageIcon("1.gif"),new ImageIcon("2.gif"),new ImageIcon("3.gif"),new ImageIcon("4.gif"),new ImageIcon("5.gif"),new ImageIcon("6.gif"),new ImageIcon("7.gif"),
new ImageIcon("8.gif"),new ImageIcon("bomb.gif"),new ImageIcon("d0.gif"),new ImageIcon("d1.gif"),new ImageIcon("d2.gif")};
public JMineFrame(int row,int col,int leiCount){
this.row=row;
this.col=col;
this.leiCount=leiCount;
this.leiCounts1=leiCount;
jmine=new JMine(row,col,leiCount);
jmine.bulei();
jmine.countAll();
}

public void init(){
leil1=new JLabel(imgs[0]);
leil2=new JLabel(imgs[0]);
leil3=new JLabel(imgs[0]);
timeL1=new JLabel(imgs[0]);
timeL2=new JLabel(imgs[0]);
timeL3=new JLabel(imgs[0]);
timeL4=new JLabel(imgs[0]);
buts=new JButton[row][col];//null





}
public boolean isWin(){
int count=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(jmine.lei[i][j]=='雷'&&buts[i][j].getIcon()==leiimgs[11]){
count++;
}
}
}
return (count==leiCount&&leiCounts1==0);
}
class TimeThread extends Thread{
//countTime=0;
public void run(){
while(true){
timeL1.setIcon(imgs[countTime/1000]);
timeL2.setIcon(imgs[countTime%1000/100]);
timeL3.setIcon(imgs[countTime%1000%100/10]);
timeL4.setIcon(imgs[countTime%1000%100%10]);
countTime++;
try {

Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void leiLabel(){
if(leiCounts1>=0){
leil1.setIcon(imgs[leiCounts1/100]);
leil2.setIcon(imgs[leiCounts1%100/10]);
leil3.setIcon(imgs[leiCounts1%100%10]);
}else{

leil1.setIcon(imgs[10]);
leil2.setIcon(imgs[leiCounts1%100/10]);
leil3.setIcon(imgs[leiCounts1%100%10]);
}


}
//创建窗体上部分的面板
public JPanel createNpanel(){
JPanel panel=new JPanel();
panel.add(leil1);
panel.add(leil2);
panel.add(leil3);
start=new JButton();
start.addActionListener(new Click());
start.setIcon(imgs[12]);
Insets space =new Insets(0,0,0,0);
start.setMargin(space);
start.setSize(26,27);
start.setPressedIcon(imgs[11]);
panel.add(start);
panel.add(timeL1);           
panel.add(timeL2);
panel.add(timeL3);
panel.add(timeL4);

return panel;
}
public JPanel createCenterPanel(){
JPanel panel=new JPanel();
GridLayout layout=new GridLayout(row,col);
panel.setLayout(layout);
Insets space=new Insets(0,0,0,0);
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
JButton but=new JButton();
but.setIcon(leiimgs[10]);
but.setMargin(space);
but.setSize(26,27);
panel.add(but);
buts[i][j]=but;
but.addMouseListener(new Click());
}
}
return panel;
}
public void showFrame(){

JPanel panel =new JPanel();
BorderLayout layout=new BorderLayout();
panel.setLayout(layout);
this.add(panel);                            
JPanel spanel=createNpanel();     
JMenuBar Bar=createBar();
this.setJMenuBar(Bar);
panel.add(spanel,BorderLayout.SOUTH);     
JPanel cPanel=createCenterPanel();
panel.add(cPanel,BorderLayout.CENTER);
this.pack();
leiLabel();
this.setLocation(100,100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TimeThread t=new TimeThread();
t.start();

}
public JMenuBar createBar(){
JMenuBar bar=new JMenuBar();
JMenu file=new JMenu("文件");



s1=new JMenuItem("初级");
s2=new JMenuItem("中级");
s3=new JMenuItem("高级");
exit=new JMenuItem("退出");

file.add(s1);
file.add(s2);
file.add(s3);
file.add(exit);

bar.add(file);

s1.addActionListener(new Click());
s2.addActionListener(new Click());
s3.addActionListener(new Click());
exit.addActionListener(new Click());


return bar;
}
public void move(int i,int j){
for(int a=i-1;a<=i+1;a++){
for(int b=j-1;b<=j+1;b++){
if(a>=0&&b>=0&&a<row&&b<col&&!(a==i&&b==j)){
if(buts[a][b].getIcon()==leiimgs[10]){
int lei=jmine.count(a, b);
if(lei==0){
buts[a][b].setIcon(leiimgs[lei]);
move(a,b);
}else{
buts[a][b].setIcon(leiimgs[lei]);
}
}
}

}
}

}
class Click implements MouseListener,ActionListener{

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(e.getSource()==buts[i][j]){
if(buts[i][j].getIcon()==leiimgs[10]||(e.getButton()==MouseEvent.BUTTON3&&buts[i][j].getIcon()==leiimgs[11])){
if(e.getButton()==MouseEvent.BUTTON1){
if(jmine.lei[i][j]=='雷'){
 for(int m=0;m<row;m++){
 for(int n=0;n<col;n++){
 if(jmine.lei[m][n]=='雷'){
 buts[m][n].setIcon(new ImageIcon("bomb.gif"));
 
 }
 }
 }
 JOptionPane.showMessageDialog(JMineFrame.this,"您踩到地雷");
}else {
int lei=jmine.count[i][j];
if(jmine.count[i][j]!=0){

buts[i][j].setIcon(leiimgs[lei]);

}else{
buts[i][j].setIcon(leiimgs[lei]);
move(i,j);
}
}
 }else if(e.getButton()==MouseEvent.BUTTON3){
 
 if(buts[i][j].getIcon()==leiimgs[10]){
 buts[i][j].setIcon(leiimgs[11]);
 leiCounts1--;
 leiLabel();
 }else{
 buts[i][j].setIcon(leiimgs[11]);
 leiCounts1++;
 leiLabel();
 }
 if(isWin()){
 JOptionPane.showMessageDialog(JMineFrame.this,"扫雷胜利");
 }
 
 }

}


}

}



}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==start){
jmine.bulei();
jmine.countAll();
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
buts[i][j].setIcon(leiimgs[10]);
}
}
leiCounts1=leiCount;
leiLabel();
countTime=0;

}else if(e.getSource()==s1){
JMineFrame.this.setVisible(false);
JMineFrame j=new JMineFrame(10,10,10);
j.init();
j.showFrame();
}else if(e.getSource()==s2){
JMineFrame.this.setVisible(false);
JMineFrame j=new JMineFrame(15,15,30);
j.init();
j.showFrame();
}else if(e.getSource()==s3){
JMineFrame.this.setVisible(false);
JMineFrame j=new JMineFrame(20,20,50);
j.init();
j.showFrame();
}else if(e.getSource()==exit){
System.exit(0);
}
}

}

public static void main(String[] args){
JMineFrame j=new JMineFrame(10,10,10);
j.init();
j.showFrame();

}

}





//随机布雷,统计地雷个数
public class JMine {
int row;
int col;
int leiCount;
char[][] lei;
int[][] count;
public JMine(int row,int col,int leiCount){
this.row=row;
this.col=col;
this.leiCount=leiCount;
lei=new char[row][col];
count=new int[row][col];
}
 
public void bulei(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
lei[i][j]='*';
}
}
for(int i=0;i<leiCount;i++){
int a=(int)(Math.random()*row);
int b=(int)(Math.random()*col);
if(lei[a][b]=='雷'){
i--;
}else{
lei[a][b]='雷';
}
}
}
public void countAll(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
count[i][j]=count(i,j);

}
}
}
public int count(int i,int j){
int count=0;
for(int a=i-1;a<=i+1;a++){
for(int b=j-1;b<=j+1;b++){
if(a>=0&&b>=0&&a<row&&b<col&&!(a==i&&b==j)&&lei[a][b]=='雷'){
count++;
}
}
}
return count;

}


}
--------------------编程问答--------------------
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,