java扫雷代码解析 扫雷java源码-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

java扫雷代码解析 扫雷java源码

求解,这是一个JAVA扫雷游戏,可是代码中没有雷,请问雷的那段代码是什么,高手指教

这个类就是雷的代码,它把雷当成一个对象,在其它地方调用,每次new Block(),就是创建一颗雷的。

在徐州等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、网站建设 网站设计制作按需网站建设,公司网站建设,企业网站建设,品牌网站建设,成都全网营销,外贸网站制作,徐州网站建设费用合理。

下面给你加个测试方法,你慢慢看吧。

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.GridLayout;

import java.awt.Point;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.BorderFactory;

import javax.swing.Icon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.SwingUtilities;

public class Block extends JButton implements MouseListener

{

public static void main(String[] args) {

JFrame jf = new JFrame();

jf.setLayout(new GridLayout(3,3));

Block b1 = new Block();

Block b2 = new Block();

b2.设置是否是雷(true);

Block b3 = new Block();

Block b4 = new Block();

b4.设置是否是雷(true);

Block b5 = new Block();

Block b6 = new Block();

b6.设置是否是雷(true);

Block b7 = new Block();

Block b8 = new Block();

Block b9 = new Block();

b1.setText(""+b1.获取周围的雷数());

b2.setText(""+b2.获取周围的雷数());

b3.setText(""+b3.获取周围的雷数());

b4.setText(""+b4.获取周围的雷数());

b5.setText(""+b5.获取周围的雷数());

b6.setText(""+b6.获取周围的雷数());

b7.setText(""+b7.获取周围的雷数());

b8.setText(""+b8.获取周围的雷数());

b9.setText(""+b9.获取周围的雷数());

jf.add(b1);

jf.add(b2);

jf.add(b3);

jf.add(b4);

jf.add(b5);

jf.add(b6);

jf.add(b7);

jf.add(b8);

jf.add(b9);

jf.setSize(300, 300);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);

}

boolean 雷=false,

周围无雷=true;

int 周围的雷数=0;

int 探雷标记=-1;

JLabel label;

Point p;

public Block()

{

label=new JLabel();

setBorder(BorderFactory.createMatteBorder(1,

1,

0,

0,

Color.lightGray));

label.setBorder(BorderFactory.createMatteBorder(1,

1,

0,

0,

Color.lightGray));

label.setHorizontalAlignment(JLabel.CENTER);

label.setLayout(new BorderLayout());

label.add(this,BorderLayout.CENTER);

p=new Point(0,0);

addMouseListener(this);

}

public void 设置是否是雷(boolean b)

{

雷=b;

}

public boolean 判断是否是雷()

{

return 雷;

}

public void 设置是雷时的图标(Icon icon)

{

label.setIcon(icon);

}

public void 设置周围无雷时的图标(Icon icon)

{

label.setIcon(icon);

}

public void 设置周围有雷时的名字(String name)

{

label.setText(name);

}

public String 获取名字()

{

return label.getText();

}

public void 设置周围的雷数(int n)

{

周围的雷数=n;

}

public int 获取周围的雷数()

{

return 周围的雷数;

}

public void 设置在雷区中的位置(int i,int j)

{

p=new Point(i,j);

}

public Point 获取在雷区中的位置()

{

return p;

}

public int 获取探雷标记()

{

return 探雷标记;

}

public void mousePressed(MouseEvent e)

{

boolean right=SwingUtilities.isRightMouseButton(e);

if((right==true)(this.isOpaque()==true))

{

探雷标记=(探雷标记+1)%3;

repaint();

}

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

if(探雷标记==0)

{

g.setColor(Color.red);

g.fillRect(6,4,8,6);

g.drawLine(13,10,14,18);

}

if(探雷标记==1)

{

g.setColor(Color.blue);

g.drawString("?",6,15);

}

}

public void mouseEntered(MouseEvent e)

{}

public void mouseReleased(MouseEvent e)

{}

public void mouseExited(MouseEvent e)

{}

public void mouseClicked(MouseEvent e)

{}

}

