前言
忘记是从何处看到过关于扫雷小程序的文章,所以这次也就跟着做一下。其实很简单的,如果有java入门的同学也可以尝试一下自己做这种java小程序。几行代码做几遍基本上能摸清楚这些基础了,对于编程能力也能提高一些。(虽然小编玩了半天也才仅仅赢了一把)
具体代码如下:
package saolei;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
public class saolei implements ActionListener {
JFrame frame=new JFrame(“扫雷小游戏”);
JButton reset=new JButton(“重来”);
Container container=new Container();
//游戏数据结构
final int row=20;
final in
t col=20;
final int leiCount=30;
JButton [][] buttons=new JButton[row][col];
int [][] counts=new int[row][col];
final int LEICODE=10;
// 构造函数
public saolei(){
//1、设置窗口
frame.setSize(1000, 800);
frame.setResizable(true);
//是否可改变窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//2、添加重来按钮
addResetButton();
//添加按钮
addButtons();
//埋雷
addLei();
//添加雷的计算
calcNeiboLei();
frame.setVisible(true);
}
public void addResetButton(){
reset.setBackground(Color.cyan);
reset.setOpaque(true);
reset.addActionListener(this);
frame.add(reset,BorderLayout.NORTH);
}
public void addLei(){
Random rand=new Random();
int randRow,randCol;
for(int i=0;i<leiCount;i++){
randRow=rand.nextInt(row);
randCol=rand.nextInt(col);
if(counts[randRow][randCol]== LEICODE){
i–;
}else{
counts[randRow][randCol]=LEICODE;
//buttons[randRow][randCol].setText(“*”);
}
}
}
public void addButtons(){
frame.add(container,BorderLayout.CENTER);
container.setLayout(new GridLayout(row,col));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
JButton button=new JButton();
button.setBackground(Color.orange);
button.setOpaque(true);
button.addActionListener(this);
buttons[i][j]=button;
container.add(button);
}
}
}
public void calcNeiboLei(){
int count;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
count=0;
if(counts[i][j]==LEICODE) continue;
if(i>0 && j>0 && counts[i-1][j-1]==LEICODE) count++;
if(i>0&&counts[i-1][j]==LEICODE) count++;
if(i>0 && j<19 && counts[i-1][j+1]==LEICODE) count++;
if(j>0 && counts[i][j-1]==LEICODE) count++;
if(j<19 && counts[i][j+1]==LEICODE) count++;
if(i<19&&j>0&&counts[i+1][j-1]==LEICODE) count++;
if(i<19&&counts[i+1][j]==LEICODE) count++;
if(i<19&&j<19&&counts[i+1][j+1]==LEICODE) count++;
counts[i][j]=count;
//buttons[i][j].setText(counts[i][j]+“”);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton button=(JButton)e.getSource();
if(button.equals(reset)){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
buttons[i][j].setText(“”);
buttons[i][j].setEnabled(true);
buttons[i][j].setBackground(Color.orange);
counts[i][j]=0;
}
}
addLei();
calcNeiboLei();
}else{
int count=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(button.equals(buttons[i][j])){
count=counts[i][j];
if(count==LEICODE){
LoseGame();
}else{
openCell(i,j);
checkWin();
} return;
}
}
}
}
}
void checkWin(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return;
}
}
JOptionPane.showMessageDialog(frame, “握草牛啤,你竟然赢了!”);
}
void openCell(int i,int j){
if(buttons[i][j].isEnabled()==false) return;
buttons[i][j].setEnabled(false);
if(counts[i][j]==0){
if(i>0 && j>0 && counts[i-1][j-1]!=LEICODE) openCell(i-1, j-1);
if(i>0&&counts[i-1][j]!=LEICODE) openCell(i-1, j);
if(i>0 && j<19 && counts[i-1][j+1]!=LEICODE) openCell(i-1, j+1);
if(j>0 && counts[i][j-1]!=LEICODE) openCell(i, j-1);
if(j<19 && counts[i][j+1]!=LEICODE) openCell(i, j+1);
if(i<19&&j>0&&counts[i+1][j-1]!=LEICODE) openCell(i+1, j-1);
if(i<19&&counts[i+1][j]!=LEICODE) openCell(i+1, j);
if(i<19&&j<19&&counts[i+1][j+1]!=LEICODE) openCell(i+1, j+1);
&&j>0&&counts[i+1][j-1]!=LEICODE) openCell(i+1, j-1);
if(i<19&&counts[i+1][j]!=LEICODE) openCell(i+1, j);
if(i<19&&j<19&&counts[i+1][j+1]!=LEICODE) openCell(i+1, j+1);