导航:首页 > 编程语言 > 扫雷程序java

扫雷程序java

发布时间:2023-03-11 18:27:09

⑴ 用java怎么写扫雷程序

首先要写一个UI,也就是操作界面,使用java.swing.*内的东西就可以搞定;
其次写一个hander,也就是具体的按钮响应,UI的初始化(哪里有雷),怎么触发雷和其他的;
一般来说简单的扫雷模型就好了,如果需要更有意思点,可以写一些数据库的操作内容的tool类具体的就是处理历史操作记录,场均数据或多人竞技的特点。
如果你是说你没有设计思路,我可以给你个提示:递归算法是触发扫雷的方法,初始化用随机数来做。

⑵ 怎么用Java做一个扫雷程序,要原创。。。 做好了给加100

第一个JAVA文件

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* 显示所有按钮的面板
* @author Administrator
*
*/
public class AllButtonPanel extends JPanel implements ActionListener{
private int row;//行数
private int col;//列数
private int mineCount;//地雷数
private MineButton[][] allButtons;//所有按钮

public AllButtonPanel(int row,int col,int mineCount){
this.row=row;
this.col=col;
this.mineCount=mineCount;
allButtons=new MineButton[row][col];
createButtons();

createMine();
init();
}

private void init(){
this.setLayout(new GridLayout(row,col));
for(int i=0;i<allButtons.length;i++){
for(int j=0;j<allButtons[i].length;j++){
this.add(allButtons[i][j]);
}
}
}

/**
* 随机布雷的方法
*
*/
private void createMine(){
int n=0;
while(n<mineCount){//随机生成mineCount个地雷
int i=(int)(Math.random()*row);
int j=(int)(Math.random()*col);
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(-1);
n++;
}
}

for(int i=0;i<allButtons.length;i++){//计算每个位置的周围地雷数
for(int j=0;j<allButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(getSurroundMineCount(i,j));
}
}
}
}

