189 8069 5689

java扫雷重新开始代码 java扫雷项目总结

JAVA,编一个游戏小游戏,比如扫雷,这个程序大概的代码是什么,谁能教教我?我比较笨,但是我认学。

import javax.swing.*;

网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于企业网站建设,高端网页制作,对凿毛机等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业营销推广优化,H5建站,响应式网站。

import java.awt.*;

import java.awt.event.*;

public class Main

{

public static void main(String[] argus)

{

Landmine Zhang = new Landmine();

}

}

//

// Landmine类 主界面

class Landmine extends JFrame

{

static Resources resources = new Resources();

Playing listener = new Playing(this); //主要监听者,监听地雷面板的动作

Help helpListener = new Help(this); //辅助监听者,监听“帮助”、“关于”

JPanel landminePanel = new JPanel(); //创建地雷面板

JPanel topPanel = new JPanel(); //创建顶部面板

JPanel lowerPanel = new JPanel(); //创建底部面板

public static MyButton [][] lei; //主区按钮组

public static int numberOfUnflaged ; //剩余的雷数,显示在topPanel上,用于提示用户

public static int numberOfClicked; //已经翻开的格子数,当数字数字到"总格子数—雷数"时,即胜利

public static int usedTime; //已用时间

public static JLabel numberOfUnflagedLabel = new JLabel(); //创建剩雷数标签

public static JLabel timeLabel = new JLabel();//创建时间标签

public static Timer timer; //创建计时

Keylistener keyListener = new Keylistener(this);

public Landmine()

{

super("扫雷__1.2版__小老头"); //标题

setBounds(300,90,800,800); //设置窗口位置和大小

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//最大化、最小化、关闭按钮

BorderLayout ff = new BorderLayout(); //创建布局管理器

setLayout(ff); //关联布局管理器

setResizable(false); //禁止改变窗口大小

/*初始化一些数据*/

numberOfClicked = 0;

numberOfUnflaged = 40;

usedTime = 0;

/*设置顶部面板*/

numberOfUnflagedLabel.setText("剩余雷数:"+numberOfUnflaged);//显示剩余雷数

numberOfUnflagedLabel.setFont(resources.fontOne);//设置剩雷数标签字体

numberOfUnflagedLabel.setIcon(resources.bombIconForLabel);//剩雷数标签图标(地雷形)

topPanel.add(numberOfUnflagedLabel); //剩雷数标签加入topPanel

timeLabel.setText("用时:" + usedTime); //显示剩余时间

timeLabel.setFont(resources.fontOne); //设置时间标签字体

timeLabel.setIcon(resources.clockIcon); //设置时间标签图标

topPanel.add(timeLabel); //时间标签加入topPanel

add(topPanel,BorderLayout.NORTH); //加入主面板上部

timer = new Timer(1000,new TimerListener());//计算器注册监听者

/*设置底部面板*/

JButton aboutJB = new JButton("关于"); //创建“关于”按钮

JButton helpJB = new JButton("求救"); //创建“求救”按钮

helpJB.addActionListener(helpListener); //"求救"按钮加入监听者

aboutJB.addActionListener(helpListener);//"关于"按钮加入监听者

helpJB.addKeyListener(keyListener);

aboutJB.addKeyListener(keyListener); //注册按键监听

lowerPanel.add(aboutJB); //“关于”按钮加入lowerPanel

lowerPanel.add(helpJB); //“帮助”按钮加入lowerPanel

add(lowerPanel,BorderLayout.SOUTH);

/*设置地雷面板*/

GridLayout dd = new GridLayout(16,16);

landminePanel.setLayout(dd); //布局管理

lei = new MyButton[18][18];

for(int i=0; i18; ++i)

{//创建下标0—17的按钮,18*18矩阵

for(int j=0; j18; ++j)

{

lei[i][j] = new MyButton(i,j);

}

}

for(int i=1; i17; ++i)

{//将下标1-16的按钮,加入面板、设置图标、翻开标记为假、加入监听者

for(int j=1; j17; ++j)

{

landminePanel.add(lei[i][j]); //按钮加入地雷面板

lei[i][j].setIcon(resources.smallIcon); //设置按钮图标

lei[i][j].isClicked = false; //翻开标记设置为 假lei[i][j].setIcon(dead);

lei[i][j].addActionListener(listener); //加入监听者

lei[i][j].addMouseListener(listener); //加入鼠标事件监听者

lei[i][j].addKeyListener(keyListener); //按钮注册按键监听,当焦点在按钮上是能监听按键

}

}

add(landminePanel,BorderLayout.CENTER); //landminePanel加入主框架中央

addLandmine(); //布雷

timer.start(); //启动计时器

setVisible(true);//显示之

}

/*布雷*/

public static void addLandmine()

{//随机将40的按钮的是否为雷的标记isBomb设为真

for(int count = 0; count40; /*blank*/)

{

int i = (int)(Math.random()*100 % 16 +1 ) ;

int j = (int)(Math.random()*100 % 16 +1 ) ;

if(lei[i][j].isBomb == false)

{

lei[i][j].isBomb = true;

count++;

}

}

}

class TimerListener implements ActionListener

{//内部类,时间监听

public void actionPerformed(ActionEvent e)

{

usedTime++;

timeLabel.setText("用时:" + usedTime);

}

}

}

