最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java swing實現(xiàn)的掃雷游戲及改進版完整示例

 更新時間:2017年12月13日 10:59:22   作者:Limbos  
這篇文章主要介紹了java swing實現(xiàn)的掃雷游戲及改進版,結(jié)合完整實例形式對比分析了java使用swing框架實現(xiàn)掃雷游戲功能與相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了java swing實現(xiàn)的掃雷游戲及改進版。分享給大家供大家參考,具體如下:

版本1:

package awtDemo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
 * 這個是一個簡單的掃雷例子,剛接觸swing編寫的,適合新手練習
 * 該程序使用setBounds(x,y,w,h)對控件布局
 * 做法參考win xp自帶的掃雷,當然還寫功能沒做出來,
 * 另外做出來的功能有些還存在bug
 *
 * @author Ping_QC
 */
public class test extends JFrame implements ActionListener, Runnable,
    MouseListener {
  private static final long serialVersionUID = -2417758397965039613L;
  private final int EMPTY     = 0;
  private final int MINE     = 1;
  private final int CHECKED    = 2;
  private final int MINE_COUNT  = 10;  // 雷的個數(shù)
  private final int BUTTON_BORDER = 50;  // 每個點的尺寸
  private final int MINE_SIZE   = 10;  // 界面規(guī)格, 20x20
  private final int START_X    = 20;  // 起始位置x
  private final int START_Y    = 50;  // 起始位置y
  private boolean flag;
  private JButton[][] jb;
  private JLabel jl;
  private JLabel showTime;
  private int[][] map;
  /**
   * 檢測某點周圍是否有雷,周圍點的坐標可由該數(shù)組計算得到
   */
  private int[][] mv = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 },
      { 1, -1 }, { 0, -1 }, { -1, -1 } };
  /**
   * 隨機產(chǎn)生設定個數(shù)的雷
   */
  public void makeMine() {
    int i = 0, tx, ty;
    for (; i < MINE_COUNT;) {
      tx = (int) (Math.random() * MINE_SIZE);
      ty = (int) (Math.random() * MINE_SIZE);
      if (map[tx][ty] == EMPTY) {
        map[tx][ty] = MINE;
        i++; // 不記重復產(chǎn)生的雷
      }
    }
  }
  /**
   * 將button數(shù)組放到frame上,與map[][]數(shù)組對應
   */
  public void makeButton() {
    for (int i = 0; i < MINE_SIZE; i++) {
      for (int j = 0; j < MINE_SIZE; j++) {
        jb[i][j] = new JButton();
        // if (map[i][j] == MINE)
        // jb[i][j].setText(i+","+j);
        // listener add
        jb[i][j].addActionListener(this);
        jb[i][j].addMouseListener(this);
        jb[i][j].setName(i + "_" + j); // 方便點擊是判斷是點擊了哪個按鈕
        // Font font = new Font(Font.SERIF, Font.BOLD, 10);
        // jb[i][j].setFont(font);
        // jb[i][j].setText(i+","+j);
        jb[i][j].setBounds(j * BUTTON_BORDER + START_X, i
            * BUTTON_BORDER + START_Y, BUTTON_BORDER, BUTTON_BORDER);
        this.add(jb[i][j]);
      }
    }
  }
  public void init() {
    flag = false;
    jl.setText("歡迎測試,一共有" + MINE_COUNT + "個雷");
    jl.setVisible(true);
    jl.setBounds(20, 20, 500, 30);
    this.add(jl);
    showTime.setText("已用時:0 秒");
    showTime.setBounds(400, 20, 100, 30);
    this.add(showTime);
    makeMine();
    makeButton();
    this.setSize(550, 600);
    this.setLocation(700, 100);
    this.setResizable(false);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
  }
  public test(String title) {
    super(title);
    this.setLayout(null);  //不使用布局管理器,每個控件的位置用setBounds設定
    jb = new JButton[MINE_SIZE][MINE_SIZE];
    jl = new JLabel();
    showTime = new JLabel();
    map = new int[MINE_SIZE][MINE_SIZE]; // 將按鈕映射到數(shù)組中
  }
  public static void main(String[] args) {
    test test = new test("腳本之家 - 掃雷游戲測試1");
    test.init();
    test.run();
  }
  @Override
  public void actionPerformed(ActionEvent e) {
    Object obj = e.getSource();
    int x, y;
    if ((obj instanceof JButton) == false) {
      showMessage("錯誤", "內(nèi)部錯誤");
      return;
    }
    String[] tmp_str = ((JButton) obj).getName().split("_");
    x = Integer.parseInt(tmp_str[0]);
    y = Integer.parseInt(tmp_str[1]);
    if (map[x][y] == MINE) {
      showMessage("死亡", "你踩到地雷啦~~~");
      flag = true;
      showMine();
      return;
    }
    dfs(x, y, 0);
    checkSuccess();
  }
  /**
   * 每次點擊完后,判斷有沒有把全部雷都找到 通過計算狀態(tài)為enable的按鈕的個數(shù)來判斷
   */
  private void checkSuccess() {
    int cnt = 0;
    for (int i = 0; i < MINE_SIZE; i++) {
      for (int j = 0; j < MINE_SIZE; j++) {
        if (jb[i][j].isEnabled()) {
          cnt++;
        }
      }
    }
    if (cnt == MINE_COUNT) {
      String tmp_str = showTime.getText();
      tmp_str = tmp_str.replaceAll("[^0-9]", "");
      showMessage("勝利", "本次掃雷共用時:" + tmp_str + "秒");
      flag = true;
      showMine();
    }
  }
  private int dfs(int x, int y, int d) {
    map[x][y] = CHECKED;
    int i, tx, ty, cnt = 0;
    for (i = 0; i < 8; i++) {
      tx = x + mv[i][0];
      ty = y + mv[i][1];
      if (tx >= 0 && tx < MINE_SIZE && ty >= 0 && ty < MINE_SIZE) {
        if (map[tx][ty] == MINE) {
          cnt++;// 該點附近雷數(shù)統(tǒng)計
        } else if (map[tx][ty] == EMPTY) {
          ;
        } else if (map[tx][ty] == CHECKED) {
          ;
        }
      }
    }
    if (cnt == 0) {
      for (i = 0; i < 8; i++) {
        tx = x + mv[i][0];
        ty = y + mv[i][1];
        if (tx >= 0 && tx < MINE_SIZE && ty >= 0 && ty < MINE_SIZE
            && map[tx][ty] != CHECKED) {
          dfs(tx, ty, d + 1);
        }
      }
    } else {
      jb[x][y].setText(cnt + "");
    }
    jb[x][y].setEnabled(false);
    return cnt;
  }
  /**
   * 在jl標簽上顯示一些信息
   *
   * @param title
   * @param info
   */
  private void showMessage(String title, String info) {
    jl.setText(info);
    System.out.println("in functino showMessage() : " + info);
  }
  public void run() {
    int t = 0;
    while (true) {
      if (flag) {
        break;
      }
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      t++;
      showTime.setText("已用時:" + t + " 秒");
    }
    // showMine();
  }
  private void showMine() {
//   Icon iconMine = new ImageIcon("e:/mine.jpg");
    for (int i = 0; i < MINE_SIZE; i++) {
      for (int j = 0; j < MINE_SIZE; j++) {
        if (map[i][j] == MINE) {
          jb[i][j].setText("#");
//         jb[i][j].setIcon(iconMine);
        }
      }
    }
  }
  @Override
  public void mouseClicked(MouseEvent e) {
    if (e.getButton() == 3) {
      Object obj = e.getSource();
      if ((obj instanceof JButton) == false) {
        showMessage("錯誤", "內(nèi)部錯誤");
        return;
      }
      String[] tmp_str = ((JButton) obj).getName().split("_");
      int x = Integer.parseInt(tmp_str[0]);
      int y = Integer.parseInt(tmp_str[1]);
    if ("{1}".equals(jb[x][y].getText())) {
        jb[x][y].setText("");
      } else {
        jb[x][y].setText("{1}");
      }
  /*   if(jb[x][y].getIcon() == null){
        jb[x][y].setIcon(new ImageIcon("e:/flag.jpg"));
      }else{
        jb[x][y].setIcon(null);
      }*/
    }
  }
  @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
  }
}

