日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java扫雷教程_java实现简单扫雷游戏

發(fā)布時(shí)間:2024/9/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java扫雷教程_java实现简单扫雷游戏 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文實(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;

/**

* 這個(gè)是一個(gè)簡單的掃雷例子,剛接觸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; // 雷的個(gè)數(shù)

private final int BUTTON_BORDER = 50; // 每個(gè)點(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è)定個(gè)數(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)擊了哪個(gè)按鈕

// 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 + "個(gè)雷");

jl.setVisible(true);

jl.setBounds(20, 20, 500, 30);

this.add(jl);

showTime.setText("已用時(shí):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); //不使用布局管理器,每個(gè)控件的位置用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("錯(cuò)誤", "內(nèi)部錯(cuò)誤");

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的按鈕的個(gè)數(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("勝利", "本次掃雷共用時(shí):" + 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("已用時(shí):" + 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("錯(cuò)誤", "內(nèi)部錯(cuò)誤");

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í)有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的java扫雷教程_java实现简单扫雷游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。