//

// Playing类 执行主要游戏操作

class Playing implements ActionListener,MouseListener

{

static Resources resources = new Resources();

public static Landmine gui;

public Playing(Landmine in )

{

gui = in;

}

public void actionPerformed(ActionEvent event)

{

MyButton receive = (MyButton)event.getSource();

if(receive.isBomb)

{//如果翻到了雷。。

for(int i=1; i17; ++i)

{//将所有的雷图标设为 “地雷”

for(int j=1; j17; ++j)

{

if(gui.lei[i][j].isBomb)

gui.lei[i][j].setIcon(resources.bombIcon);

}

}

receive.setIcon(resources.deadIcon);//将踩到的地雷图标设为 “衰”

gui.timer.stop(); //停止计时器

JOptionPane.showMessageDialog(null,"小朋友,你挂了…","失败!",

JOptionPane.INFORMATION_MESSAGE,

resources.deadIcon);//提示失败

int yourChose = JOptionPane.showConfirmDialog(null,"你可能是一不小心点错了,再来一局?" );

if(yourChose == JOptionPane.OK_OPTION)

{//点击“是”时

replay();

}

else

{//点击 “否” 或 “取消” 时退出程序

System.exit(0);

}

}

else if(receive.isClicked ==false)

{//未翻到雷

showBombNumber(receive);

}

}

public static void showBombNumber(MyButton in)

{//翻开点击的按钮

int numberOfLandmine = 0;//记录雷的个数

in.isClicked = true; //翻开标记设为真

/*检测周围8个方块是否为雷*/

if(gui.lei[in.num_x-1][in.num_y-1].isBomb == true) numberOfLandmine++;//左上

if(gui.lei[in.num_x][in.num_y-1].isBomb == true) numberOfLandmine++; //上

if(gui.lei[in.num_x+1][in.num_y-1].isBomb == true) numberOfLandmine++;//右上

if(gui.lei[in.num_x+1][in.num_y].isBomb == true) numberOfLandmine++; //右

if(gui.lei[in.num_x+1][in.num_y+1].isBomb == true) numberOfLandmine++;//右下

if(gui.lei[in.num_x][in.num_y+1].isBomb == true) numberOfLandmine++; //下

if(gui.lei[in.num_x-1][in.num_y+1].isBomb == true) numberOfLandmine++;//左下

if(gui.lei[in.num_x-1][in.num_y].isBomb == true) numberOfLandmine++; //左

in.setIcon(new ImageIcon("images/"+numberOfLandmine+".png"));//根据周围的雷数显示数字图标

gui.numberOfClicked++;//翻开格子数+1

if(gui.numberOfClicked==216)

{//翻开216个格子时游戏成功,用户选择是否再来一局

int yourChoice = JOptionPane.showConfirmDialog(null,"恭喜你成功了!再来一盘吗?");

if(yourChoice == JOptionPane.OK_OPTION)

replay();

else

System.exit(0);

}

if(numberOfLandmine==0)

{//如果周围无雷,则将周围未翻开格子的全部翻开

if(gui.lei[in.num_x-1][in.num_y-1].isClicked == false)

showBombNumber(gui.lei[in.num_x-1][in.num_y-1]);

if(gui.lei[in.num_x][in.num_y-1].isClicked == false)

showBombNumber(gui.lei[in.num_x][in.num_y-1]);

if(gui.lei[in.num_x+1][in.num_y-1].isClicked == false)

showBombNumber(gui.lei[in.num_x+1][in.num_y-1]);

if(gui.lei[in.num_x+1][in.num_y].isClicked == false)

showBombNumber(gui.lei[in.num_x+1][in.num_y]);

if(gui.lei[in.num_x+1][in.num_y+1].isClicked == false)

showBombNumber(gui.lei[in.num_x+1][in.num_y+1]);

if(gui.lei[in.num_x][in.num_y+1].isClicked == false)

showBombNumber(gui.lei[in.num_x][in.num_y+1]);

if(gui.lei[in.num_x-1][in.num_y+1].isClicked == false)

showBombNumber(gui.lei[in.num_x-1][in.num_y+1]);

if(gui.lei[in.num_x-1][in.num_y].isClicked == false)

showBombNumber(gui.lei[in.num_x-1][in.num_y]);

}

}

public static void replay()

{//重新开始

gui.dispose(); //释放框架资源

gui.timer.stop(); //终止计时器

Landmine ff = new Landmine();//重新创建一个主类的实例

//这几条语句实现了重新开始————关闭上一个窗口,重新开启一个

//但是这种方法会造成内存的浪费,一个改进的方法是不关闭当年窗口,而是将当前窗口重新初始化

}

public void mousePressed(MouseEvent e)

{//当鼠标右键点击时自动调用此函数

int mods = e.getModifiers();

MyButton receive = (MyButton)e.getSource();

if((mods InputEvent.BUTTON3_MASK) != 0)

{//鼠标右键

if(receive.isClicked == false)

{

receive.isRight = receive.isRight ? false : true;//改变receive.isRight的值

if(receive.isRight)

{//如果添加标记,则剩余雷数-1,设置标签为“旗帜”

gui.numberOfUnflaged--;

receive.setIcon(resources.flagIcon);

}

else

{//如果清除标记,则剩余雷数+1,设置标签为“未翻开”

gui.numberOfUnflaged++;

receive.setIcon(resources.smallIcon);

}

gui.numberOfUnflagedLabel.setText("剩余雷数:"+gui.numberOfUnflaged);

//更新剩余雷数标签

}

}

}

public void mouseReleased(MouseEvent e){}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

}