/**
* 统计(i,j)坐标周围8个位置的地雷数
* @param data
* @param i
* @param j
* @return
*/
private int getSurroundMineCount(int i,int j){
int num=0;//统计周围的雷数
if(i-1>=0&&j-1>=0){
num+=(allButtons[i-1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i-1>=0){
num+=(allButtons[i-1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i-1>=0&&j+1<allButtons[0].length){
num+=(allButtons[i-1][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(j-1>=0){
num+=(allButtons[i][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(j+1<allButtons[0].length){
num+=(allButtons[i][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length&&j-1>=0){
num+=(allButtons[i+1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length){
num+=(allButtons[i+1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length&&j+1<allButtons[0].length){
num+=(allButtons[i+1][j+1].getCountOfSurroundMines()==-1?1:0);
}
return num;
}

/**
* 生成按钮
*
*/
private void createButtons(){
for(int i=0;i<allButtons.length;i++){
for(int j=0;j<allButtons[i].length;j++){
allButtons[i][j]=new MineButton(i,j);
allButtons[i][j].setSize(6,6);
allButtons[i][j].addActionListener(this);//添加点击事件监听
allButtons[i][j].addMouseListener(new MouseAdapter(){//添加鼠标右键事件监听
public void mouseClicked(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON3){
int remain=Integer.parseInt(CleanMine.remainMine.getText());
JButton b=(JButton)e.getSource();
if(b.getText().equals("")){
remain--;
CleanMine.remainMine.setText(remain+"");
b.setText("&");
}else if(b.getText().equals("&")){
remain++;
CleanMine.remainMine.setText(remain+"");
b.setText("");
}
}
}
});
}
}
}

public void actionPerformed(ActionEvent e) {//点击事件监听的方法
MineButton b=(MineButton)e.getSource();
int r=b.getRow();
int c=b.getCol();
if(allButtons[r][c].getCountOfSurroundMines()==-1){//如果是地雷
for(int i=0;i<allButtons.length;i++){//把所有按钮都显示出来
for(int j=0;j<allButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()==-1){//如果该位置是地雷
allButtons[i][j].setText("$");
}else if(allButtons[i][j].getCountOfSurroundMines()==0){//如果该位置为空(该位置不是地雷,周围8个位置也没有地雷)
allButtons[i][j].setText("");
allButtons[i][j].setBackground(Color.CYAN);
}else{//如果该位置不是地雷,但周围8个位置中有地雷
allButtons[i][j].setText(allButtons[i][j].getCountOfSurroundMines()+"");
allButtons[i][j].setBackground(Color.CYAN);
}
}
}
}else{//如果不是地雷
showEmpty(r,c);//执行排空操作
}
}

/**
* 排空方法,若(i,j)位置为空,则显示空白。然后依次递归找它周围的8个位置。
* @param data
* @param i
* @param j
*/
private void showEmpty(int i,int j){
MineButton b=allButtons[i][j];
if(b.isCleared()){
return;
}
if(allButtons[i][j].getCountOfSurroundMines()==0){
b.setBackground(Color.CYAN);
b.setCleared(true);
if(i-1>=0&&j-1>=0){
showEmpty(i-1,j-1);
}
if(i-1>=0){
showEmpty(i-1,j);
}
if(i-1>=0&&j+1<allButtons[0].length){
showEmpty(i-1,j+1);
}
if(j-1>=0){
showEmpty(i,j-1);
}
if(j+1<allButtons[0].length){
showEmpty(i,j+1);
}
if(i+1<allButtons.length&&j-1>=0){
showEmpty(i+1,j-1);
}
if(i+1<allButtons.length){
showEmpty(i+1,j);
}
if(i+1<allButtons.length&&j+1<allButtons[0].length){
showEmpty(i+1,j+1);
}
}else if(allButtons[i][j].getCountOfSurroundMines()>0){
b.setText(allButtons[i][j].getCountOfSurroundMines()+"");
b.setBackground(Color.CYAN);
b.setCleared(true);
}
}
}

第二个JAVA文件

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* 扫雷游戏主界面
* @author tony.tang
*
*/
public class CleanMine extends JFrame implements ActionListener{
private JLabel text1,text2;
public static JLabel remainMine;//剩余地雷数
private JLabel time;//消耗时间
private JButton reset;//重新开始
private JPanel center;
private int row,col,mine;

public CleanMine(){
text1=new JLabel("剩余地雷:");
text2=new JLabel("消耗时间:");
remainMine=new JLabel("10");
time=new JLabel("0");
reset=new JButton("重新开始");
reset.addActionListener(this);
JMenuBar bar=new JMenuBar();
JMenu game=new JMenu("游戏");
JMenu help=new JMenu("帮助");
JMenuItem item;
game.add(item=new JMenuItem("开局"));item.addActionListener(this);
game.addSeparator();
ButtonGroup bg=new ButtonGroup();
game.add(item=new JCheckBoxMenuItem("初级",true));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("中级"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("高级"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("自定义..."));bg.add(item);item.addActionListener(this);
game.addSeparator();
game.add(item=new JMenuItem("退出"));item.addActionListener(this);

help.add(item=new JMenuItem("查看帮助"));item.addActionListener(this);
help.add(item=new JMenuItem("关于扫雷..."));item.addActionListener(this);

bar.add(game);
bar.add(help);

this.setJMenuBar(bar);

init();
}

private void init(){
JPanel north=new JPanel();
north.add(text1);
north.add(remainMine);
north.add(reset);
north.add(text2);
north.add(time);

this.add(north,BorderLayout.NORTH);
this.row=9;
this.col=9;
this.mine=10;
restart();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

new Thread(){
public void run(){
while(Integer.parseInt(remainMine.getText())>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
time.setText((Integer.parseInt(time.getText())+1)+"");
}
}
}.start();
}

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("初级")){
this.row=9;
this.col=9;
this.mine=10;
restart();
return;
}
if(e.getActionCommand().equals("中级")){
this.row=16;
this.col=16;
this.mine=40;
restart();
return;
}
if(e.getActionCommand().equals("高级")){
this.row=16;
this.col=30;
this.mine=99;
restart();
return;
}
if(e.getActionCommand().equals("重新开始")){
restart();
return;
}
}

private void restart(){
if(center!=null){
this.remove(center);
}
center=new AllButtonPanel(row,col,mine);
this.add(center,BorderLayout.CENTER);
this.remainMine.setText(mine+"");
this.time.setText("0");
this.setSize(col*30,row*30+10);
this.setResizable(false);
this.setVisible(true);
}

/**
* @param args
*/
public static void main(String[] args) {
new CleanMine();

}
}
第三个JAVA文件.

import javax.swing.JButton;
import java.awt.*;

public class MineButton extends JButton {
private int row;
private int col;
private boolean cleared=false;
private int countOfSurroundMines;//周围地雷数,如果本按钮是雷,则为-1;

public MineButton(int row,int col){
this.row=row;
this.col=col;
this.setMargin(new Insets(0,0,0,0));
}

public int getCol() {
return col;
}

public int getRow() {
return row;
}

public boolean isCleared() {
return cleared;
}

public void setCleared(boolean cleared) {
this.cleared = cleared;
}

public int getCountOfSurroundMines() {
return countOfSurroundMines;
}

public void setCountOfSurroundMines(int countOfSurroundMines) {
this.countOfSurroundMines = countOfSurroundMines;
}

}

全部编译以后就可以执行了

⑶ java 扫雷源代码

建议去学习Java相关知识,首先是图形界面编程,把局不出来,然后是功能实现

⑷ 运行在Eclipse环境下的java扫雷游戏的初级代码是什么

import java.awt.Button;x0dx0aimport java.util.Set;x0dx0a// 每一个小方块类x0dx0apublic class Diamond extends Button {x0dx0aprivate Diamond[] diamonds;x0dx0ax0dx0a// 该小方块周围的八个方向上的小方块x0dx0aprivate Diamond east;x0dx0aprivate Diamond north;x0dx0aprivate Diamond northEast;x0dx0aprivate Diamond northWest;x0dx0aprivate Diamond south;x0dx0aprivate Diamond southEast;x0dx0aprivate Diamond southWest;x0dx0aprivate Diamond west;x0dx0ax0dx0aprivate boolean isBomb;// 是否是雷x0dx0aprivate boolean isChange;// 又没有被翻过x0dx0aprivate int no;// 产生的方块的编号x0dx0ax0dx0a// 持有所有小方块的引用,方便进行操作x0dx0apublic Diamond(Diamond[] diamonds) {x0dx0athis.diamonds = diamonds;x0dx0a}x0dx0ax0dx0a// 按键时方块发生改变x0dx0apublic boolean change() {x0dx0athis.isChange = true;// 说明已经翻过了x0dx0aif(isBomb) {// 触雷x0dx0a//this.setBackground(Color.red);x0dx0areturn true;x0dx0a} else {// 不是雷,就显示周围雷的数目x0dx0a//this.setLabel(this.getNearBombNo() + "");x0dx0athis.setLabel(this.getNearBombNo() + "");x0dx0a//if(this.getNearBombNo() == 0) {x0dx0a//this.moveon();x0dx0a//}x0dx0areturn false;x0dx0a}x0dx0a}x0dx0ax0dx0a// 获得该小方块周围雷的数量x0dx0apublic int getNearBombNo() {x0dx0aint no = 0;x0dx0aif(this.northWest != null && this.northWest.isBomb) no++;x0dx0aif(this.north != null && this.north.isBomb) no++;x0dx0aif(this.northEast != null && this.northEast.isBomb) no++;x0dx0aif(this.east != null && this.east.isBomb) no++;x0dx0aif(this.southEast != null && this.southEast.isBomb) no++;x0dx0aif(this.south != null && this.south.isBomb) no++;x0dx0aif(this.southWest != null && this.southWest.isBomb) no++;x0dx0aif(this.west != null && this.west.isBomb) no++;x0dx0ax0dx0areturn no;x0dx0a}x0dx0ax0dx0a// 获得该小方块周围的小方块x0dx0apublic Diamond getNearDimaond(int i) {x0dx0aint index = -1;x0dx0aswitch (i) {x0dx0acase 1:// 1表示西北,2,表示北,以此类推x0dx0aindex = no - 10;x0dx0aif(index < 1 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 2:x0dx0aindex = no - 9;x0dx0aif(index < 1) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 3:x0dx0aindex = no - 8;x0dx0aif(index < 1 || no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 4:x0dx0aindex = no + 1;x0dx0aif(no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 5:x0dx0aindex = no + 10;x0dx0aif(index >= 81 ||no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 6:x0dx0aindex = no + 9;x0dx0aif(index > 81) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 7:x0dx0aindex = no + 8;x0dx0aif(index >= 81 || no==1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0acase 8:x0dx0aindex = no - 1;x0dx0aif(no==1 || no==10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {x0dx0areturn null;x0dx0a} else {x0dx0areturn diamonds[index];x0dx0a}x0dx0a}x0dx0areturn null;x0dx0a}x0dx0ax0dx0a// 递归,set是用来装已经翻过的小方块的,不然会死循环,为什么用set,因为set是不重复的x0dx0apublic void moveon(Set set) {x0dx0ax0dx0aset.add(this);// 先把自己加上x0dx0aif(this.getNorthWest() != null && this.getNorthWest().isBomb == false) {x0dx0athis.getNorthWest().change();x0dx0ax0dx0aif(this.getNorthWest().getNearBombNo() == 0) {x0dx0aif(set.contains(this.getNorthWest()) == false)x0dx0athis.getNorthWest().moveon(set);x0dx0a}x0dx0ax0dx0aset.add(this.getNorthWest());x0dx0a}x0dx0ax0dx0aif(this.getNorth() != null && this.getNorth().isBomb == false) {x0dx0athis.getNorth().change();x0dx0aif(this.getNorth().getNearBombNo() == 0) {x0dx0aif(set.contains(this.getNorth()) == false)x0dx0athis.getNorth().moveon(set);x0dx0a}x0dx0ax0dx0aset.add(this.getNorth());x0dx0a} x0dx0ax0dx0aif(this.getNorthEast() != null && this.getNorthEast().isBomb == false) {x0dx0athis.getNorthEast().change();x0dx0aif(this.getNorthEast().getNearBombNo() == 0) {x0dx0aif(set.contains(this.getNorthEast()) == false)x0dx0athis.getNorthEast().moveon(set);x0dx0a}x0dx0ax0dx0aset.add(this.getNorthEast());x0dx0a} x0dx0ax0dx0aif(this.getEast() != null && this.getEast().isBomb == false) {x0dx0athis.getEast().change();x0dx0aif(this.getEast().getNearBombNo() == 0) {x0dx0aif(set.contains(this.getEast()) == false)x0dx0athis.getEast().moveon(set);x0dx0a}x0dx0ax0dx0aset.add(this.getEast());x0dx0a} x0dx0ax0dx0aif(this.getSouthEast() != null && this.getSouthEast().isBomb == false) {x0dx0athis.getSouthEast().change();x0dx0aif(this.getSouthEast().getNearBombNo() == 0) {x0dx0aif(set.contains(this.getSouthEast()) == false)x0dx0athis.getSouthEast().moveon(set);x0dx0a}x0dx0ax0dx0aset.add(this.getSouthEast());x0dx0a} x0dx0ax0dx0aif(this.getSouth() != null && this.getSouth().isBomb == false) {x0dx0athis.getSouth().change();x0dx0aif(this.getSouth().getNearBombNo() == 0) {x0dx0aif(set.contains(this.getSouth()) == false)x0dx0athis.getSouth().moveon(set);x0dx0a}x0dx0ax0dx0aset.add(this.getSouth());x0dx0a} x0dx0ax0dx0aif(this.getSouthWest() != null && this.getSouthWest().isBomb == false) {x0dx0athis.getSouthWest().change();x0dx0aif(this.getSouthWest().getNearBombNo() == 0) {x0dx0aif(set.contains(this.getSouthWest()) == false)x0dx0athis.getSouthWest().moveon(set);x0dx0a}x0dx0ax0dx0aset.add(this.getSouthWest());x0dx0a} x0dx0ax0dx0aif(this.getWest() != null && this.getWest().isBomb == false) {x0dx0athis.getWest().change();x0dx0aif(this.getWest().getNearBombNo() == 0) {x0dx0aif(set.contains(this.getWest()) == false)x0dx0athis.getWest().moveon(set);x0dx0a}x0dx0ax0dx0aset.add(this.getWest());x0dx0a} x0dx0a}x0dx0ax0dx0a/*public Diamond[] getDiamonds() {x0dx0areturn diamonds;x0dx0a}*/x0dx0ax0dx0apublic Diamond getEast() {x0dx0areturn east;x0dx0a}x0dx0ax0dx0apublic int getNo() {x0dx0areturn no;x0dx0a}x0dx0ax0dx0apublic Diamond getNorth() {x0dx0areturn north;x0dx0a}x0dx0ax0dx0apublic Diamond getNorthEast() {x0dx0areturn northEast;x0dx0a}x0dx0ax0dx0apublic Diamond getNorthWest() {x0dx0areturn northWest;x0dx0a}x0dx0ax0dx0apublic Diamond getSouth() {x0dx0areturn south;x0dx0a}x0dx0ax0dx0apublic Diamond getSouthEast() {x0dx0areturn southEast;x0dx0a}x0dx0ax0dx0apublic Diamond getSouthWest() {x0dx0areturn southWest;x0dx0a}x0dx0ax0dx0apublic Diamond getWest() {x0dx0areturn west;x0dx0a}x0dx0ax0dx0apublic boolean isBomb() {x0dx0areturn isBomb;x0dx0a}x0dx0ax0dx0apublic boolean isChange() {x0dx0areturn isChange;x0dx0a}x0dx0ax0dx0apublic void setBomb(boolean isBomb) {x0dx0athis.isBomb = isBomb;x0dx0a}x0dx0ax0dx0apublic void setChange(boolean isChange) {x0dx0athis.isChange = isChange;x0dx0a}x0dx0ax0dx0apublic void setDiamonds(Diamond[] diamonds) {x0dx0athis.diamonds = diamonds;x0dx0a}x0dx0ax0dx0apublic void setEast(Diamond east) {x0dx0athis.east = east;x0dx0a}x0dx0ax0dx0apublic void setNo(int no) {x0dx0athis.no = no;x0dx0a}x0dx0ax0dx0apublic void setNorth(Diamond north) {x0dx0athis.north = north;x0dx0a}x0dx0ax0dx0apublic void setNorthEast(Diamond northEast) {x0dx0athis.northEast = northEast;x0dx0a}x0dx0ax0dx0apublic void setNorthWest(Diamond northWest) {x0dx0athis.northWest = northWest;x0dx0a}x0dx0ax0dx0apublic void setSouth(Diamond south) {x0dx0athis.south = south;x0dx0a}x0dx0ax0dx0apublic void setSouthEast(Diamond southEast) {x0dx0athis.southEast = southEast;x0dx0a}x0dx0ax0dx0apublic void setSouthWest(Diamond southWest) {x0dx0athis.southWest = southWest;x0dx0a}x0dx0ax0dx0apublic void setWest(Diamond west) {x0dx0athis.west = west;x0dx0a}x0dx0ax0dx0a}

⑸ 求一个java扫雷游戏的程序源代码,尽量多点注释,要确实可用的!急急急急急急急急急急急急!!!!!

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.Timer;

public class ScanLei1 extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;
private Container contentPane;
private JButton btn;
private JButton[] btns;
private JLabel b1;
private JLabel b2;
private JLabel b3;
private Timer timer;
private int row=9;
private int col=9;
private int bon=10;
private int[][] a;
private int b;
private int[] a1;
private JPanel p,p1,p2,p3;

public ScanLei1(String title){
super(title);
contentPane=getContentPane();
setSize(297,377);
this.setBounds(400, 100, 400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
timer =new Timer(1000,(ActionListener) this);
a = new int[row+2][col+2];
initGUI();
}

public void initGUI(){
p3=new JPanel();
b=bon;
JMenuBar menuBar=new JMenuBar();
JMenu menu1=new JMenu("游戏");
JMenu menu2=new JMenu("帮助");
JMenuItem mi1=new JMenuItem("初级");
JMenuItem mi2 = new JMenuItem("中级");
JMenuItem mi3 =new JMenuItem("高级");
mi1.addActionListener(this);
menu1.add(mi1);
mi2.addActionListener(this);
menu1.add(mi2);
mi3.addActionListener(this);
menu1.add(mi3);
menuBar.add(menu1);
menuBar.add(menu2);
p3.add(menuBar);
b1=new JLabel(bon+"");
a1=new int[bon];
btn =new JButton("开始");
btn.addActionListener(this);
b2=new JLabel("0");
b3=new JLabel("");
btns=new JButton[row*col];
p=new JPanel();
p.setLayout(new BorderLayout());
contentPane.add(p);
p.add(p3,BorderLayout.NORTH);
//combo=new JComboBox(new Object[]{"初级","中级","高级"} );
//加监听
/*combo.addItemListener(new ItemListener(){
}});*/
p1=new JPanel();
//在那个位置
//(( FlowLayout)p1.getLayout()).setAlignment( FlowLayout.RIGHT);
p1.add(b1);
p1.add(btn);
p1.add(b2);
p1.add(b3);
p.add(p3,BorderLayout.NORTH);
p.add(p1,BorderLayout.CENTER);
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;i<row*col;i++){
btns[i]=new JButton("");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p,BorderLayout.NORTH);
contentPane.add(p2,BorderLayout.CENTER);
}
public void go(){
setVisible(true);
}
public static void main(String[] args){
new ScanLei1("扫雷").go();
}
public void out(int[][] a,JButton[] btns,ActionEvent e,int i,int x,int y){
int p=1;
if(a[x][y]==0){
a[x][y]=10;
btns[i].setEnabled(false); //33
for(int l=y-1;l<=y+1;l++){
int m=x-1-1;
int n=l-1;
p=1;
System.out.println(a[1][2]);
if(n>-1&&n<col&&m>-1&&m<row)
{
for(int q=0;q<row&&p==1;q++){//col-->row;
if(((n+col*q)>=(m*col))&&((n+col*q)<(m+1)*col)){
if(a[x-1][l]!=0&&a[x-1][l]!=10){
btns[n+col*q].setText(a[x-1][l]+"");
a[x-1][l]=10;
btns[n+col*q].setEnabled(false);
}
else if(a[x-1][l]==0){
//a[x-1][l]=10;
btns[n+col*q].setEnabled(false);
out(a,btns,e,n+col*q,x-1,l); ////55////
a[x-1][l]=10;
btns[n+col*q].setEnabled(false);
}
p=0;

}
}
}
p=1;
m=x;
if(n>-1&&n<col&&m>-1&&m<col)
{
for(int q=0;q<row&&p==1;q++){
if(((n+col*q)>=(m*col))&&((n+col*q)<(m+1)*col)){
if(a[x+1][l]!=0&&a[x+1][l]!=10){
btns[n+col*q].setText(a[x+1][l]+"");
a[x+1][l]=10;
btns[n+col*q].setEnabled(false);
}
else if(a[x+1][l]==0){

out(a,btns,e,n+col*q,x+1,l);///55////
a[x+1][l]=10;
btns[n+col*q].setEnabled(false);
}
p=0;
}
}

}
}
int m=x-1;
int n=y-1-1;
p=1;
if(n>-1&&n<col&&m>-1&&m<col)
{
for(int q=0;q<row&&p==1;q++){
if(((n+col*q)>=(m*col))&&((n+col*q)<(m+1)*col)){
if(a[x][y-1]!=0&&a[x][y-1]!=10){
btns[n+col*q].setText(a[x][y-1]+"");
a[x][y-1]=10;
btns[n+col*q].setEnabled(false);
}
else if(a[x][y-1]==0){

out(a,btns,e,n+col*q,x,y-1);

a[x][y-1]=10;
btns[n+col*q].setEnabled(false);
}
p=0;
}
}
}
p=1;
m=x-1;
n=y+1-1;
if(n>-1&&n<col&&m>-1&&m<col)
{
for(int q=0;q<row&&p==1;q++){
if(((n+col*q)>=(m*col))&&((n+col*q)<(m+1)*col)){
if(a[x][y+1]!=0&&a[x][y+1]!=10){
btns[n+col*q].setText(a[x][y+1]+"");
a[x][y+1]=10;
btns[n+col*q].setEnabled(false);
}
else if(a[x][y+1]==0){
out(a,btns,e,n+col*q,x,y+1);
a[x][y+1]=10;
btns[n+col*q].setEnabled(false);
}
p=0;
}
}
}
}
}
public void actionPerformed(ActionEvent e) {

if(e.getActionCommand()=="初级"){
row=9;
col=9;
bon=10;
a1=new int[bon];
b=bon;
//setSize(297,377);
a = new int[row+2][col+2];
this.remove(p2);
timer.stop();
b1.setText("10");
b2.setText("0");
b3.setText("");
btns=new JButton[row*col];
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;i<row*col;i++){
btns[i]=new JButton(" ");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p2,BorderLayout.CENTER);
//setSize(297,377);
this.pack();
for(int i=0;i<row*col;i++){
btns[i].setText(" ");
btns[i].setEnabled(true);
}
for(int i=0;i<row+2;i++){
for(int j=0;j<col+2;j++){
a[i][j]=0;
}
}

}else if(e.getActionCommand()=="中级"){
row=16;
col=16;
bon=40;
//setSize(33*col,33*row+80);
a1=new int[bon];
a = new int[row+2][col+2];
b=bon;
this.remove(p2);
timer.stop();
b1.setText("40");
b2.setText("0");
b3.setText("");
btns=new JButton[row*col];

p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;i<row*col;i++){
btns[i]=new JButton(" ");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p2,BorderLayout.CENTER);
this.pack();
//setSize(33*col,33*row+80);
for(int i=0;i<row*col;i++){
btns[i].setText("");
btns[i].setEnabled(true);
}
for(int i=0;i<row+2;i++){
for(int j=0;j<col+2;j++){
a[i][j]=0;
}
}
}else if(e.getActionCommand()=="高级"){
row=16;
col=32;
bon=99;
setSize(33*col,33*row+80);
a1=new int[bon];
a = new int[row+2][col+2];
b=bon;
this.remove(p2);
timer.stop();
b1.setText("99");
b2.setText("0");
b3.setText("");
btns=new JButton[row*col];
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;i<row*col;i++){
btns[i]=new JButton(" ");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p2,BorderLayout.CENTER);
//setSize(33*col,33*row+80);
this.pack();
for(int i=0;i<row*col;i++){
btns[i].setText("");
btns[i].setEnabled(true);
}
for(int i=0;i<row+2;i++){
for(int j=0;j<col+2;j++){
a[i][j]=0;
}
}

}
if(e.getSource()==btn){
timer.start();
b=bon;
b3.setText("");
//System.out.println(bon);
//清空
for(int i=0;i<row*col;i++){
btns[i].setText("");
btns[i].setEnabled(true);
}
for(int i=0;i<row+2;i++){
for(int j=0;j<col+2;j++){
a[i][j]=0;
}
}
//产生随机数
for(int i=0;i<bon;i++)
{ int p=1;
int m=(int)(Math.random()*row*col);
while(p==1){
int l=1;
int j;
for( j=0;j<i&&l==1;j++){

if(a1[j]==m){
m=(int)(Math.random()*row*col);
l=0;
}
}
if(j==i){
a1[i]=m;
p=0;
}
}
}
b1.setText(bon+"");
b2.setText("0");
//布雷
for(int i=0;i<bon;i++){
int x=(a1[i]/col+1);
int y=(a1[i]%col+1);
a[x][y]=100;
}
for(int i=0;i<row+2;i++){
for(int j=0;j<col+2;j++){
if(i==0||j==0||i==row+1||j==col+1){
a[i][j]=0;
}
}
}
for(int i=1;i<=row;i++){
for(int j=1;j<=col;j++){
if(a[i][j]!=100){
for(int l=j-1;l<=j+1;l++){
if(a[i-1][l]==100){
a[i][j]++;
}
if(a[i+1][l]==100){
a[i][j]++;
}
}
if(a[i][j-1]==100){
a[i][j]++;
}
if(a[i][j+1]==100){
a[i][j]++;
}
}
}
}
}
if(e.getSource()==timer)
{
String time=b2.getText().trim();

int t=Integer.parseInt(time);
//System.out.println(t);
if(t>=600){
timer.stop();
}else{

t++;
b2.setText(t+"");
}
}
for(int i=0;i<col*row;i++){
if(btns[i].getText()!="★")
{
int x=i/col+1;
int y=i%col+1;
if(e.getSource()==btns[i]&&a[x][y]==100){
btns[i].setText("★");
btns[i].setEnabled(false);
a[x][y]=10;
for(int k=0;k<col*row;k++){
int m1=k/col+1;
int n1=k%col+1;
if(a[m1][n1]!=10&&btns[k].getText()=="★"){
btns[k].setText("*o*");
}
}
for(int j=0;j<col*row;j++){
int m=j/col+1;
int n=j%col+1;
if(a[m][n]==100){
btns[j].setText("★");
btns[j].setEnabled(false);
b3.setText("你输了 !!");
}
btns[j].setEnabled(false);
a[m][n]=10;
}
timer.stop();
}
else if(e.getSource()==btns[i]){
if(a[x][y]==0){
out(a,btns,e,i,x,y);
a[x][y]=10;
btns[i].setEnabled(false);
}
if(a[x][y]!=0&&a[x][y]!=10){
btns[i].setText(a[x][y]+"");
btns[i].setEnabled(false);
a[x][y]=10;
}
}
}else if(btns[i].getText()=="★"){
}
}
}
class NormoreMouseEvent extends MouseAdapter{
public void mouseClicked(MouseEvent e) {
System.out.println(b);
for(int i=0;i<col*row;i++){
int x1=i/col+1;
int y1=i%col+1;
if(e.getSource()==btns[i]&&btns[i].getText()!="★"&&a[x1][y1]!=10)
{
if(e.getButton()==MouseEvent.BUTTON3){
btns[i].setText("★");
b--;
if(b==0){
int flag=0;
for(int j=0;j<col*row;j++){
int x=j/col+1;
int y=j%col+1;
if(a[x][y]==100&&btns[j].getText()=="★"){
flag++;
}
}
if(flag==bon){
timer.stop();
b3.setText("你赢了!");
}
}
b1.setText(b+"");
}
}else if(e.getSource()==btns[i]&&btns[i].getText()=="★"&&a[x1][y1]!=-1){
if(e.getButton()==MouseEvent.BUTTON3){
btns[i].setText("");
b++;
if(b>bon){
b1.setText(bon+"");
}
else{
b1.setText(b+"");
}
btns[i].setEnabled(true);
}
}
}
}
}
}

⑹ 高分求一个运行在Eclipse环境下的java 扫雷游戏的初级代码 越小越好 越短越好 运行就好,就是初级就好了,

import java.awt.Button;
import java.util.Set;
// 每一个小方块类
public class Diamond extends Button {
private Diamond[] diamonds;

// 该小方块周围的八个方向上的小方块
private Diamond east;
private Diamond north;
private Diamond northEast;
private Diamond northWest;
private Diamond south;
private Diamond southEast;
private Diamond southWest;
private Diamond west;

private boolean isBomb;// 是否是雷
private boolean isChange;// 又没有被翻过
private int no;// 产生的方块的编号

// 持有所有小方块的引用,方便进行操作
public Diamond(Diamond[] diamonds) {
this.diamonds = diamonds;
}

// 按键时方块发生改变
public boolean change() {
this.isChange = true;// 说明已经翻过了
if(isBomb) {// 触雷
//this.setBackground(Color.red);
return true;
} else {// 不是雷,就显示周围雷的数目
//this.setLabel(this.getNearBombNo() + "");
this.setLabel(this.getNearBombNo() + "");
//if(this.getNearBombNo() == 0) {
// this.moveon();
//}
return false;
}
}

// 获得该小方块周围雷的数量
public int getNearBombNo() {
int no = 0;
if(this.northWest != null && this.northWest.isBomb) no++;
if(this.north != null && this.north.isBomb) no++;
if(this.northEast != null && this.northEast.isBomb) no++;
if(this.east != null && this.east.isBomb) no++;
if(this.southEast != null && this.southEast.isBomb) no++;
if(this.south != null && this.south.isBomb) no++;
if(this.southWest != null && this.southWest.isBomb) no++;
if(this.west != null && this.west.isBomb) no++;

return no;
}

// 获得该小方块周围的小方块
public Diamond getNearDimaond(int i) {
int index = -1;
switch (i) {
case 1:// 1表示西北,2,表示北,以此类推
index = no - 10;
if(index < 1 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
case 2:
index = no - 9;
if(index < 1) {
return null;
} else {
return diamonds[index];
}
case 3:
index = no - 8;
if(index < 1 || no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72) {
return null;
} else {
return diamonds[index];
}
case 4:
index = no + 1;
if(no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {
return null;
} else {
return diamonds[index];
}
case 5:
index = no + 10;
if(index >= 81 ||no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {
return null;
} else {
return diamonds[index];
}
case 6:
index = no + 9;
if(index > 81) {
return null;
} else {
return diamonds[index];
}
case 7:
index = no + 8;
if(index >= 81 || no==1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
case 8:
index = no - 1;
if(no==1 || no==10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
}
return null;
}

// 递归,set是用来装已经翻过的小方块的,不然会死循环,为什么用set,因为set是不重复的
public void moveon(Set<Diamond> set) {

set.add(this);// 先把自己加上
if(this.getNorthWest() != null && this.getNorthWest().isBomb == false) {
this.getNorthWest().change();

if(this.getNorthWest().getNearBombNo() == 0) {
if(set.contains(this.getNorthWest()) == false)
this.getNorthWest().moveon(set);
}

set.add(this.getNorthWest());
}

if(this.getNorth() != null && this.getNorth().isBomb == false) {
this.getNorth().change();
if(this.getNorth().getNearBombNo() == 0) {
if(set.contains(this.getNorth()) == false)
this.getNorth().moveon(set);
}

set.add(this.getNorth());
}

if(this.getNorthEast() != null && this.getNorthEast().isBomb == false) {
this.getNorthEast().change();
if(this.getNorthEast().getNearBombNo() == 0) {
if(set.contains(this.getNorthEast()) == false)
this.getNorthEast().moveon(set);
}

set.add(this.getNorthEast());
}

if(this.getEast() != null && this.getEast().isBomb == false) {
this.getEast().change();
if(this.getEast().getNearBombNo() == 0) {
if(set.contains(this.getEast()) == false)
this.getEast().moveon(set);
}

set.add(this.getEast());
}

if(this.getSouthEast() != null && this.getSouthEast().isBomb == false) {
this.getSouthEast().change();
if(this.getSouthEast().getNearBombNo() == 0) {
if(set.contains(this.getSouthEast()) == false)
this.getSouthEast().moveon(set);
}

set.add(this.getSouthEast());
}

if(this.getSouth() != null && this.getSouth().isBomb == false) {
this.getSouth().change();
if(this.getSouth().getNearBombNo() == 0) {
if(set.contains(this.getSouth()) == false)
this.getSouth().moveon(set);
}

set.add(this.getSouth());
}

if(this.getSouthWest() != null && this.getSouthWest().isBomb == false) {
this.getSouthWest().change();
if(this.getSouthWest().getNearBombNo() == 0) {
if(set.contains(this.getSouthWest()) == false)
this.getSouthWest().moveon(set);
}

set.add(this.getSouthWest());
}

if(this.getWest() != null && this.getWest().isBomb == false) {
this.getWest().change();
if(this.getWest().getNearBombNo() == 0) {
if(set.contains(this.getWest()) == false)
this.getWest().moveon(set);
}

set.add(this.getWest());
}
}

/*public Diamond[] getDiamonds() {
return diamonds;
}*/

public Diamond getEast() {
return east;
}

public int getNo() {
return no;
}

public Diamond getNorth() {
return north;
}

public Diamond getNorthEast() {
return northEast;
}

public Diamond getNorthWest() {
return northWest;
}

public Diamond getSouth() {
return south;
}

public Diamond getSouthEast() {
return southEast;
}

public Diamond getSouthWest() {
return southWest;
}

public Diamond getWest() {
return west;
}

public boolean isBomb() {
return isBomb;
}

public boolean isChange() {
return isChange;
}

public void setBomb(boolean isBomb) {
this.isBomb = isBomb;
}

public void setChange(boolean isChange) {
this.isChange = isChange;
}

public void setDiamonds(Diamond[] diamonds) {
this.diamonds = diamonds;
}

public void setEast(Diamond east) {
this.east = east;
}

public void setNo(int no) {
this.no = no;
}

public void setNorth(Diamond north) {
this.north = north;
}

public void setNorthEast(Diamond northEast) {
this.northEast = northEast;
}

public void setNorthWest(Diamond northWest) {
this.northWest = northWest;
}

public void setSouth(Diamond south) {
this.south = south;
}

public void setSouthEast(Diamond southEast) {
this.southEast = southEast;
}

public void setSouthWest(Diamond southWest) {
this.southWest = southWest;
}

public void setWest(Diamond west) {
this.west = west;
}

}

⑺ 怎样用JAVA实现扫雷游戏

要详细代码?还是只要启动?
java编写实现,代码如下:import Java.awt.*;
import java.awt.event.*;
import javax.Swing.*;

/*按扭类*/

class Bomb extends JButton
{

public int num_x,num_y; //第几号方块
public int BombRoundCount; //周围雷数
public boolean isBomb; //是否为雷
public boolean isClicked; //是否被点击
public int BombFlag; //探雷标记
public boolean isRight; //是否点击右键

public Bomb(int x,int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
BombRoundCount = 0;
isBomb = false;
isClicked = false;
isRight = false;
}
}
/*窗口及算法实现类*/

class MainBomb extends JFrame implements ActionListener,MouseListener
{

public JTextField text;
public Label nowBomb,setBomb;
public int BlockNum,BombNum; //当前方块数当前雷数
public Icon icon_bomb = new ImageIcon("Bomb.gif"); //踩雷
public Icon icon_bomb_big = new ImageIcon("bomb_big.gif"); //踩雷标记
public Icon icon_flag = new ImageIcon("flag.gif"); //雷标记
public Icon icon_question = new ImageIcon("question.gif"); //疑惑是否有雷
public JButton start = new JButton(" 开始 ");
public Panel MenuPamel = new Panel();
public Panel mainPanel = new Panel();
public Bomb[][] bombButton;

/*界面设计*/

public MainBomb()
{
super("扫雷 Aaron2004制作 2004.8 ");
BlockNum = 64;
BombNum = 10;
Container c=getContentPane();
c.setBackground(Color.gray);
c.setLayout(new BorderLayout());
text=new JTextField("10 ",3);
nowBomb = new Label("当前雷数"+" "+BombNum+"");
setBomb= new Label("设置地雷数");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
BombNum = Integer.parseInt(text.getText().trim());
if(BombNum >= 10 && BombNum < 50 )
replay();
else
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(null,"您设置的地雷数太多了,请重设!","错误",2);
}

}
} );
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel,"North");

mainPanel.setLayout(new GridLayout( (int)Math.sqrt(BlockNum) , (int)Math.sqrt(BlockNum)) );
bombButton=new Bomb[ (int)Math.sqrt(BlockNum) ][];
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++)
{
bombButton[ i ]=new Bomb[ (int)Math.sqrt(BlockNum) ];
}
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++ )
for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++ )
{
bombButton[ i ][ j ]=new Bomb(i,j);
bombButton[ i ][ j ].setForeground( Color.gray);
bombButton[ i ][ j ].addActionListener(this);
bombButton[ i ][ j ].addMouseListener(this);
}
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++ )
for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++ )
mainPanel.add(bombButton[ i ][ j ]);
c.add(mainPanel,"Center");
startBomb();
setSize(400,400);
setLocation(350,200);
setResizable(false);
}

/*布雷*/

public void startBomb()
{

for(int i=0;i<BombNum;i++)
{
int x =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));
int y =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));

if(bombButton[ x ][ y ].isBomb==true)
i--;
else
bombButton[ x ][ y ].isBomb=true ;
}
}