急!高手!以下是JAVA扫雷游戏中的一部分代码1!帮我做下详细的注释

上面不是写着注释了...

发一部分上来

哪知道是做什么用的..

JAVA代码解释及流程图 有会编JAVA的 讲一扫雷游戏的题 将明白的分都给你

扫雷游戏的算法概述:

你可以把地雷所在的区域抽象成一个二维数组。数组里的元素是该地周围的雷数。然后根据玩家所设定的地雷个数用一个随机数生成器来撒雷。有雷的地方可以用-1来表示,没有雷的地方在撒雷的时候数它旁边的雷数,然后将值填入对应的数组元素里。这样,你的雷阵已经部署完毕。

接下来是玩家的操作了。如果你有仔细观察的话,当你点击一个位置的是后发生的情况有3种:

1. 如果该处有雷的话,很简单,游戏结束

2. 如果该处没有雷,但是这个地方附近有至少一个雷的话,只将当前的位置周围雷数显示出来。

3. 如果该处没有雷,并且附近也没有雷的话,就用一个循环或是递归的方法将它四周的没有雷的地方也显示出来。如果他四周的位置的四周也没有雷的话,继续这个过程,知道四周至少有一个雷时候停止。

玩家事件的处理:

玩家的事件有三种:单击,左右键同时点击,和右击。

单击可以想象成走到该处,如果有雷的话就游戏结束,没有的话就根据我以上讲的三点来判断该做什么。

左右键同时点击的话就将该处四周可能有雷的地方显示出来,如果玩家错将没有雷的地方放了小红旗的话,游戏结束。

右击是放小红旗。

当所有雷都被清楚的时候,游戏结束,玩家胜利。

java扫雷游戏代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Frame

extends JFrame {

JTextField text;

JLabel nowBomb, setBomb;

int BombNum, BlockNum; // 当前雷数,当前方块数

int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数

JButton start = new JButton(" 开始 ");

JPanel MenuPamel = new JPanel();

JPanel bombPanel = new JPanel();

Bomb[][] bombButton;

JPanel c;

BorderLayout borderLayout1 = new BorderLayout();

GridLayout gridLayout1 = new GridLayout();

public Frame() {

try {

setDefaultCloseOperation(EXIT_ON_CLOSE);

jbInit();

}

catch (Exception exception) {

exception.printStackTrace();

}

}

private void jbInit() throws Exception {

c = (JPanel) getContentPane();

setTitle("扫雷");

c.setBackground(Color.WHITE);

MenuPamel.setBackground(Color.GRAY);

c.setLayout(borderLayout1);

setSize(new Dimension(600, 600));

setResizable(false);

BlockNum = 144;

BombNum = 10;

text = new JTextField("10 ", 3);

nowBomb = new JLabel("当前雷数" + ":" + BombNum);

setBomb = new JLabel("设置地雷数");

start.addActionListener(new Frame1_start_actionAdapter(this));

MenuPamel.add(setBomb);

MenuPamel.add(text);

MenuPamel.add(start);

MenuPamel.add(nowBomb);

c.add(MenuPamel, java.awt.BorderLayout.SOUTH);

bombPanel.setLayout(gridLayout1);

gridLayout1.setColumns( (int) Math.sqrt(BlockNum));

gridLayout1.setRows( (int) Math.sqrt(BlockNum));

bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (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].setSize(10, 10);

bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小

bombButton[i][j].setForeground(Color.white);

bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));

bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));

bombPanel.add(bombButton[i][j]);

}

}

c.add(bombPanel, java.awt.BorderLayout.CENTER);

startBomb();

}

/* 开始按钮 */

public void start_actionPerformed(ActionEvent e) {

int num=Integer.parseInt(text.getText().trim());

if (num = 5 num 50) {

BombNum = num;

startBomb();

}

else if (num 5) {

JOptionPane.showMessageDialog(null, "您设置的地雷数太少了,请重设!", "错误",

JOptionPane.ERROR_MESSAGE);

num=10;

BombNum = num;

}

else {

JOptionPane.showMessageDialog(null, "您设置的地雷数太多了,请重设!", "错误",

JOptionPane.ERROR_MESSAGE);

num=10;

BombNum = num;

}

}