運行效果:

版本2是對上面版本1程序的改進,在基礎(chǔ)不變的基礎(chǔ)上增加了右鍵標記功能以及自主選擇難度功能。

package awtDemo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class saolei extends JFrame implements ActionListener, Runnable,
    MouseListener {
  private final int loEMPTY     = 0;
  private final int loMINE     = 1;
  private final int loCHECKED    = 2;
  private final int loMINE_COUNT  = 10;
  private final int loBUTTON_BORDER = 50;
  private final int loMINE_SIZE   = 10;
  private final int loSTART_X    = 20;
  private final int loSTART_Y    = 50;
  private boolean flag;
  private JButton[][] jb;
  private JLabel jl;
  private JLabel showTime;
  private int[][] map;
  private int[][] mv = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 },
      { 1, -1 }, { 0, -1 }, { -1, -1 } };
  public void makeloMINE() {
    int i = 0, tx, ty;
    for (; i < loMINE_COUNT;) {
      tx = (int) (Math.random() * loMINE_SIZE);
      ty = (int) (Math.random() * loMINE_SIZE);
      if (map[tx][ty] == loEMPTY) {
        map[tx][ty] = loMINE;
        i++;
      }
    }
  }
  public void makeButton() {
    for (int i = 0; i < loMINE_SIZE; i++) {
      for (int j = 0; j < loMINE_SIZE; j++) {
        jb[i][j] = new JButton();
        jb[i][j].addActionListener(this);
        jb[i][j].addMouseListener(this);
        jb[i][j].setName(i + "_" + j);
        jb[i][j].setBounds(j * loBUTTON_BORDER + loSTART_X, i
            * loBUTTON_BORDER + loSTART_Y, loBUTTON_BORDER, loBUTTON_BORDER);
        this.add(jb[i][j]);
      }
    }
  }
  public void init() {
    flag = false;
    jl.setText("歡迎測試,一共有" + loMINE_COUNT + "個雷");
    jl.setVisible(true);
    jl.setBounds(20, 20, 500, 30);
    this.add(jl);
    showTime.setText("已用時:0 秒");
    showTime.setBounds(400, 20, 100, 30);
    this.add(showTime);
    makeloMINE();
    makeButton();
    this.setSize(550, 600);
    this.setLocation(700, 100);
    this.setResizable(false);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
  }
  public saolei(String title) {
    super(title);
    this.setLayout(null);  //不使用布局管理器,每個控件的位置用setBounds設定
    jb = new JButton[loMINE_SIZE][loMINE_SIZE];
    jl = new JLabel();
    showTime = new JLabel();
    map = new int[loMINE_SIZE][loMINE_SIZE]; // 將按鈕映射到數(shù)組中
  }
  public static void main(String[] args) {
   saolei test = new saolei("腳本之家 - 掃雷游戲測試2");
    test.init();
    test.run();
  }
  @Override
  public void actionPerformed(ActionEvent e) {
    Object obj = e.getSource();
    int x, y;
    if ((obj instanceof JButton) == false) {
      showMessage("錯誤", "內(nèi)部錯誤");
      return;
    }
    String[] tmp_str = ((JButton) obj).getName().split("_");
    x = Integer.parseInt(tmp_str[0]);
    y = Integer.parseInt(tmp_str[1]);
    if (map[x][y] == loMINE) {
      showMessage("死亡", "你踩到地雷啦~~~");
      flag = true;
      showloMINE();
      return;
    }
    dfs(x, y, 0);
    checkSuccess();
  }
  private void checkSuccess() {
    int cnt = 0;
    for (int i = 0; i < loMINE_SIZE; i++) {
      for (int j = 0; j < loMINE_SIZE; j++) {
        if (jb[i][j].isEnabled()) {
          cnt++;
        }
      }
    }
    if (cnt == loMINE_COUNT) {
      String tmp_str = showTime.getText();
      tmp_str = tmp_str.replaceAll("[^0-9]", "");
      showMessage("勝利", "本次掃雷共用時:" + tmp_str + "秒");
      flag = true;
      showloMINE();
    }
  }
  private int dfs(int x, int y, int d) {
    map[x][y] = loCHECKED;
    int i, tx, ty, cnt = 0;
    for (i = 0; i < 8; i++) {
      tx = x + mv[i][0];
      ty = y + mv[i][1];
      if (tx >= 0 && tx < loMINE_SIZE && ty >= 0 && ty < loMINE_SIZE) {
        if (map[tx][ty] == loMINE) {
          cnt++;
        } else if (map[tx][ty] == loEMPTY) {
          ;
        } else if (map[tx][ty] == loCHECKED) {
          ;
        }
      }
    }
    if (cnt == 0) {
      for (i = 0; i < 8; i++) {
        tx = x + mv[i][0];
        ty = y + mv[i][1];
        if (tx >= 0 && tx < loMINE_SIZE && ty >= 0 && ty < loMINE_SIZE
            && map[tx][ty] != loCHECKED) {
          dfs(tx, ty, d + 1);
        }
      }
    } else {
      jb[x][y].setText(cnt + "");
    }
    jb[x][y].setEnabled(false);
    return cnt;
  }
  private void showMessage(String title, String info) {
    jl.setText(info);
    System.out.println("in functino showMessage() : " + info);
  }
  public void run() {
    int t = 0;
    while (true) {
      if (flag) {
        break;
      }
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      t++;
      showTime.setText("已用時:" + t + " 秒");
    }
  }
  private void showloMINE() {
    for (int i = 0; i < loMINE_SIZE; i++) {
      for (int j = 0; j < loMINE_SIZE; j++) {
        if (map[i][j] == loMINE) {
          jb[i][j].setText("雷");
        }
      }
    }
  }
  public void mouseClicked(MouseEvent e) {
    if (e.getButton() == 3) {
      Object obj = e.getSource();
      if ((obj instanceof JButton) == false) {
        showMessage("錯誤", "內(nèi)部錯誤");
        return;
      }
      String[] tmp_str = ((JButton) obj).getName().split("_");
      int x = Integer.parseInt(tmp_str[0]);
      int y = Integer.parseInt(tmp_str[1]);
    if ("{1}quot".equals(jb[x][y].getText())) {
        jb[x][y].setText("");
      } else {
        jb[x][y].setText("{1}quot");
      }
    }
  }
  public void mousePressed(MouseEvent e) {
  }
  @Override
  public void mouseReleased(MouseEvent e) {
  }
  public void mouseEntered(MouseEvent e) {
  }
  @Override
  public void mouseExited(MouseEvent e) {
  }
}

