短代码的java扫雷 java 扫雷代码

高分求一个运行在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 != nullthis.northWest.isBomb) no;
if(this.north != nullthis.north.isBomb) no;
if(this.northEast != nullthis.northEast.isBomb) no;
if(this.east != nullthis.east.isBomb) no;
if(this.southEast != nullthis.southEast.isBomb) no;
if(this.south != nullthis.south.isBomb) no;
if(this.southWest != nullthis.southWest.isBomb) no;
if(this.west != nullthis.west.isBomb) no;
return no;
}
// 获得该小方块周围的小方块
public Diamond getNearDimaond(int i) {
int index = -1;
switch (i) {
case 1:// 1表示西北,2,表示北,以此类推
index = no - 10;
if(index1 || 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(index1) {
return null;
} else {
return diamonds[index];
}
case 3:
index = no - 8;
if(index1 || 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 = no1;
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 = no10;
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 = no9;
if(index81) {
return null;
} else {
return diamonds[index];
}
case 7:
index = no8;
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(SetDiamond set) {
set.add(this);// 先把自己加上
if(this.getNorthWest() != nullthis.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() != nullthis.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() != nullthis.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() != nullthis.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() != nullthis.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() != nullthis.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() != nullthis.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() != nullthis.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做扫雷 。有几个问题没想到怎么做 。1.每个方块,怎么判定周围八没做过短代码的java扫雷,就是说点啥想法 。
是不是可以用二维数组来定义每个格子,这样判断周围8个格子短代码的java扫雷的时候就可以[i][j]
i和j -1来查看有没有雷,雷也可以存在一个二维数组里,有雷存1,没雷存0 , 你再把它也保存起来,存档的话可以添加一个初始化判断 , 有存档就调出二维数组,去布局显示 。^^
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; // 找到短代码的java扫雷的地雷数短代码的java扫雷,剩余雷数短代码的java扫雷,剩余方块数
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 = 5num50) {
BombNum = num;
startBomb();
}
else if (num5) {
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; iBombNum; ) {
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; xi2; x) {
for (int y = j - 1; yj2; 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, "您挖完了所有的雷短代码的java扫雷,您胜利了!", "胜利",
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; xi2; x) {
for (int y = j - 1; yj2; 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.BombRoundCount0) {
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, "短代码的java扫雷你踩到地雷了,按确定重来", "踩到地雷", 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.BombFlag1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb0) {
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);
}
}
【短代码的java扫雷 java 扫雷代码】短代码的java扫雷的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 扫雷代码、短代码的java扫雷的信息别忘了在本站进行查找喔 。

    推荐阅读