/* 开始,布雷 */

public void startBomb() {

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].isRight = false;

bombButton[i][j].BombFlag = 0;

bombButton[i][j].BombRoundCount = 9;

bombButton[i][j].setEnabled(true);

bombButton[i][j].setText("");

bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小

bombButton[i][j].setForeground(Color.BLUE);

rightBomb = 0;

restBomb = BombNum;

restBlock = BlockNum - BombNum;

}

}

for (int i = 0; i BombNum; ) {

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) {

bombButton[x][y].isBomb = true;

i++;

}

}

CountRoundBomb();

}

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

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) {

for (int x = i - 1; x i + 2; x++) {

for (int y = j - 1; y j + 2; y++) {

if ( (x = 0) (y = 0)

(x ( (int) Math.sqrt(BlockNum)))

(y ( (int) Math.sqrt(BlockNum)))) {

if (bombButton[x][y].isBomb == true) {

count++;

}

}

}

}

bombButton[i][j].BombRoundCount = count;

}

}

}

}

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

public void isWin() {

restBlock = BlockNum - BombNum;

for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {

for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {

if (bombButton[i][j].isClicked == true) {

restBlock--;

}

}

}

if (rightBomb == BombNum || restBlock == 0) {

JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",

JOptionPane.INFORMATION_MESSAGE);

startBomb();

}

}

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

public void isNull(Bomb ClickedButton) {

int i, j;

i = ClickedButton.num_x;

j = ClickedButton.num_y;

for (int x = i - 1; x i + 2; x++) {

for (int y = j - 1; y j + 2; y++) {

if ( ( (x != i) || (y != j)) (x = 0) (y = 0)

(x ( (int) Math.sqrt(BlockNum)))

(y ( (int) Math.sqrt(BlockNum)))) {

if (bombButton[x][y].isBomb == false

bombButton[x][y].isClicked == false

bombButton[x][y].isRight == false) {

turn(bombButton[x][y]);

}

}

}

}

}

/* 翻开 */

public void turn(Bomb ClickedButton) {

ClickedButton.setEnabled(false);

ClickedButton.isClicked = true;

if (ClickedButton.BombRoundCount 0) {

ClickedButton.setText(ClickedButton.BombRoundCount + "");

}

else {

isNull(ClickedButton);

}

}

/* 左键点击 */

public void actionPerformed(ActionEvent e) {

if ( ( (Bomb) e.getSource()).isClicked == false

( (Bomb) e.getSource()).isRight == false) {

if ( ( (Bomb) e.getSource()).isBomb == false) {

turn( ( (Bomb) e.getSource()));

isWin();

}

else {

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].setText("b");

}

}

}

( (Bomb) e.getSource()).setForeground(Color.RED);

( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));

( (Bomb) e.getSource()).setText("X");

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

startBomb();

}

}

}

/* 右键点击 */

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 (restBomb 0) {

bombSource.setForeground(Color.RED);

bombSource.setText("F");

bombSource.isRight = true;

restBomb--;

}

else {

bombSource.BombFlag = 0;

}

}

else if (bombSource.BombFlag == 2) {

restBomb++;

bombSource.setText("Q");

bombSource.isRight = false;

}

else {

bombSource.setText("");

}

if (bombSource.isBomb == true) {

if (bombSource.BombFlag == 1) {

rightBomb++;

}

else if (bombSource.BombFlag == 2) {

rightBomb--;

}

}

nowBomb.setText("当前雷数" + ":" + restBomb);

isWin();

}

}

public static void main(String[] args) {

Frame frame = new Frame();

frame.setVisible(true);

}

}

class Frame1_start_actionAdapter

implements ActionListener {

private Frame adaptee;

Frame1_start_actionAdapter(Frame adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.start_actionPerformed(e);

}

}

////////////////////////////

class Bomb