運行效果:

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設計有所幫助。

相關(guān)文章

  • 解決Idea運行junit測試時報Error:[3,17]?程序包org.junit不存在的問題

    解決Idea運行junit測試時報Error:[3,17]?程序包org.junit不存在的問題

    這篇文章主要介紹了Idea運行junit測試時報Error:[3,17]?程序包org.junit不存在解決方法,本文給大家分享兩種解決辦法,需要的朋友可以參考下
    2023-03-03
  • Mybatis學習總結(jié)之mybatis使用建議

    Mybatis學習總結(jié)之mybatis使用建議

    這篇文章主要介紹了Mybatis學習總結(jié)之mybatis使用建議的相關(guān)資料,非常具有參考借鑒價值,需要的朋友可以參考下
    2016-05-05
  • Java基礎(chǔ)教程之String深度分析

    Java基礎(chǔ)教程之String深度分析

    這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)教程之String的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • SpringBoot--- SpringSecurity進行注銷權(quán)限控制的配置方法

    SpringBoot--- SpringSecurity進行注銷權(quán)限控制的配置方法

    這篇文章主要介紹了SpringBoot--- SpringSecurity進行注銷,權(quán)限控制,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • fastjson 使用方法詳細介紹

    fastjson 使用方法詳細介紹

    Fastjson是一個Java語言編寫的JSON處理器,由阿里巴巴公司開發(fā)。接下來通過本文給大家分享fastjson 使用方法詳細介紹,感興趣的朋友一起看看吧
    2017-11-11
  • nacos配置中心持久化相關(guān)配置方式

    nacos配置中心持久化相關(guān)配置方式

    這篇文章主要介紹了nacos配置中心持久化相關(guān)配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • SpringMVC實現(xiàn)自定義類型轉(zhuǎn)換器

    SpringMVC實現(xiàn)自定義類型轉(zhuǎn)換器

    本篇文章主要介紹了SpringMVC實現(xiàn)自定義類型轉(zhuǎn)換器 ,詳細的介紹了自定義類型轉(zhuǎn)換器的用法和好處,有興趣的可以了解一下。
    2017-04-04
  • Java使用Iterator迭代器遍歷集合數(shù)據(jù)的方法小結(jié)

    Java使用Iterator迭代器遍歷集合數(shù)據(jù)的方法小結(jié)

    這篇文章主要介紹了Java使用Iterator迭代器遍歷集合數(shù)據(jù)的方法,結(jié)合實例形式分析了java迭代器進行集合數(shù)據(jù)遍歷的常見操作技巧,需要的朋友可以參考下
    2019-11-11
  • java數(shù)據(jù)結(jié)構(gòu)-堆實現(xiàn)優(yōu)先隊列

    java數(shù)據(jù)結(jié)構(gòu)-堆實現(xiàn)優(yōu)先隊列

    通常都把隊列比喻成排隊買東西,大家都很守秩序,先排隊的人就先買東西。但是優(yōu)先隊列有所不同,它不遵循先進先出的規(guī)則,而是根據(jù)隊列中元素的優(yōu)先權(quán),優(yōu)先權(quán)最大的先被取出,這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)-堆實現(xiàn)優(yōu)先隊列,感興趣的朋友一起看看吧
    2021-08-08
  • 如何獲取?Spring?heapdump中的明文密碼

    如何獲取?Spring?heapdump中的明文密碼

    Actuator是Spring?Boot提供的應用系統(tǒng)監(jiān)控的開源框架,在攻防場景里經(jīng)常會遇到Actuator配置不當?shù)那闆r,攻擊者可以直接下載heapdump堆轉(zhuǎn)儲文件,本文介紹如何獲取?Spring?heapdump中的密碼明文,需要的朋友可以參考下
    2022-07-07

最新評論

句容市| 缙云县| 扶风县| 临泽县| 寻乌县| 施甸县| 太保市| 太保市| 丹江口市| 永济市| 北辰区| 台南市| 台南市| 兴仁县| 兴仁县| 道真| 郯城县| 磐石市| 天等县| 大余县| 沧州市| 西充县| 通州市| 和顺县| 遵化市| 新源县| 漠河县| 万安县| 河津市| 清苑县| 武乡县| 成武县| 石河子市| 岳普湖县| 漠河县| 灵石县| 称多县| 崇礼县| 云南省| 高雄县| 怀集县|