熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

掃雷程序(Java語言)

2022-06-13   來源: 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();

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26581.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.