extends JButton {

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

int BombRoundCount; // 周围雷数

boolean isBomb; // 是否为雷

boolean isClicked; // 是否被点击

int BombFlag; // 探雷标记

boolean isRight; // 是否点击右键

public Bomb(int x, int y) {

num_x = x;

num_y = y;

BombFlag = 0;

BombRoundCount = 9;

isBomb = false;

isClicked = false;

isRight = false;

}

}

class Bomb_actionAdapter

implements ActionListener {

private Frame adaptee;

Bomb_actionAdapter(Frame adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.actionPerformed(e);

}

}

class Bomb_mouseAdapter

extends MouseAdapter {

private Frame adaptee;

Bomb_mouseAdapter(Frame adaptee) {

this.adaptee = adaptee;

}

public void mouseClicked(MouseEvent e) {

adaptee.mouseClicked(e);

}

}

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

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

悬赏100元钱。注释一个java扫雷游戏源代码,就是把每行的意思写到后面就可以了!在线等。

import java.awt.*;

import javax.swing.*;

import java.util.Random;

import java.awt.event.*;

class Min extends JPanel //雷的类

{

//备注:鼠标的左键 = 1;右键 = 3;中键 = 2

private int flag = 0,statu = 0; //定义雷的属性 0:没有打开 1:打开 2:标示为雷 3:不确定

//flag = 0 不是雷 ; flag = 1是雷

private int but,count = 0; //but:哪一个鼠标键被按下去了 count:这个区域周围有多少个雷

private int mx = 0,my = 0,mw = 10; //定义雷的坐标和宽度

public Min() //构造函数

{

statu = 0;

}

public Min(int f,int x,int y,int w)

//构造函数

{

flag = f;

mx = x;

my = y;

mw = w;

}

public int getFlag(){return flag;}

public int getStatu(){return statu;}

public int getMx(){return mx;}

public int getMy(){return my;}

public int getMw(){return mw;}

public int getCount(){return count;}

public void setFlag(int f){flag = f;}

public void setCount(int c){count = c;}

public void setData(int f,int x,int y,int w,int s)

//传递值

{

flag = f;

mx = (x-1)*w;

my = (y-1)*w;

mw = w-1;

statu = s;

}

//根据你点击鼠标的不同来改变雷的属性

public int sendKey(int key)

{

//返回值,如果游戏结束则返回-1

int rtn = 1;

if(key == 3)

{

switch(statu)

{

case 1:

break;

case 2:

statu = 3;

break;

case 3:

statu = 0;

break;

case 0:

statu = 2;

break;

}

rtn = 1;

}

if(key == 1 statu == 0)

{

switch(flag)

{

case 0:

statu = 1;

rtn = 2;

break;

case 1:

statu = 1;

rtn = -1;

break;

}

}

return rtn;

}

}

class DrawPanel extends JPanel