/*重新开始*/

public void replay()
{
nowBomb.setText("当前雷数"+" "+BombNum+"");
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++)
for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++)
{
bombButton[ i ][ j ].isBomb=false;
bombButton[ i ][ j ].isClicked=false;
bombButton[ i ][ j ].setEnabled(true);
bombButton[ i ][ j ].setText("");
bombButton[ i ][ j ].setIcon(null);
}
startBomb();
}

/*是否挖完了所有的雷*/

public void isWin()
{
int findBomb=0; //找到的地雷数

for(int i = 0;i < (int)Math.sqrt(BlockNum) ; i++)
for(int j = 0;j < (int)Math.sqrt(BlockNum ); j++)
{
if(bombButton[ i ][ j ].isBomb == true && bombButton[ i ][ j ].isRight == true)
findBomb++;
}
if( findBomb == Integer.parseInt(text.getText().trim()) )
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"您挖完了所有的雷,您胜利了!","您胜利了",2);
}
}

/*计算方块周围雷数 */

public void CountRoundBomb()
{
for (int i = 0; i < (int)Math.sqrt(BlockNum); i++) {
for (int j = 0; j < (int)Math.sqrt(BlockNum); j++) {
int count = 0;
//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombButton[ i ][ j ].isBomb != true) {
if ( (i - 1 >= 0) && (j - 1 >= 0)) {
if (bombButton[i - 1][j - 1].isBomb == true) {
count += 1; //检测左上方空格是否是地雷
}
}
if ( (i - 1 >= 0)) {
if (bombButton[i - 1][ j ].isBomb == true) {
count += 1; //检测上方空格是否为地雷
}
}
if ( (i - 1 >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i - 1][j + 1] .isBomb == true) {
count += 1; //检测右上方是否为地雷
}
}
if ( (j - 1 >= 0)) {
if (bombButton[ i ][j - 1] .isBomb == true) {
count += 1; //检测左边是否为地雷
}
}
if ( (i >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[ i ][j + 1].isBomb == true) {
count += 1; //右边
}
}
if ( (j - 1 >= 0) && (i + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][j - 1].isBomb == true) {
count += 1; //左下
}
}
if ( (i + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][ j ].isBomb == true) {
count += 1; //下
}
}
if ( (j + 1 <= (int)Math.sqrt(BlockNum)-1) && (i + 1 <= Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][j + 1].isBomb == true) {
count += 1; //右下
}
}
bombButton[ i ][ j ].BombRoundCount = count;
}
}
}
}