//

// Help类,响应“关于”、“求救”

class Help implements ActionListener

{

static Resources resources = new Resources();

public static Landmine gui;

public Help(Landmine in)

{

gui = in ;

}

public void actionPerformed(ActionEvent event)

{

if(event.getActionCommand()=="关于")

JOptionPane.showMessageDialog(null,"扫雷1.2版。。小老头出品");

if(event.getActionCommand()=="求救")

help();

}

public static void help()

{//求救

int stopNumber = (int)(Math.random() * gui.numberOfUnflaged + 1 );

int count = 0;

for(int i=1; i17;++i )

{

for(int j=1; j17; ++j)

{

if( gui.lei[i][j].isBomb !gui.lei[i][j].isClicked !gui.lei[i][j].isRight )

{

count++;

}

if(count == stopNumber)

{

gui.lei[i][j].setIcon(resources.badIcon);

return;

}

}

}

}

}

//

// Keylistener类,响应键盘事件

class Keylistener implements KeyListener

{

static Resources resources = new Resources();

Landmine gui;

public Keylistener(Landmine in)

{

gui = in;

}

public void keyPressed(KeyEvent e)

{//有键按下时自动执行该方法

if(e.getKeyCode() == KeyEvent.VK_UP)

{//按键为 向上 时,将所有未标记的地雷显示出

for(int i=1; i17; ++i)

{

for(int j=1; j17; ++j)

{

if(gui.lei[i][j].isBomb !gui.lei[i][j].isRight)

gui.lei[i][j].setIcon(resources.badIcon);

}

}

}

if(e.getKeyCode() == KeyEvent.VK_DOWN)

{//按键为 向下 时,将所有未标记的地雷恢复为未点击的图标

for(int i=1; i17; ++i)

{

for(int j=1; j17; ++j)

{

if(gui.lei[i][j].isBomb !gui.lei[i][j].isRight)

gui.lei[i][j].setIcon(resources.smallIcon);

}

}

}

}

public void keyReleased(KeyEvent e){}

public void keyTyped(KeyEvent e){}

}

//

// 按钮类 MyBtton

class MyButton extends JButton

{

public int num_x,num_y; //第几号方块

public boolean isBomb; //是否为雷

public boolean isClicked; //是否被点击

public int BombFlag; //探雷标记

public boolean isRight; //是否点击右键

public MyButton(int x, int y)

{

BombFlag = 0;

num_x = x;

num_y = y;

isBomb = false;

isClicked = true;

isRight = false;

}

}

//

// 资源类 其他类中用到的图标,字体等

class Resources

{

public static ImageIcon deadIcon;

public static ImageIcon smallIcon;

public static ImageIcon clockIcon;

public static ImageIcon bombIcon;

public static ImageIcon flagIcon;

public static ImageIcon badIcon;

public static ImageIcon bombIconForLabel;

public static Font fontOne;

public Resources()

{

deadIcon = new ImageIcon("images/dead.gif");

smallIcon = new ImageIcon("images/smallIcon.png");

clockIcon = new ImageIcon("images/clock2.png");

bombIcon = new ImageIcon("images/bomb.png");

flagIcon = new ImageIcon("images/flag_2.png");

badIcon = new ImageIcon("images/bad.gif");

bombIconForLabel = new ImageIcon("images/bombForLabel.gif");

fontOne = new Font("null",Font.BOLD,20);

}

}

怎样用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;iBombNum;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的程序代码

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扫雷重新开始代码 java扫雷项目总结
当前路径:http://cdxtjz.cn/article/dojohss.html

其他资讯