import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*按扭类*/
class bomb extends jbutton
{
public int num_x,num_y; //第几号方块
public int bombroundcount; //周围雷数
public boolean isbomb; //是否为雷
public boolean isclicked; //是否被点击
public int bombflag; //探雷标记
public boolean isright; //是否点击右键
public bomb(int x,int y)
{
bombflag = 0;
num_x = x;
num_y = y;
bombroundcount = 0;
isbomb = false;
isclicked = false;
isright = false;
}
}
/*窗口及算法实现类*/
class mainbomb extends jframe implements actionlistener,mouselistener
{
public jtextfield text;
public label nowbomb,setbomb;
public int blocknum,bombnum; //当前方块数当前雷数
public icon icon_bomb = new imageicon("bomb.gif"); //踩雷
public icon icon_bomb_big = new imageicon("bomb_big.gif"); //踩雷标记
public icon icon_flag = new imageicon("flag.gif"); //雷标记
public icon icon_question = new imageicon("question.gif"); //疑惑是否有雷
public jbutton start = new jbutton(" 开始 ");
public panel menupamel = new panel();
public panel mainpanel = new panel();
public bomb[][] bombbutton;
/*界面设计*/
public mainbomb()
{
super("扫雷 aaron2004制作 2004.8 ");
blocknum = 64;
bombnum = 10;
container c=getcontentpane();
c.setbackground(color.gray);
c.setlayout(new borderlayout());
text=new jtextfield("10 ",3);
nowbomb = new label("当前雷数"+" "+bombnum+"");
setbomb= new label("设置地雷数");
start.addactionlistener(new actionlistener(){
public void actionperformed(actionevent e)
{
bombnum = integer.parseint(text.gettext().trim());
if(bombnum >= 10 && bombnum < 50 )
replay();
else
{
joptionpane msg = new joptionpane();
joptionpane.showmessagedialog(null,"您设置的地雷数太多了,请重设!","错误",2);
}
}
} );
menupamel.add(setbomb);
menupamel.add(text);
menupamel.add(start);
menupamel.add(nowbomb);
c.add(menupamel,"north");
mainpanel.setlayout(new gridlayout( (int)math.sqrt(blocknum) , (int)math.sqrt(blocknum)) );
bombbutton=new bomb[ (int)math.sqrt(blocknum) ][];
for(int i = 0 ; i < (int)math.sqrt(blocknum) ; i++)
{
bombbutton[ i ]=new bomb[ (int)math.sqrt(blocknum) ];
}
for(int i = 0 ; i < (int)math.sqrt(blocknum) ; i++ )
for(int j = 0 ; j < (int)math.sqrt(blocknum) ; j++ )
{
bombbutton[ i ][ j ]=new bomb(i,j);
bombbutton[ i ][ j ].setforeground( color.gray);
bombbutton[ i ][ j ].addactionlistener(this);
bombbutton[ i ][ j ].addmouselistener(this);
}
for(int i = 0 ; i < (int)math.sqrt(blocknum) ; i++ )
for(int j = 0 ; j < (int)math.sqrt(blocknum) ; j++ )
mainpanel.add(bombbutton[ i ][ j ]);
c.add(mainpanel,"center");
startbomb();
setsize(400,400);
setlocation(350,200);
setresizable(false);
}
/*布雷*/
public void startbomb()
{
for(int i=0;i<bombnum;i++)
{
int x =(int)(math.random()*(int)(math.sqrt(blocknum)-1));
int y =(int)(math.random()*(int)(math.sqrt(blocknum)-1));
if(bombbutton[ x ][ y ].isbomb==true)
i–;
else
bombbutton[ x ][ y ].isbomb=true ;
}
}
/*重新开始*/
public void replay()
{
nowbomb.settext("当前雷数"+" "+bombnum+"");
for(int i = 0 ; i < (int)math.sqrt(blocknum) ; i++)
for(int j = 0 ; j < (int)math.sqrt(blocknum) ; j++)
{
bombbutton[ i ][ j ].isbomb=false;
bombbutton[ i ][ j ].isclicked=false;
bombbutton[ i ][ j ].setenabled(true);
bombbutton[ i ][ j ].settext("");
bombbutton[ i ][ j ].seticon(null);
}
startbomb();
}
/*是否挖完了所有的雷*/
public void iswin()
{
int findbomb=0; //找到的地雷数
for(int i = 0;i < (int)math.sqrt(blocknum) ; i++)
for(int j = 0;j < (int)math.sqrt(blocknum ); j++)
{
if(bombbutton[ i ][ j ].isbomb == true && bombbutton[ i ][ j ].isright == true)
findbomb++;
}
if( findbomb == integer.parseint(text.gettext().trim()) )
{
joptionpane msg = new joptionpane();
joptionpane.showmessagedialog(this,"您挖完了所有的雷,您胜利了!","您胜利了",2);
}
}
/*计算方块周围雷数 */
public void countroundbomb()
{
for (int i = 0; i < (int)math.sqrt(blocknum); i++) {
for (int j = 0; j < (int)math.sqrt(blocknum); j++) {
int count = 0;
//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombbutton[ i ][ j ].isbomb != true) {
if ( (i – 1 >= 0) && (j – 1 >= 0)) {
if (bombbutton[i – 1][j – 1].isbomb == true) {
count += 1; //检测左上方空格是否是地雷
}
}
if ( (i – 1 >= 0)) {
if (bombbutton[i – 1][ j ].isbomb == true) {
count += 1; //检测上方空格是否为地雷
}
}
if ( (i – 1 >= 0) && (j + 1 <= (int)math.sqrt(blocknum)-1)) {
if (bombbutton[i – 1][j + 1] .isbomb == true) {
count += 1; //检测右上方是否为地雷
}
}
if ( (j – 1 >= 0)) {
if (bombbutton[ i ][j – 1] .isbomb == true) {
count += 1; //检测左边是否为地雷
}
}
if ( (i >= 0) && (j + 1 <= (int)math.sqrt(blocknum)-1)) {
if (bombbutton[ i ][j + 1].isbomb == true) {
count += 1; //右边
}
}
if ( (j – 1 >= 0) && (i + 1 <= (int)math.sqrt(blocknum)-1)) {
if (bombbutton[i + 1][j – 1].isbomb == true) {
count += 1; //左下
}
}
if ( (i + 1 <= (int)math.sqrt(blocknum)-1)) {
if (bombbutton[i + 1][ j ].isbomb == true) {
count += 1; //下
}
}
if ( (j + 1 <= (int)math.sqrt(blocknum)-1) && (i + 1 <= math.sqrt(blocknum)-1)) {
if (bombbutton[i + 1][j + 1].isbomb == true) {
count += 1; //右下
}
}
bombbutton[ i ][ j ].bombroundcount = count;
}
}
}
}
/**当选中的位置为空,则翻开周围的地图**/
public void isnull(bomb[][] bombbutton,bomb clickecbutton)
{
int i,j;
i=clickecbutton.num_x;
j=clickecbutton.num_y;
if (clickecbutton.isbomb==true) {
}
else {
if ( (i – 1 >= 0) && (j – 1 >= 0)) { //检测左上方空格是否是空
if (bombbutton[i – 1][j – 1].isbomb == false && bombbutton[i – 1][j – 1].isclicked == false && bombbutton[i – 1][j – 1].isright == false) {
bombbutton[i – 1][j – 1].settext((bombbutton[i – 1][j – 1].bombroundcount)+"");
bombbutton[i – 1][j – 1].setenabled(false);
bombbutton[i – 1][j – 1].isclicked=true;
}
}
if ( (i – 1 >= 0)) { //检测上方空格是否为空
if (bombbutton[i – 1][ j ] .isbomb == false && bombbutton[i – 1][ j ].isclicked == false && bombbutton[i – 1][ j ].isright == false) {
bombbutton[i – 1][ j ].settext((bombbutton[i – 1][ j ].bombroundcount)+"");
bombbutton[i – 1][ j ].setenabled(false);
bombbutton[i – 1][ j ].isclicked=true;
}
}
if ( (i – 1 >= 0) && (j + 1 <= ((int)math.sqrt(blocknum)-1)) ) { //检测右上方是否为空
if (bombbutton[i – 1][j + 1] .isbomb == false && bombbutton[i – 1][j + 1].isclicked == false && bombbutton[i – 1][j + 1].isright == false) {
bombbutton[i – 1][j + 1].settext((bombbutton[i – 1][j + 1].bombroundcount)+"");
bombbutton[i – 1][j + 1].setenabled(false);
bombbutton[i – 1][j + 1].isclicked=true;
}
}
if ( (j – 1 >= 0)) { //检测左边是否为空
if (bombbutton[ i ][j – 1].isbomb == false && bombbutton[ i ][j – 1].isclicked == false && bombbutton[ i ][j – 1].isright == false) {
bombbutton[ i ][j – 1].settext((bombbutton[ i ][j – 1].bombroundcount)+"");
bombbutton[ i ][j – 1].setenabled(false);
bombbutton[ i ][j – 1].isclicked=true;
}
}
if ( (i >= 0) && (j + 1 <= ((int)math.sqrt(blocknum)-1)) ) { //检测右边空格是否是空
if (bombbutton[ i ][j + 1].isbomb == false && bombbutton[ i ][j + 1].isclicked == false && bombbutton[ i ][j + 1].isright == false) {
bombbutton[ i ][j + 1].settext((bombbutton[ i ][j + 1].bombroundcount)+"");
bombbutton[ i ][j + 1].setenabled(false);
bombbutton[ i ][j + 1].isclicked=true;
}
}
if ( (j – 1 >= 0) && (i + 1 <= ((int)math.sqrt(blocknum)-1)) ) { //检测左下空格是否是空
if (bombbutton[i + 1][j – 1].isbomb == false && bombbutton[i + 1][j – 1].isclicked == false && bombbutton[i + 1][j – 1].isright == false) {
bombbutton[i + 1][j – 1].settext((bombbutton[i + 1][j – 1].bombroundcount)+"");
bombbutton[i + 1][j – 1].setenabled(false);
bombbutton[i + 1][j – 1].isclicked=true;
}
}
if ( (i + 1 <= ((int)math.sqrt(blocknum)-1)) ) { //检测下边空格是否是空
if (bombbutton[i + 1][ j ].isbomb == false && bombbutton[i + 1][ j ].isclicked == false && bombbutton[i + 1][ j ].isright == false) {
bombbutton[i + 1][ j ].settext((bombbutton[i + 1][ j ].bombroundcount)+"");
bombbutton[i + 1][ j ].setenabled(false);
bombbutton[i + 1][ j ].isclicked=true;
}
}
if ( (j + 1 <= ((int)math.sqrt(blocknum)-1) ) && (i + 1 <= ((int)math.sqrt(blocknum)-1)) ) { //检测右下边空格是否是空
if (bombbutton[i + 1][j + 1].isbomb == false && bombbutton[i + 1][j + 1].isclicked == false && bombbutton[i + 1][j + 1].isright == false) {
bombbutton[i + 1][j + 1].settext((bombbutton[i + 1][j + 1].bombroundcount)+"");
bombbutton[i + 1][j + 1].setenabled(false);
bombbutton[i + 1][j + 1].isclicked=true;
}
}
if ( (i – 1 >= 0) && (j – 1 >= 0))//检测左上
isnull(bombbutton,bombbutton[i – 1][j – 1]);
if ( (i – 1 >= 0))
isnull( bombbutton,bombbutton[i – 1][ j ]);//检测上方
if ( (i – 1 >= 0) && (j + 1 <= (int)math.sqrt(blocknum)-1))
isnull( bombbutton,bombbutton[i – 1][j + 1]);//检测右上
if ( (j – 1 >= 0))
isnull(bombbutton,bombbutton[i][j – 1]);//检测左边
if ( (i >= 0) && (j + 1 <= ((int)math.sqrt(blocknum)-1)) )
isnull(bombbutton,bombbutton[i][j + 1]);//检测右边
if ( (j – 1 >= 0) && (i + 1 <= ((int)math.sqrt(blocknum)-1)) )
isnull(bombbutton,bombbutton[i + 1][j – 1]); //检测左下
if ( (i + 1 <= ((int)math.sqrt(blocknum)-1)) ) //检测下
isnull(bombbutton,bombbutton[i + 1][ j ]);
if ( (j + 1 <= ((int)math.sqrt(blocknum)-1)) && (i + 1 <= ((int)math.sqrt(blocknum)-1)) ) //检测右下
isnull(bombbutton,bombbutton[i + 1][j + 1]);
}
}
public void actionperformed(actionevent e)
{
countroundbomb();
if(((bomb)e.getsource()).isbomb==false && ((bomb)e.getsource()).isclicked == false)
{
((bomb)e.getsource()).settext(( ((bomb)e.getsource()).bombroundcount )+"");
((bomb)e.getsource()).isclicked=true;
((bomb)e.getsource()).seticon(null);
((bomb)e.getsource()).setenabled(false);
if((((bomb)e.getsource()).bombroundcount) == 0)
isnull(bombbutton,(bomb)e.getsource());
iswin();
}
else if(((bomb)e.getsource()).isbomb == true)
{
for(int i=0;i<(int)math.sqrt(blocknum);i++)
for(int j=0;j<(int)math.sqrt(blocknum);j++)
{
if(bombbutton[ i ][ j ].isbomb == true)
bombbutton[ i ][ j ].seticon(icon_bomb);
}
((bomb)e.getsource()).seticon(icon_bomb_big);
joptionpane msg = new joptionpane();
joptionpane.showmessagedialog(this,"你踩到地雷了,按确定重来","你踩到地雷了",2);
replay();
}
}
public void mouseclicked(mouseevent e)
{
bomb bombsource = (bomb)e.getsource();
boolean right = swingutilities.isrightmousebutton(e);
if((right == true) && (bombsource.isclicked == false))
{
bombsource.bombflag = (bombsource.bombflag + 1)%3;
if(bombsource.bombflag == 1)
{
if(bombnum > 0 && bombsource.isright == false ){
bombsource.seticon(icon_flag);
bombsource.isright = true;
bombnum–;
}
iswin();
nowbomb.settext("当前雷数"+" "+bombnum+"");
}
else if(bombsource.bombflag == 2)
{
if( (bombnum !=0 ) ||(bombnum ==0 &&(bombsource.geticon()==icon_flag)) )
bombnum++;
bombsource.seticon(icon_question);
nowbomb.settext("当前雷数"+" "+bombnum+"");
}
else if(bombsource.bombflag == 0)
{
bombsource.seticon(null);
bombsource.isright = false;
}
}
}
public void mouseentered(mouseevent e)
{}
public void mousereleased(mouseevent e)
{}
public void mouseexited(mouseevent e)
{}
public void mousepressed(mouseevent e)
{}
}
/*主类*/
public class main
{
public static void main(string args[])
{
(new mainbomb()).show();
}
}