/**当选中的位置为空,则翻开周围的地图**/

public void isNull(Bomb[][] bombButton,Bomb ClickecButton)
{
int i,j;
i=ClickecButton.num_x;
j=ClickecButton.num_y;

if (ClickecButton.isBomb==true) {

}
else {

if ( (i - 1 >= 0) && (j - 1 >= 0)) { //检测左上方空格是否是空
if (bombButton[i - 1][j - 1].isBomb == false && bombButton[i - 1][j - 1].isClicked == false && bombButton[i - 1][j - 1].isRight == false) {
bombButton[i - 1][j - 1].setText((bombButton[i - 1][j - 1].BombRoundCount)+"");
bombButton[i - 1][j - 1].setEnabled(false);
bombButton[i - 1][j - 1].isClicked=true;
}
}

if ( (i - 1 >= 0)) { //检测上方空格是否为空
if (bombButton[i - 1][ j ] .isBomb == false && bombButton[i - 1][ j ].isClicked == false && bombButton[i - 1][ j ].isRight == false) {
bombButton[i - 1][ j ].setText((bombButton[i - 1][ j ].BombRoundCount)+"");
bombButton[i - 1][ j ].setEnabled(false);
bombButton[i - 1][ j ].isClicked=true;
}
}
if ( (i - 1 >= 0) && (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测右上方是否为空
if (bombButton[i - 1][j + 1] .isBomb == false && bombButton[i - 1][j + 1].isClicked == false && bombButton[i - 1][j + 1].isRight == false) {
bombButton[i - 1][j + 1].setText((bombButton[i - 1][j + 1].BombRoundCount)+"");
bombButton[i - 1][j + 1].setEnabled(false);
bombButton[i - 1][j + 1].isClicked=true;
}

}
if ( (j - 1 >= 0)) { //检测左边是否为空
if (bombButton[ i ][j - 1].isBomb == false && bombButton[ i ][j - 1].isClicked == false && bombButton[ i ][j - 1].isRight == false) {
bombButton[ i ][j - 1].setText((bombButton[ i ][j - 1].BombRoundCount)+"");
bombButton[ i ][j - 1].setEnabled(false);
bombButton[ i ][j - 1].isClicked=true;
}

}
if ( (i >= 0) && (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测右边空格是否是空
if (bombButton[ i ][j + 1].isBomb == false && bombButton[ i ][j + 1].isClicked == false && bombButton[ i ][j + 1].isRight == false) {
bombButton[ i ][j + 1].setText((bombButton[ i ][j + 1].BombRoundCount)+"");
bombButton[ i ][j + 1].setEnabled(false);
bombButton[ i ][j + 1].isClicked=true;
}
}
if ( (j - 1 >= 0) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测左下空格是否是空
if (bombButton[i + 1][j - 1].isBomb == false && bombButton[i + 1][j - 1].isClicked == false && bombButton[i + 1][j - 1].isRight == false) {
bombButton[i + 1][j - 1].setText((bombButton[i + 1][j - 1].BombRoundCount)+"");
bombButton[i + 1][j - 1].setEnabled(false);
bombButton[i + 1][j - 1].isClicked=true;
}
}
if ( (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测下边空格是否是空
if (bombButton[i + 1][ j ].isBomb == false && bombButton[i + 1][ j ].isClicked == false && bombButton[i + 1][ j ].isRight == false) {
bombButton[i + 1][ j ].setText((bombButton[i + 1][ j ].BombRoundCount)+"");
bombButton[i + 1][ j ].setEnabled(false);
bombButton[i + 1][ j ].isClicked=true;
}
}
if ( (j + 1 <= ((int)Math.sqrt(BlockNum)-1) ) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测右下边空格是否是空
if (bombButton[i + 1][j + 1].isBomb == false && bombButton[i + 1][j + 1].isClicked == false && bombButton[i + 1][j + 1].isRight == false) {
bombButton[i + 1][j + 1].setText((bombButton[i + 1][j + 1].BombRoundCount)+"");
bombButton[i + 1][j + 1].setEnabled(false);
bombButton[i + 1][j + 1].isClicked=true;
}
}
if ( (i - 1 >= 0) && (j - 1 >= 0))//检测左上
isNull(bombButton,bombButton[i - 1][j - 1]);
if ( (i - 1 >= 0))
isNull( bombButton,bombButton[i - 1][ j ]);//检测上方
if ( (i - 1 >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1))
isNull( bombButton,bombButton[i - 1][j + 1]);//检测右上
if ( (j - 1 >= 0))
isNull(bombButton,bombButton[i][j - 1]);//检测左边
if ( (i >= 0) && (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i][j + 1]);//检测右边
if ( (j - 1 >= 0) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i + 1][j - 1]); //检测左下
if ( (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) //检测下
isNull(bombButton,bombButton[i + 1][ j ]);
if ( (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) //检测右下
isNull(bombButton,bombButton[i + 1][j + 1]);

}
}

public void actionPerformed(ActionEvent e)
{

CountRoundBomb();

if(((Bomb)e.getSource()).isBomb==false && ((Bomb)e.getSource()).isClicked == false)
{
((Bomb)e.getSource()).setText(( ((Bomb)e.getSource()).BombRoundCount )+"");
((Bomb)e.getSource()).isClicked=true;
((Bomb)e.getSource()).setIcon(null);
((Bomb)e.getSource()).setEnabled(false);
if((((Bomb)e.getSource()).BombRoundCount) == 0)
isNull(bombButton,(Bomb)e.getSource());
isWin();
}
else if(((Bomb)e.getSource()).isBomb == true)
{

for(int i=0;i<(int)Math.sqrt(BlockNum);i++)
for(int j=0;j<(int)Math.sqrt(BlockNum);j++)
{
if(bombButton[ i ][ j ].isBomb == true)
bombButton[ i ][ j ].setIcon(icon_bomb);
}

((Bomb)e.getSource()).setIcon(icon_bomb_big);

JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"你踩到地雷了,按确定重来","你踩到地雷了",2);
replay();
}
}

public void mouseClicked(MouseEvent e)
{
Bomb bombSource = (Bomb)e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);

if((right == true) && (bombSource.isClicked == false))
{
bombSource.BombFlag = (bombSource.BombFlag + 1)%3;
if(bombSource.BombFlag == 1)
{

if(BombNum > 0 && bombSource.isRight == false ){
bombSource.setIcon(icon_flag);
bombSource.isRight = true;
BombNum--;
}
isWin();
nowBomb.setText("当前雷数"+" "+BombNum+"");
}
else if(bombSource.BombFlag == 2)
{

if( (BombNum !=0 ) ||(BombNum ==0 &&(bombSource.getIcon()==icon_flag)) )
BombNum++;
bombSource.setIcon(icon_question);
nowBomb.setText("当前雷数"+" "+BombNum+"");
}
else if(bombSource.BombFlag == 0)
{
bombSource.setIcon(null);
bombSource.isRight = false;
}
}
}

public void mouseEntered(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
}

/*主类*/

public class Main
{
public static void main(String args[])
{
(new MainBomb()).show();

}
}

⑻ java编写扫雷程序的流程图,哪位大侠帮下,小弟急用~~~

我有源代码:绝对可以通过,不过比较简单而已,对学生而言应该可以了吧,这是以前写的:
一下两个文件放在一个包里就行了
/*
This class defines a class that contains some useful
attributions and some methods to set or get these attributions
*/
import javax.swing.JButton;
public class ExButton extends JButton
{
//if the button is a mine,the isMine will be true
private boolean isMine;
//to check if a button has been visited is useful
//when using the recursion in the Game class
private boolean isVisited;
//the row number of the button
int btnRowNumber;
//the column number of the button
int btnColumnNumber;
//the mines around a button
int minesAround=0;
public void setIndex(int btnRowNumber,int btnColumnNumber)
{
this.btnRowNumber=btnRowNumber;
this.btnColumnNumber=btnColumnNumber;

}
public int getRowNumber()
{
return this.btnRowNumber;
}
public int getColumnNumber()
{
return this.btnColumnNumber;
}
public void setVisited(boolean isVisited)
{
this.isVisited=isVisited;
}
public boolean getVisited()
{
return this.isVisited;
}
public void setMine(boolean isMine)
{
this.isMine=isMine;
}
public boolean getMine()
{
return this.isMine;
}
//the attribute of minesAround add one each
//time a mine is put down around the button

public void addMinesAround()
{
this.minesAround++;
}
public int getMinesAround()
{
return this.minesAround;
}
}
-------------------------------------------------
/*
File Name: Game.java
Author: Tian Wei Student Number: Email: [email protected]
Assignment number: #4
Description: In this program ,a frame will be created which contains
ten "mines".When you click a button ,it will present the
number of mines around or a message of losing the game
(if the button is a mine).You can make a right click to
sign a dengerous button as well.When all the mines have
been signed ,a message box of winning the game will jump
to the screen.And the the message of the time you used in
the game.More over,you can click the button on the bottom
to restart the game.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import java.util.Timer;
public class Game extends JFrame{
//define some menber variables
private long minute=0,second=0;//take down time used int the game
private ExButton[][] btn;//two-dimension array present the buttons
private JLabel label;
private JButton restart;//restart button
private int minesRemained;//remained mines that you have not signed
private boolean thisTry=true;
private JLabel timeUsed=new JLabel ();
private Random rand=new Random();
private final int ROWS,COLUMNS;
private final int MINES;
// the constuctor
public Game(int rows,int columns,int mines)
{
super("Find mines");
this.ROWS=rows;
this.COLUMNS=columns;
this.MINES=mines;
minesRemained=MINES;
Timer timer=new Timer();//Timer's object to timer the game
timer.schele(new MyTimer(), 0, 1000);//do the function every second
Container container=getContentPane();
container.setLayout(new BorderLayout());
//Jpanel in the Container
JPanel jpanel=new JPanel();
jpanel.setLayout(new GridLayout(ROWS,COLUMNS));
restart=new JButton("click me to restart the game");
JPanel jpanel2=new JPanel();
//Another JPanel in the Container
jpanel2.setLayout(new FlowLayout());
jpanel2.add(timeUsed);
jpanel2.add(restart);
ButtonListener restartHandler=new ButtonListener();
restart.addActionListener(restartHandler);
container.add(jpanel2,BorderLayout.SOUTH);
btn=new ExButton[ROWS+2][COLUMNS+2];
//initialize the buttons
for(int i=0;i<=ROWS+1;i++)
{
for(int j=0;j<=COLUMNS+1;j++)
{
btn[i][j]=new ExButton();
btn[i][j].addMouseListener(new MouseClickHandler());
btn[i][j].setIndex(i,j);
btn[i][j].setVisited(false);
}

}
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
jpanel.add(btn[i][j]);

container.add(jpanel,BorderLayout.CENTER);
JPanel jpanel3=new JPanel ();
label=new JLabel();
label.setText("Mines remaining "+MINES);
jpanel3.add(label);

container.add(jpanel3,BorderLayout.NORTH );
this.addMines();
this.addMinesAround();
}
//randomly put ten mines
private void addMines()
{
for(int i=1;i<=MINES;i++)
{
int raInt1=rand.nextInt(ROWS);
int raInt2=rand.nextInt(COLUMNS);
if((raInt1==0)||(raInt2==0)||btn[raInt1][raInt2].getMine())
i--;
else
btn[raInt1][raInt2].setMine(true);
}
}
//take down the mines around a button
private void addMinesAround()
{
for(int i=1;i<=ROWS;i++)
{
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine())
{
btn[i][j-1].addMinesAround();
btn[i][j+1].addMinesAround();
btn[i-1][j-1].addMinesAround();
btn[i-1][j].addMinesAround();
btn[i-1][j+1].addMinesAround();
btn[i+1][j-1].addMinesAround();
btn[i+1][j].addMinesAround();
btn[i+1][j+1].addMinesAround();

}
}
}
}
//if a button clicked is a empty one,then use a recursion
//to find all the empty buttons around
private void checkEmpty(ExButton button)
{
button.setVisited(true);
int x=button.getRowNumber();
int y=button.getColumnNumber();
button.setBackground(Color.white);
if((button.getMinesAround()==0)&&(x>=1)&&(x<=ROWS)
&&(y>=1)&&(y<=COLUMNS))
{
if(!btn[x][y-1].getVisited())
checkEmpty(btn[x][y-1]);
if(!btn[x][y+1].getVisited())
checkEmpty(btn[x][y+1]);
if(!btn[x-1][y].getVisited())
checkEmpty(btn[x-1][y]);
if(!btn[x+1][y].getVisited())
checkEmpty(btn[x+1][y]);
if(!btn[x-1][y-1].getVisited())
checkEmpty(btn[x-1][y-1]);
if(!btn[x-1][y+1].getVisited())
checkEmpty(btn[x-1][y+1]);
if(!btn[x+1][y-1].getVisited())
checkEmpty(btn[x+1][y-1]);
if(!btn[x+1][y+1].getVisited())
checkEmpty(btn[x+1][y+1]);
}
else if(button.getMinesAround()>0)
button.setText(""+button.getMinesAround());

}
//the main function
public static void main(String args[])
{
String rows,columns,mines;
int rowNumber,columnNumber,mineNumber;
rows=JOptionPane.showInputDialog("Enter the rows of the game");
columns=JOptionPane.showInputDialog("Enter the columns of the game");
mines=JOptionPane.showInputDialog("Enter the mines of the game");
rowNumber=Integer.parseInt(rows);
columnNumber=Integer.parseInt(columns);
mineNumber=Integer.parseInt(mines);
Game frame=new Game(rowNumber,columnNumber,mineNumber);
frame.setTitle("Find Mines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(220, 80);
frame.setSize(600, 600 );
frame.setVisible(true);
}
//there are three inner class below

//The first inner class is used to do the mouse listener's
//function.When you click a button ,it will present the
//number of mines around or a message of losing the game
//(if the button is a mine).You can make a right click to
//sign a dengerous button as well.When ten mines have been
//signed,it will check whether all the signed ones are mines
private class MouseClickHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
//get the button that been clicked
ExButton eventButton=new ExButton();
eventButton=(ExButton)event.getSource();
eventButton.setVisited(true);
//when it is a right click
if(event.isMetaDown())
{
if(eventButton.getText()=="#")
{
minesRemained++;
eventButton.setText("");

}
else
{
if((eventButton.getBackground()==Color.white)||
(eventButton.getText()!=""))
{
//do nothing
}

else
{
minesRemained--;
eventButton.setText("#");

}
}
label.setText("Mines remaining "+minesRemained);
//check if all the signed buttons are mines
if(minesRemained==0)
{
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine()&&btn[i][j].getText()!="#")
thisTry=false;
if(!btn[i][j].getMine()&&btn[i][j].getText()=="#")
thisTry=false;

}
if(thisTry)
{
//win the game
JOptionPane.showMessageDialog(null, "You succeed" +
" in this experience!");
JOptionPane.showMessageDialog(null, "Time used:"+
timeUsed.getText());
}

else//you have wrongly signed one or more mines
JOptionPane.showMessageDialog(null, "You have wrongly " +
"signed one or more mines,please check it and go on!");

}
}
else if(event.isAltDown())
{
//do nothing
}
else
{//normally click(left click)
if(eventButton.getText()=="#")
{
//do nothing
}
else if(eventButton.getMine())
{
//lose the game
JOptionPane.showMessageDialog(null, "What a pity!" +
"You failed!" );
//show all the mines to the loser
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine())
btn[i][j].setBackground(Color.BLACK);
}
JOptionPane.showMessageDialog(null, "Time used: 0"+
minute+":"+second);

}
else
{
if(eventButton.getMinesAround()==0)
{
//call the function to find all the empty buttons around
checkEmpty(eventButton);
}
else
eventButton.setText(""+eventButton.getMinesAround());

}
}

}

}
//The second class is to listen to the button which used to
//restart the game.In this class,it will dispose the old frame
//and create a new one(Of course,the mines's position have
//been changed).
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//what to dispose is the object of Game class
Game.this.dispose();
//the same code as in the main function
Game frame=new Game(ROWS,COLUMNS,MINES);
frame.setTitle("Find Mines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600 );
//make sure the frame is at the center of the screen
frame.setLocation(220, 80);
frame.setVisible(true);
}
}
//The last class is the class that will be used in the
//Timer's object timer.It should inherit the class TimerTask
//It is the task that the Timer will do every second
private class MyTimer extends TimerTask
{
public void run()
{
second+=1;
minute+=second/60;
second=second%60;
//change the text of the time used in the game
timeUsed.setText("Time used 0"+minute+":"+second);
}

}
}//end of the class Game

⑼ 求高手帮忙一个“扫雷”游戏的JAVA的程序代码

import javax.swing.ImageIcon; //程序入口
public class Block {
String name; //名字,比如"雷"或数字
int aroundMineNumber; //周围雷的数目
ImageIcon mineIcon; //雷的图标
boolean isMine=false; //是否是雷
boolean isMark=false; //是否被标记
boolean isOpen=false; //是否被挖开
public void setName(String name) {
this.name=name;
}
//设置周围的雷数
public void setAroundMineNumber(int n) {
aroundMineNumber=n;
}
//获得周围的雷数
public int getAroundMineNumber() {
return aroundMineNumber;
}

public String getName() {
return name;
}
//判断是否是雷
public boolean isMine() {
return isMine;
}
//设置是否为雷
public void setIsMine(boolean b) {
isMine=b;
}
//设置雷的图标
public void setMineIcon(ImageIcon icon){
mineIcon=icon;
}
//获得雷的图标
public ImageIcon getMineicon(){
return mineIcon;
}
//确定雷是否被挖开
public boolean getIsOpen() {
return isOpen;
}
//设置为已经被挖开
public void setIsOpen(boolean p) {
isOpen=p;
}
//返回此处是否已经被标记
public boolean getIsMark() {
return isMark;
}
//设置此处是否已经被标记
public void setIsMark(boolean m) {
isMark=m;
}
import javax.swing.*;
import java.awt.*;
public class BlockView extends JPanel{
JLabel blockNameOrIcon; //用来显示Block对象的name、number和mineIcon属性
JButton blockCover; //用来遮挡blockNameOrIcon.
CardLayout card; //卡片式布局
BlockView(){
card=new CardLayout();
setLayout(card);
blockNameOrIcon=new JLabel("",JLabel.CENTER);
blockNameOrIcon.setHorizontalTextPosition(AbstractButton.CENTER);
blockNameOrIcon.setVerticalTextPosition(AbstractButton.CENTER);
blockCover=new JButton();
add("cover",blockCover);
add("view",blockNameOrIcon);
}
//给出视觉效果变化
public void giveView(Block block){
// 如果是雷,将对应的图标和文字更改
if(block.isMine){
blockNameOrIcon.setText(block.getName());
blockNameOrIcon.setIcon(block.getMineicon());
}
else {
int n=block.getAroundMineNumber();
if(n>=1)
blockNameOrIcon.setText(""+n);
else
blockNameOrIcon.setText(" ");
}
}
public void seeBlockNameOrIcon(){
card.show(this,"view");
validate();
}

public void seeBlockCover(){
card.show(this,"cover");
validate();
}
public JButton getBlockCover(){
return blockCover;
}
}

阅读全文

与扫雷程序java相关的资料

热点内容
服务器换主机需要什么系统 浏览:749
linux监控jvm内存 浏览:79
空调压缩机自制工具 浏览:518
命令行控制路由器 浏览:957
逆拓扑排序算法描述 浏览:588
如何远程链接到linux服务器地址 浏览:630
抹茶app支付方式怎么选 浏览:556
猎人宝宝攻击命令 浏览:159
操作系统是编译原理吗 浏览:646
云服务器迁移后 浏览:260
excel格式转换pdf 浏览:987
登录器一般存在哪个文件夹 浏览:535
中兴光猫机器码算法 浏览:330
android响应时间测试 浏览:940
java编程思想第四版答案 浏览:888
如何对nbt编程 浏览:885
mscpdf 浏览:948
文件夹d盘突然0字节可用 浏览:272
吃火腿肠的解压场面 浏览:339
卫星锅加密教程 浏览:792