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

java實(shí)現(xiàn)簡單掃雷游戲

 更新時間:2020年04月23日 10:47:57   作者:Ping_QC  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)簡單掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下

package com.test.swing;
 
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編寫的,適合新手練習(xí)
 * 該程序使用setBounds(x,y,w,h)對控件布局
 * 做法參考win xp自帶的掃雷,當(dāng)然還寫功能沒做出來,
 * 另外做出來的功能有些還存在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; // 每個點(diǎn)的尺寸
 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;
 /**
 * 檢測某點(diǎn)周圍是否有雷,周圍點(diǎn)的坐標(biāo)可由該數(shù)組計(jì)算得到
 */
 private int[][] mv = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 },
 { 1, -1 }, { 0, -1 }, { -1, -1 } };
 
 /**
 * 隨機(jī)產(chǎn)生設(shè)定個數(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++; // 不記重復(fù)產(chǎn)生的雷
 }
 }
 }
 
 /**
 * 將button數(shù)組放到frame上,與map[][]數(shù)組對應(yīng)
 */
 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); // 方便點(diǎn)擊是判斷是點(diǎn)擊了哪個按鈕
 // 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設(shè)定
 
 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("Hello Miner!");
 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();
 }
 
 /**
 * 每次點(diǎn)擊完后,判斷有沒有把全部雷都找到 通過計(jì)算狀態(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++;// 該點(diǎn)附近雷數(shù)統(tǒng)計(jì)
 } 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標(biāo)簽上顯示一些信息
 * 
 * @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}quot;.equals(jb[x][y].getText())) {
 jb[x][y].setText("");
 } else {
 jb[x][y].setText("{1}quot;);
 }
 /* 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
 
 }
}

更多精彩游戲,請參考專題《java經(jīng)典小游戲》

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java實(shí)現(xiàn)后臺數(shù)據(jù)顯示在前端

    java實(shí)現(xiàn)后臺數(shù)據(jù)顯示在前端

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)后臺數(shù)據(jù)顯示在前端,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • java 格式化輸出數(shù)字的方法

    java 格式化輸出數(shù)字的方法

    在實(shí)際工作中,常常需要設(shè)定數(shù)字的輸出格式,如以百分比的形式輸出,或者設(shè)定小數(shù)位數(shù)等,現(xiàn)稍微總結(jié)如下
    2014-01-01
  • 解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題

    解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題

    這篇文章主要介紹了解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot項(xiàng)目中如何訪問HTML頁面

    SpringBoot項(xiàng)目中如何訪問HTML頁面

    這篇文章主要介紹了SpringBoot項(xiàng)目中如何訪問HTML頁面,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • springboot整合mybatis-plus基于注解實(shí)現(xiàn)一對一(一對多)查詢功能

    springboot整合mybatis-plus基于注解實(shí)現(xiàn)一對一(一對多)查詢功能

    這篇文章主要介紹了springboot整合mybatis-plus基于純注解實(shí)現(xiàn)一對一(一對多)查詢功能,因?yàn)楸救瞬捎玫氖莝pring-boot進(jìn)行開發(fā),本身springboot就提倡采用不用配置自動配置的方式,所以真心希望mybatis(不是mybatis-plus)這點(diǎn)需要繼續(xù)努力
    2021-09-09
  • 帶你輕松了解Modbus協(xié)議

    帶你輕松了解Modbus協(xié)議

    這篇文章主要給大家介紹了關(guān)于Modbus協(xié)議的相關(guān)資料,此協(xié)議定義了一個控制器能認(rèn)識使用的消息結(jié)構(gòu),而不管它們是經(jīng)過何種網(wǎng)絡(luò)進(jìn)行通信的,需要的朋友可以參考下
    2021-11-11
  • Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏

    Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏

    這篇文章主要介紹了利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏,首先在需要進(jìn)行脫敏的VO字段上面標(biāo)注相關(guān)脫敏注解,具體實(shí)例代碼文中給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • Spring Boot中@RequestParam參數(shù)的5種情況說明

    Spring Boot中@RequestParam參數(shù)的5種情況說明

    這篇文章主要介紹了Spring Boot中@RequestParam參數(shù)的5種情況說明,具有很好的參考價值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Mybatis-Plus更新時間字段不生效的解決

    Mybatis-Plus更新時間字段不生效的解決

    在使用Mybatis-Plus時,可能會遇到updateTime字段不自動更新的問題,通過分析,原因在于selectById獲取舊數(shù)據(jù)后,如果字段已有值,更新操作不會自動填充更新時間,可以通過直接在實(shí)體中設(shè)置更新時間或在更新操作時指定時間來解決此問題
    2024-09-09
  • java聯(lián)調(diào)生成測試數(shù)據(jù)工具類方式

    java聯(lián)調(diào)生成測試數(shù)據(jù)工具類方式

    這篇文章主要介紹了java聯(lián)調(diào)生成測試數(shù)據(jù)工具類方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論

长兴县| 巩留县| 南京市| 陆丰市| 平泉县| 崇州市| 玛纳斯县| 九江县| 友谊县| 吕梁市| 江孜县| 万山特区| 河津市| 汉寿县| 类乌齐县| 会同县| 子长县| 门源| 抚顺县| 修水县| 尚志市| 桂平市| 兴海县| 大兴区| 米脂县| 临桂县| 丽水市| 宁武县| 墨脱县| 河池市| 揭东县| 舞钢市| 双流县| 大余县| 阿拉善左旗| 九寨沟县| 田东县| 大化| 漯河市| 中宁县| 定陶县|