{

private int i,j;

private int f = 0; //if f = 1 then game over ,if f =2 then win

private int chx = 0,chy = 0; //专门记录坐标x,y的值

private int msum = 6,ksum = 0; //msum:雷的个数,ksum:标示雷的个数

private int bx = 10,by = 10,bw = 40; //bx,by:棋盘的大小,bw:棋子的大小

public Min board[][] = {

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

};

// 画坐标为ax,ay区域的雷的状态

public void draw(Graphics g,int ax,int ay)

{

int x,y,w; // 坐标x,y;和宽度:w

int s,c,flag; //状态;雷的个数;

int cx = bw/2 - 4;

int cy = bw/2 + 4;

x = board[ax][ay].getMx();

y = board[ax][ay].getMy();

w = board[ax][ay].getMw();

s = board[ax][ay].getStatu();

c = board[ax][ay].getCount();

flag= board[ax][ay].getFlag();

switch(s)

{

case 0: //没有打开状态

{

g.setColor(Color.black);

g.fillRect(x,y,w,w);

break;

}

case 1: //打开状态

{

g.setColor(Color.blue);

g.fillRect(x,y,w,w);

if(c != 0 flag == 0) //此处没有雷

{

g.setColor(Color.red);

g.drawString(String.valueOf(c),x + cx,y + cy);

}

if(flag == 1) //此处有雷

{

g.setColor(Color.red);

g.fillRect(x,y,w,w);

g.setColor(Color.blue);

g.drawString(" 雷",x + cx,y + cy);

}

break;

}

case 2: //标雷状态

{

g.setColor(Color.green);

g.fillRect(x,y,w,w);

g.setColor(Color.blue);

g.drawString(" 旗",x + cx,y + cy);

break;

}

case 3: //不确定状态

{

g.setColor(Color.black);

g.fillRect(x,y,w,w);

g.setColor(Color.red);

g.drawString("?",x + cx,y + cy);

break;

}

default:

break;

}

}

// 没有图形器的绘图函数:画出坐标ax,ay的雷的状态和图形

public void draw(int ax,int ay)

{

Graphics g;

g = this.getGraphics();

draw(g,ax,ay);

}

//打开周围没有雷的地方,并且绘画所在区域点击左键触发

public int openNoMin(int ax,int ay)

{

int i,j;

if(ax1||ay1||axbx||ayby) return 0; //鼠标点击的区域出界了

if(board[ax][ay].getStatu() != 0) return 0; //如果此区域打开了,返回

board[ax][ay].sendKey(1); //如果返回值等于-1,就说明游戏结束

draw(ax,ay);

if(board[ax][ay].getFlag() == 1)

//如果游戏结束,把所有的雷都显示出来

{

for(i = 1;i=bx;i++)

{

for(j = 1;j = by;j++)

{

if(board[i][j].getFlag() == 1)

{

board[i][j].sendKey(1);

draw(i,j);

}

}

}

return -1;

}

//如果游戏没有结束

if(board[ax][ay].getCount() 0)

{

ksum ++;

return 1; //周围有雷,就不用打开周围地区

}

if(board[ax][ay].getCount() == 0 board[ax][ay].getFlag() == 0)

//周围没有雷,打开周围地区,直到有雷的地区

{

openNoMin(ax-1,ay-1);openNoMin(ax,ay-1);openNoMin(ax+1,ay-1);

openNoMin(ax-1,ay ); openNoMin(ax+1,ay );

openNoMin(ax-1,ay+1);openNoMin(ax,ay+1);openNoMin(ax+1,ay+1);

}

ksum ++;

return 1;

}

//计算坐标x,y的周围雷的个数

public int getCount(int ai,int aj)

{

int sum = 0;

if(board[ai][aj].getFlag() == 1)

{

return sum;

}

if(ai1aj1aibxajby)

{

sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+

board[ai-1][aj ].getFlag()+ board[ai+1][aj ].getFlag()+

board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();

}

if(ai==1aj==1)

{

sum = board[ai+1][aj ].getFlag()+

board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();

}

if(ai==1aj==by)

{

sum = board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+

board[ai+1][aj ].getFlag();

}

if(ai==bxaj==1)

{

sum = board[ai-1][aj ].getFlag()+

board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag();

}

if(ai==bxaj==by)

{

sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+

board[ai-1][aj ].getFlag();

}

if(ai==1aj1ajby)

{

sum = board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+

board[ai+1][aj ].getFlag()+

board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();

}

if(ai==bxaj1ajby)

{

sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+

board[ai-1][aj ].getFlag()+

board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag();

}

if(ai1aibxaj==1)

{

sum = board[ai-1][aj ].getFlag()+ board[ai+1][aj ].getFlag()+

board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();

}

if(ai1aibxaj==by)

{

sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+

board[ai-1][aj ].getFlag()+ board[ai+1][aj ].getFlag();

}

return sum;

}

// 传入参数:几列,几行,宽度,雷数

public void initMin(int ax,int ay,int aw,int as)

{

int k = 1; //表明产生的第几个雷

Random r; //随机数

f = 0; //f=0表示游戏还没有结束

ksum = 0;

bx = ax;

by = ay;

bw = aw;

msum = as;

r = new Random();

//初始化底盘的值

for(i = 1;i = bx;i++)

{

for(j=1;j=by;j++)

{

board[i][j].setData(0,i,j,bw,0);

}

}

// 随机产生雷

while(k = msum)

{

i = r.nextInt(bx)+1;

j = r.nextInt(by)+1;

if(board[i][j].getFlag() != 1)

{

board[i][j].setFlag(1);

k++;

}

}

// 非雷区的周围有几个雷,初始化其值

for(i = 1;i = bx;i++)

{

for(j=1;j=by;j++)

{

board[i][j].setCount(getCount(i,j));

}

}

setBackground(Color.white);

repaint();

}

// 构造函数

public DrawPanel(int ax,int ay,int aw,int as)

{

initMin(ax,ay,aw,as);

addMouseListener(new MouseAdapter()

{

public void mousePressed(MouseEvent me)

{

int r;

if(f != 0) return; //如果游戏结束,返回

chx = me.getX();

chy = me.getY();

if(me.getButton() != 1)

{

board[chx/bw+1][chy/bw+1].sendKey(me.getButton());

draw(chx/bw+1,chy/bw+1);

}

else if(me.getButton() == 1)

{

if(openNoMin(chx/bw+1,chy/bw+1) == -1)

{

f = 1;

repaint();

}

else if ( ksum + msum == bx*by )

{

f = 2;

repaint();

}

}

}

}

);

}

// 重画所有的图形,包括一些修饰的图形

public void paint(Graphics g)

{

int x,y,w;

int s;

int cx = bw/2 - 4;

int cy = bw/2 + 4;

g.clearRect(0,0,600,600);

for(i=1;i=bx;i++)

{

for(j=1;j=by;j++)

{

draw(g,i,j);

}

}

if(f == 1)

{

Font f = new Font("11",1,70);

Font fo = g.getFont();

g.setColor(Color.white);

g.setFont(f);

//g.setSize();

g.drawString("Game Over",0,200);

g.setFont(fo);

}

if( f == 2 )

{

Font f = new Font("11",1,70);

Font fo = g.getFont();

g.setColor(Color.white);

g.setFont(f);

//g.setSize();

g.drawString("You win!",0,200);

g.setFont(fo);

}

}

};

