扫雷代码java_Java版的扫雷游戏源码

packagecom.xz.sl;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.GridLayout;importjava.awt.Insets;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.Random;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JOptionPane;public class Saolei extendsJFrame{final int ROW = 20;final int COL = 20;final int LEI = 30;

JButton[][] buttons= newJButton[ROW][COL];int[][] counts = new int[ROW][COL];final int LCODE = 11; //雷的编码

Container container= newContainer();publicSaolei() {

init();

}private voidinit() {

JButton restBtn= new JButton("重来");

restBtn.setOpaque(true);

restBtn.setBackground(Color.PINK);

restBtn.addActionListener(newActionListener() {

@Overridepublic voidactionPerformed(ActionEvent e) {for(int i=0;i

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

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

buttons[i][j].setBackground(Color.YELLOW);

counts[i][j]= 0;

}

}

mailei();

jslsl();

}

});

setLayout(newBorderLayout());

add(restBtn,BorderLayout.NORTH);

add(container,BorderLayout.CENTER);

container.setLayout(newGridLayout(ROW,COL));for(int i=0;i

JButton button= newJButton();

button.setMargin(new Insets(0, 0, 0, 0));

button.setBackground(Color.YELLOW);

button.setOpaque(true);

buttons[i][j]=button;

button.addActionListener(newActionListener() {

@Overridepublic voidactionPerformed(ActionEvent e) {

JButton button=(JButton) e.getSource();if(button.equals(restBtn)) {

}else{int count = 0;for(int i=0;i

count=counts[i][j];//踩到雷了

if(count ==LEI) {

clcl();

}else{

openCell(i,j);

checkWin();

}return;

}

}

}

}

}private voidcheckWin() {for(int i=0;i

if(buttons[i][j].isEnabled() == true && counts[i][j] !=LEI) {return;

}

}

}

JOptionPane.showMessageDialog(container,"恭喜您赢了...");

}private void openCell(int i,intj ) {//如果格子已经打开,直接返回

if(buttons[i][j].isEnabled() == false) {return;

}

buttons[i][j].setText(counts[i][j]+"");

buttons[i][j].setEnabled(false);

buttons[i][j].setBackground(Color.CYAN);if(counts[i][j] == 0) {//左上角

if(i > 0 && j > 0 && counts[i-1][j-1] !=LEI) {

openCell(i-1, j-1);

}if(i > 0 && j > 0 && counts[i-1][j] !=LEI) {

openCell(i-1, j);

}if(i > 0 && j < 19 && counts[i-1][j+1] !=LEI) {

openCell(i-1, j+1);

}if(i > 0 && j > 0 && counts[i][j-1] !=LEI) {

openCell(i, j-1);

}if(i > 0 && j < 19 && counts[i][j+1] !=LEI) {

openCell(i, j+1);

}if(i < 19 && j > 0 && counts[i+1][j-1] !=LEI) {

openCell(i+1, j-1);

}if(i < 19 && j > 0 && counts[i+1][j] !=LEI) {

openCell(i+1, j);

}if(i < 19 && j < 19 && counts[i+1][j+1] !=LEI) {

openCell(i+1, j+1);

}

}else{

buttons[i][j].setText(counts[i][j]+"");

}

}

});

container.add(button);

}

}

mailei();//计算周边的雷的数量

jslsl();

setVisible(true);

setTitle("扫雷游戏");

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(600,700);

}private voidjslsl() {for(int i=0;i

}//左上角

if(i > 0 && j > 0 && counts[i-1][j-1] ==LEI) {

count++;

}if(i > 0 && j > 0 && counts[i-1][j] ==LEI) {

count++;

}if(i > 0 && j < 19 && counts[i-1][j+1] ==LEI) {

count++;

}if(i > 0 && j > 0 && counts[i][j-1] ==LEI) {

count++;

}if(i > 0 && j < 19 && counts[i][j+1] ==LEI) {

count++;

}if(i < 19 && j > 0 && counts[i+1][j-1] ==LEI) {

count++;

}if(i < 19 && j > 0 && counts[i+1][j] ==LEI) {

count++;

}if(i < 19 && j < 19 && counts[i+1][j+1] ==LEI) {

count++;

}

counts[i][j]=count;//buttons[i][j].setText(counts[i][j]+" ");

}

}

}private voidclcl() {for(int i=0;i

buttons[i][j].setText("X");

buttons[i][j].setBackground(Color.RED);

buttons[i][j].setEnabled(false);

}else{

buttons[i][j].setText(c+"");

buttons[i][j].setEnabled(false);

}

}

}

}private voidmailei() {

Random random= newRandom();intrandRow,randCol;for(int i=0;i

randRow=random.nextInt(ROW);

randCol=random.nextInt(COL);if(counts[randRow][randCol] ==LEI) {

i--;

}else{

counts[randRow][randCol]=LEI;//buttons[randRow][randCol].setText(LEI+"");

}

}

}public static voidmain(String[] args) {newSaolei();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值