java

位置:IT落伍者 >> java >> 浏览文章

扫雷程序(Java语言)


发布日期:2019年08月02日
 
扫雷程序(Java语言)

import javaawt*;

import javaawtevent*;

import javamath*;

import javautil*;

import javaxswing*;

//程序入口

public class JavaClearBomb {

public static void main(String args[]){

DrawInterFace DIF=new DrawInterFace();

DIFRandomMine();

}

}

//生成界面

class DrawInterFace{

//地雷标志无雷有雷

private static int fmine[]=new int[];

//右键记录标志

private static boolean rmouse;

//用来存放总地雷数

private static int fcount=;

//存放剩余地雷数

private static int rcount;

//存放随机生成的数

private static int r[]=new int[];

//记录标签是否翻开标志

private static boolean appear[]=new boolean[];

//记录时间

private static long time=;

//标记线程

private boolean stop = true;

//标记又右键

private static boolean rightclick[]=new boolean[];

private Frame fr;

private Menu mm;

private MenuBar mb;

private MenuItem mimimimimimimimimih;

private CheckboxMenuItem mimimi;

//用CardLayout来放置按钮和标签plbmine里包含lbmine和btmine为了实现CardLayout的功能

private Panel puppdown;

private Label lbleftlbrightlbover;

private Button btagainbtexit;

private JButton btstart;

private static Button btmine[];

private static Label lbmine[];

private static Panel plbmine[];

private int minenum=;

private int mine[]=new int[];

private static CardLayout CLayout=new CardLayout();

private Dialog digOver;

public DrawInterFace(){

//生成菜单栏

fr=new Frame(扫雷);

mb=new MenuBar();

frsetMenuBar(mb);

m=new Menu(游戏);

m=new Menu(帮助);

mbadd(m);

mbsetHelpMenu(m);

mi=new MenuItem(开局);

mi=new MenuItem(初级);

mi=new MenuItem(中级);

mi=new MenuItem(高级);

mi=new MenuItem(自定义);

mi=new CheckboxMenuItem(标记);

mi=new CheckboxMenuItem(颜色);

mi=new CheckboxMenuItem(声音);

mi=new MenuItem(扫雷英雄榜);

mi=new MenuItem(退出);

mih=new MenuItem(河海大学);

mi=new MenuItem(计算机周挺川);

madd(mi);

maddSeparator();

madd(mi);

madd(mi);

madd(mi);

madd(mi);

maddSeparator();

madd(mi);

madd(mi);

madd(mi);

maddSeparator();

madd(mi);

maddSeparator();

madd(mi);

madd(mih);

maddSeparator();

madd(mi);

frsetMenuBar(mb);

mbadd(m);

mbadd(m);

frsetResizable(false);

frsetLocation();

//生成时间显示区雷数显示区和开始游戏按钮

pup=new Panel();

pdown=new Panel();

pupsetLayout(new FlowLayout());

lbleft=new Label();

lbleftsetAlignment(LabelCENTER);

lbright=new Label();

lbrightsetAlignment(LabelCENTER);

btstart=new JButton(new ImageIcon(image/minegif));

// lbleftsetLocation();

// lbrightsetLocation();

// btstartsetBounds();

pupadd(lbleft);

pupadd(btstart);

pupadd(lbright);

//对话框

lbover=new Label(踩到雷了);

btagain=new Button(再玩一局);

btexit=new Button(算了);

digOver=new Dialog(fr你输了);

digOversetLayout(new FlowLayout());

digOversetResizable(false);

digOversetSize();

digOveradd(lbover);

digOveradd(btagain);

digOveradd(btexit);

btagainaddActionListener(new Restart());

btexitaddActionListener(new Exit());

digOversetLocation();

//生成雷区

pdownsetLayout(new GridLayout());

btmine=new Button[];

lbmine=new Label[];

plbmine=new Panel[];

for(int i=;i<;i++)

{

btmine[i]=new Button();

lbmine[i]=new Label();

lbmine[i]setBackground(ColorLIGHT_GRAY);

lbmine[i]setAlignment(LabelCENTER);//设定标签文字居中显示

plbmine[i]=new Panel();

}

for(int i=;i<;i++)

{

plbmine[i]setLayout(CLayout);

plbmine[i]add(btmine[i]up);

plbmine[i]add(lbmine[i]up);

pdownadd(plbmine[i]down);

}

//设定雷区按钮的监听

for(int i=;i<;i++)

{

btmine[i]addActionListener(new BombAction());

btmine[i]addMouseListener(new UnitEvent(i));

}

//设定开始按钮的监听

btstartaddActionListener(new Start());

//画出panel

fradd(pupNorth);

fradd(pdownSouth);

frsetSize();

frsetVisible(true);

fraddWindowListener(new WindowClosing());

//初始化

for(int i=;i<;i++)

{

fmine[i]=;

appear[i]=false;

rightclick[i]=false;

}

rmouse=false;

rcount=;

lbrightsetText(IntegertoString(rcount));

}

//随机生成个大小介于之间的整数并对雷进行标记(算法有待优化)

public static void RandomMine(){

//存到fmine[i]中为雷没有

Random rm=new Random();

for(int i=;i<;i++){

r[i]=rmnextInt();

for(int j=;j<i;j++){

while((r[i]==r[j])&&(i!=j)){

r[i]=rmnextInt();

};

}

}

for(int i=;i<;i++)

{

fmine[r[i]]=;

}

}

//计算某方格周围的雷数

static int HowManyMine(int x){

int temp=;

switch (x){

//左上角

case : temp=fmine[x+]+fmine[x+]+fmine[x+];break;

//右上角

case : temp=fmine[x]+fmine[x+]+fmine[x+];break;

//左下角

case : temp=fmine[x]+fmine[x]+fmine[x+];break;

//右下角

case : temp=fmine[x]+fmine[x]+fmine[x];break;

//上边

case :

case :

case :

case :

case :

case :

case : temp=fmine[x]+fmine[x+]+fmine[x+]+fmine[x+]+fmine[x+];break;

//下边

case :

case :

case :

case :

case :

case :

case : temp=fmine[x]+fmine[x]+fmine[x]+fmine[x]+fmine[x+];break;

//左边

case :

case :

case :

case :

case :

case :

case : temp=fmine[x]+fmine[x]+fmine[x+]+fmine[x+]+fmine[x+];break;

//右边

case :

case :

case :

case :

case :

case :

case : temp=fmine[x]+fmine[x]+fmine[x]+fmine[x+]+fmine[x+];break;

//其余

default: temp=fmine[x]+fmine[x]+fmine[x]+fmine[x]+fmine[x+]+fmine[x+]+fmine[x+]+fmine[x+];break;

}

return temp;

}

//判断标签是否已经显示

////展开雷区方法(此算法严重需要改进)

static void ClickMine(int i){

CLayoutlast(plbmine[i]);

if(fmine[i]==) {

Systemoutprintln(踩到雷了);

return;

}

else {

if(HowManyMine(i)==)

{

CLayoutlast(plbmine[i]);

appear[i]=true;

}

else{

CLayoutlast(plbmine[i]);

appear[i]=true;

return;

}

}

btmine[i]setVisible(false);

switch (i){

//左上角

case : {if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}break;}

//右上角

case : {if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}break;}

//左下角

case : {if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}break;}