// 主类和程序的入口

public class Mine extends JFrame implements ActionListener

{

Container cp = getContentPane();

JButton bt = new JButton("开局");

Label l1 = new Label("列:");

Label l2 = new Label("行:");

Label l3 = new Label("宽度:");

Label l4 = new Label("雷的个数:");

TextField tf1 = new TextField("10",2); //列

TextField tf2 = new TextField("10",2); //行

TextField tf3 = new TextField("40",2); //宽度

TextField tf4 = new TextField("15",2); //雷的个数

int x=10,y=10,w=40,sum=15;

DrawPanel dp = new DrawPanel(x,y,w,sum);

public Mine()

{

setBackground(Color.white);

cp.setLayout(null);

cp.add(dp);

cp.add(bt);

cp.add(tf1);

cp.add(tf2);

cp.add(tf3);

cp.add(tf4);

cp.add(l1);

cp.add(l2);

cp.add(l3);

cp.add(l4);

l1.setBounds(20 ,10,20,20);

tf1.setBounds(40,10,20,20);

l2.setBounds(70,10,20,20);

tf2.setBounds(90,10,20,20);

l3.setBounds(120,10,40,20);

tf3.setBounds(160,10,20,20);

l4.setBounds(190,10,60,20);

tf4.setBounds(250,10,20,20);

bt.setBounds(300,10,80,20);

dp.setBounds(20,40,x*w,y*w);

setResizable(false);

setSize(x*w+40,y*w+80);

setTitle(" 扫雷");

show();

bt.addActionListener(this);

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{System.exit(0);}

}

);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == bt)

{

//x = Integer.parseInt(tf1.getText());

//y = Integer.parseInt(tf2.getText());

//w = Integer.parseInt(tf3.getText());

sum = Integer.parseInt(tf4.getText());

setSize(x*w+40,y*w+80);

dp.setBounds(20,40,x*w,y*w);

show();

dp.initMin(x,y,w,sum);

}

}

public static void main(String args[])

{

new Mine();

}

};


网站名称:java扫雷代码解析 扫雷java源码
标题来源:http://kswsj.cn/article/higodi.html

其他资讯