//右下角

case : {if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}break;}

//上边

case :

case :

case :

case :

case :

case :

case : {if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}break;}

//下边

case :

case :

case :

case :

case :

case :

case : {if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}break;}

//左边

case :

case :

case :

case :

case :

case :

case : {if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}break;}

//右边

case :

case :

case :

case :

case :

case :

case : {if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}break;}

//其余

default: {if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i]==false){CLayoutlast(plbmine[i]);ClickMine(i);appear[i]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}if(appear[i+]==false){CLayoutlast(plbmine[i+]);ClickMine(i+);appear[i+]=true;}break;}

}

}

//内部类实现监听实现扫雷中心算法

class BombAction implements ActionListener{

public void actionPerformed(ActionEvent e){

if(stop==true){

new Clock()start();

}

stop = false;

for (int i=;i<;i++){

if (egetSource()==btmine[i]&&rightclick[i]==true) return;

}

for (int i=;i<;i++){

lbmine[i]setText(IntegertoString(HowManyMine(i)));

if(HowManyMine(i)==) {lbmine[i]setText();}

if (egetSource()==btmine[i])

{

if(fmine[i]==)

{

//判断是否失败

stop=true;

digOversetVisible(true);

frsetEnabled(false);

}

else

{

//实现展开雷区(较难算法)

ClickMine(i);

//判断是否胜利(要改进)

int win=;

for(int j=;j<;j++){

if(appear[j]==true) win++;

}

if(win==){

stop=true;

lboversetText(用时+time+);

digOversetTitle(你赢了);

digOversetVisible(true);

}

}

}

}

}

}

//右键标记

//线程实现记时

class Clock extends Thread{

public void run(){

while(stop==false){

try{

sleep();

}

catch(InterruptedException e){

Systemoutprintln(egetMessage());

}

time++;

lbleftsetText(StringvalueOf(time));

}

return;

}

}

//开始按钮的监听

class Start implements ActionListener{

public void actionPerformed(ActionEvent e){

for(int i=;i<;i++)

{

fmine[i]=;

appear[i]=false;

lbmine[i]setText();

btmine[i]setVisible(true);

CLayoutfirst(plbmine[i]);

btmine[i]setLabel();

btmine[i]setEnabled(true);

rightclick[i]=false;

}

btstartsetIcon(new ImageIcon(image/minegif));

RandomMine();

rcount=;

lbrightsetText(IntegertoString(rcount));

//初始化线程

stop = true;

time=;

lbleftsetText(LongtoString(time));

}

}

//右击事件

class UnitEvent extends MouseAdapter{

private int i;

public UnitEvent(int i){

thisi=i;

}

public void mouseClicked(MouseEvent e){

if(egetModifiers()==InputEventBUTTON_MASK){

}

if(egetModifiers()==InputEventBUTTON_MASK){

}

if(egetModifiers()==InputEventBUTTON_MASK){

if(btmine[i]getLabel()==){

btmine[i]setLabel();

rightclick[i]=true;

rcount;

lbrightsetText(StringvalueOf(rcount));

return;

}

if(btmine[i]getLabel()==){

btmine[i]setLabel(?);

btmine[i]setEnabled(true);

rightclick[i]=false;

rcount++;

lbrightsetText(StringvalueOf(rcount));

return;

}

if(btmine[i]getLabel()==?){

btmine[i]setLabel();

return;

}

}

}

}

//对话框按钮的监听

class Restart implements ActionListener{

public void actionPerformed(ActionEvent e){

new Start()actionPerformed(e);

digOversetVisible(false);

frsetEnabled(true);

lboversetText(踩到雷了);

digOversetTitle(你输了);

}

}

class Exit implements ActionListener{

public void actionPerformed(ActionEvent e){

Systemexit();

}

}

//内部类关闭窗口

class WindowClosing extends WindowAdapter{

public void windowClosing(WindowEvent e){

Systemexit();

}

}

}

               

上一篇:JAVA垃圾回收算法摘要

下一篇:64位环境中的Java