怎么用Java做一个扫雷程序,要原创。。。 做好了给加100
2个回答
展开全部
第一个JAVA文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 显示所有按钮的面板
* @author Administrator
*
*/
public class AllButtonPanel extends JPanel implements ActionListener{
private int row;//行数
private int col;//列数
private int mineCount;//地雷数
private MineButton[][] allButtons;//所有按钮
public AllButtonPanel(int row,int col,int mineCount){
this.row=row;
this.col=col;
this.mineCount=mineCount;
allButtons=new MineButton[row][col];
createButtons();
createMine();
init();
}
private void init(){
this.setLayout(new GridLayout(row,col));
for(int i=0;i<allButtons.length;i++){
for(int j=0;j<allButtons[i].length;j++){
this.add(allButtons[i][j]);
}
}
}
/**
* 随机布雷的方法
*
*/
private void createMine(){
int n=0;
while(n<mineCount){//随机生成mineCount个地雷
int i=(int)(Math.random()*row);
int j=(int)(Math.random()*col);
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(-1);
n++;
}
}
for(int i=0;i<allButtons.length;i++){//计算每个位置的周围地雷数
for(int j=0;j<allButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(getSurroundMineCount(i,j));
}
}
}
}
/**
* 统计(i,j)坐标周围8个位置的地雷数
* @param data
* @param i
* @param j
* @return
*/
private int getSurroundMineCount(int i,int j){
int num=0;//统计周围的雷数
if(i-1>=0&&j-1>=0){
num+=(allButtons[i-1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i-1>=0){
num+=(allButtons[i-1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i-1>=0&&j+1<allButtons[0].length){
num+=(allButtons[i-1][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(j-1>=0){
num+=(allButtons[i][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(j+1<allButtons[0].length){
num+=(allButtons[i][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length&&j-1>=0){
num+=(allButtons[i+1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length){
num+=(allButtons[i+1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length&&j+1<allButtons[0].length){
num+=(allButtons[i+1][j+1].getCountOfSurroundMines()==-1?1:0);
}
return num;
}
/**
* 生成按钮
*
*/
private void createButtons(){
for(int i=0;i<allButtons.length;i++){
for(int j=0;j<allButtons[i].length;j++){
allButtons[i][j]=new MineButton(i,j);
allButtons[i][j].setSize(6,6);
allButtons[i][j].addActionListener(this);//添加点击事件监听
allButtons[i][j].addMouseListener(new MouseAdapter(){//添加鼠标右键事件监听
public void mouseClicked(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON3){
int remain=Integer.parseInt(CleanMine.remainMine.getText());
JButton b=(JButton)e.getSource();
if(b.getText().equals("")){
remain--;
CleanMine.remainMine.setText(remain+"");
b.setText("&");
}else if(b.getText().equals("&")){
remain++;
CleanMine.remainMine.setText(remain+"");
b.setText("");
}
}
}
});
}
}
}
public void actionPerformed(ActionEvent e) {//点击事件监听的方法
MineButton b=(MineButton)e.getSource();
int r=b.getRow();
int c=b.getCol();
if(allButtons[r][c].getCountOfSurroundMines()==-1){//如果是地雷
for(int i=0;i<allButtons.length;i++){//把所有按钮都显示出来
for(int j=0;j<allButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()==-1){//如果该位置是地雷
allButtons[i][j].setText("$");
}else if(allButtons[i][j].getCountOfSurroundMines()==0){//如果该位置为空(该位置不是地雷,周围8个位置也没有地雷)
allButtons[i][j].setText("");
allButtons[i][j].setBackground(Color.CYAN);
}else{//如果该位置不是地雷,但周围8个位置中有地雷
allButtons[i][j].setText(allButtons[i][j].getCountOfSurroundMines()+"");
allButtons[i][j].setBackground(Color.CYAN);
}
}
}
}else{//如果不是地雷
showEmpty(r,c);//执行排空操作
}
}
/**
* 排空方法,若(i,j)位置为空,则显示空白。然后依次递归找它周围的8个位置。
* @param data
* @param i
* @param j
*/
private void showEmpty(int i,int j){
MineButton b=allButtons[i][j];
if(b.isCleared()){
return;
}
if(allButtons[i][j].getCountOfSurroundMines()==0){
b.setBackground(Color.CYAN);
b.setCleared(true);
if(i-1>=0&&j-1>=0){
showEmpty(i-1,j-1);
}
if(i-1>=0){
showEmpty(i-1,j);
}
if(i-1>=0&&j+1<allButtons[0].length){
showEmpty(i-1,j+1);
}
if(j-1>=0){
showEmpty(i,j-1);
}
if(j+1<allButtons[0].length){
showEmpty(i,j+1);
}
if(i+1<allButtons.length&&j-1>=0){
showEmpty(i+1,j-1);
}
if(i+1<allButtons.length){
showEmpty(i+1,j);
}
if(i+1<allButtons.length&&j+1<allButtons[0].length){
showEmpty(i+1,j+1);
}
}else if(allButtons[i][j].getCountOfSurroundMines()>0){
b.setText(allButtons[i][j].getCountOfSurroundMines()+"");
b.setBackground(Color.CYAN);
b.setCleared(true);
}
}
}
第二个JAVA文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 扫雷游戏主界面
* @author tony.tang
*
*/
public class CleanMine extends JFrame implements ActionListener{
private JLabel text1,text2;
public static JLabel remainMine;//剩余地雷数
private JLabel time;//消耗时间
private JButton reset;//重新开始
private JPanel center;
private int row,col,mine;
public CleanMine(){
text1=new JLabel("剩余地雷:");
text2=new JLabel("消耗时间:");
remainMine=new JLabel("10");
time=new JLabel("0");
reset=new JButton("重新开始");
reset.addActionListener(this);
JMenuBar bar=new JMenuBar();
JMenu game=new JMenu("游戏");
JMenu help=new JMenu("帮助");
JMenuItem item;
game.add(item=new JMenuItem("开局"));item.addActionListener(this);
game.addSeparator();
ButtonGroup bg=new ButtonGroup();
game.add(item=new JCheckBoxMenuItem("初级",true));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("中级"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("高级"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("自定义..."));bg.add(item);item.addActionListener(this);
game.addSeparator();
game.add(item=new JMenuItem("退出"));item.addActionListener(this);
help.add(item=new JMenuItem("查看帮助"));item.addActionListener(this);
help.add(item=new JMenuItem("关于扫雷..."));item.addActionListener(this);
bar.add(game);
bar.add(help);
this.setJMenuBar(bar);
init();
}
private void init(){
JPanel north=new JPanel();
north.add(text1);
north.add(remainMine);
north.add(reset);
north.add(text2);
north.add(time);
this.add(north,BorderLayout.NORTH);
this.row=9;
this.col=9;
this.mine=10;
restart();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Thread(){
public void run(){
while(Integer.parseInt(remainMine.getText())>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
time.setText((Integer.parseInt(time.getText())+1)+"");
}
}
}.start();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("初级")){
this.row=9;
this.col=9;
this.mine=10;
restart();
return;
}
if(e.getActionCommand().equals("中级")){
this.row=16;
this.col=16;
this.mine=40;
restart();
return;
}
if(e.getActionCommand().equals("高级")){
this.row=16;
this.col=30;
this.mine=99;
restart();
return;
}
if(e.getActionCommand().equals("重新开始")){
restart();
return;
}
}
private void restart(){
if(center!=null){
this.remove(center);
}
center=new AllButtonPanel(row,col,mine);
this.add(center,BorderLayout.CENTER);
this.remainMine.setText(mine+"");
this.time.setText("0");
this.setSize(col*30,row*30+10);
this.setResizable(false);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new CleanMine();
}
}
第三个JAVA文件.
import javax.swing.JButton;
import java.awt.*;
public class MineButton extends JButton {
private int row;
private int col;
private boolean cleared=false;
private int countOfSurroundMines;//周围地雷数,如果本按钮是雷,则为-1;
public MineButton(int row,int col){
this.row=row;
this.col=col;
this.setMargin(new Insets(0,0,0,0));
}
public int getCol() {
return col;
}
public int getRow() {
return row;
}
public boolean isCleared() {
return cleared;
}
public void setCleared(boolean cleared) {
this.cleared = cleared;
}
public int getCountOfSurroundMines() {
return countOfSurroundMines;
}
public void setCountOfSurroundMines(int countOfSurroundMines) {
this.countOfSurroundMines = countOfSurroundMines;
}
}
全部编译以后就可以执行了
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 显示所有按钮的面板
* @author Administrator
*
*/
public class AllButtonPanel extends JPanel implements ActionListener{
private int row;//行数
private int col;//列数
private int mineCount;//地雷数
private MineButton[][] allButtons;//所有按钮
public AllButtonPanel(int row,int col,int mineCount){
this.row=row;
this.col=col;
this.mineCount=mineCount;
allButtons=new MineButton[row][col];
createButtons();
createMine();
init();
}
private void init(){
this.setLayout(new GridLayout(row,col));
for(int i=0;i<allButtons.length;i++){
for(int j=0;j<allButtons[i].length;j++){
this.add(allButtons[i][j]);
}
}
}
/**
* 随机布雷的方法
*
*/
private void createMine(){
int n=0;
while(n<mineCount){//随机生成mineCount个地雷
int i=(int)(Math.random()*row);
int j=(int)(Math.random()*col);
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(-1);
n++;
}
}
for(int i=0;i<allButtons.length;i++){//计算每个位置的周围地雷数
for(int j=0;j<allButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(getSurroundMineCount(i,j));
}
}
}
}
/**
* 统计(i,j)坐标周围8个位置的地雷数
* @param data
* @param i
* @param j
* @return
*/
private int getSurroundMineCount(int i,int j){
int num=0;//统计周围的雷数
if(i-1>=0&&j-1>=0){
num+=(allButtons[i-1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i-1>=0){
num+=(allButtons[i-1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i-1>=0&&j+1<allButtons[0].length){
num+=(allButtons[i-1][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(j-1>=0){
num+=(allButtons[i][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(j+1<allButtons[0].length){
num+=(allButtons[i][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length&&j-1>=0){
num+=(allButtons[i+1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length){
num+=(allButtons[i+1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length&&j+1<allButtons[0].length){
num+=(allButtons[i+1][j+1].getCountOfSurroundMines()==-1?1:0);
}
return num;
}
/**
* 生成按钮
*
*/
private void createButtons(){
for(int i=0;i<allButtons.length;i++){
for(int j=0;j<allButtons[i].length;j++){
allButtons[i][j]=new MineButton(i,j);
allButtons[i][j].setSize(6,6);
allButtons[i][j].addActionListener(this);//添加点击事件监听
allButtons[i][j].addMouseListener(new MouseAdapter(){//添加鼠标右键事件监听
public void mouseClicked(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON3){
int remain=Integer.parseInt(CleanMine.remainMine.getText());
JButton b=(JButton)e.getSource();
if(b.getText().equals("")){
remain--;
CleanMine.remainMine.setText(remain+"");
b.setText("&");
}else if(b.getText().equals("&")){
remain++;
CleanMine.remainMine.setText(remain+"");
b.setText("");
}
}
}
});
}
}
}
public void actionPerformed(ActionEvent e) {//点击事件监听的方法
MineButton b=(MineButton)e.getSource();
int r=b.getRow();
int c=b.getCol();
if(allButtons[r][c].getCountOfSurroundMines()==-1){//如果是地雷
for(int i=0;i<allButtons.length;i++){//把所有按钮都显示出来
for(int j=0;j<allButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()==-1){//如果该位置是地雷
allButtons[i][j].setText("$");
}else if(allButtons[i][j].getCountOfSurroundMines()==0){//如果该位置为空(该位置不是地雷,周围8个位置也没有地雷)
allButtons[i][j].setText("");
allButtons[i][j].setBackground(Color.CYAN);
}else{//如果该位置不是地雷,但周围8个位置中有地雷
allButtons[i][j].setText(allButtons[i][j].getCountOfSurroundMines()+"");
allButtons[i][j].setBackground(Color.CYAN);
}
}
}
}else{//如果不是地雷
showEmpty(r,c);//执行排空操作
}
}
/**
* 排空方法,若(i,j)位置为空,则显示空白。然后依次递归找它周围的8个位置。
* @param data
* @param i
* @param j
*/
private void showEmpty(int i,int j){
MineButton b=allButtons[i][j];
if(b.isCleared()){
return;
}
if(allButtons[i][j].getCountOfSurroundMines()==0){
b.setBackground(Color.CYAN);
b.setCleared(true);
if(i-1>=0&&j-1>=0){
showEmpty(i-1,j-1);
}
if(i-1>=0){
showEmpty(i-1,j);
}
if(i-1>=0&&j+1<allButtons[0].length){
showEmpty(i-1,j+1);
}
if(j-1>=0){
showEmpty(i,j-1);
}
if(j+1<allButtons[0].length){
showEmpty(i,j+1);
}
if(i+1<allButtons.length&&j-1>=0){
showEmpty(i+1,j-1);
}
if(i+1<allButtons.length){
showEmpty(i+1,j);
}
if(i+1<allButtons.length&&j+1<allButtons[0].length){
showEmpty(i+1,j+1);
}
}else if(allButtons[i][j].getCountOfSurroundMines()>0){
b.setText(allButtons[i][j].getCountOfSurroundMines()+"");
b.setBackground(Color.CYAN);
b.setCleared(true);
}
}
}
第二个JAVA文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 扫雷游戏主界面
* @author tony.tang
*
*/
public class CleanMine extends JFrame implements ActionListener{
private JLabel text1,text2;
public static JLabel remainMine;//剩余地雷数
private JLabel time;//消耗时间
private JButton reset;//重新开始
private JPanel center;
private int row,col,mine;
public CleanMine(){
text1=new JLabel("剩余地雷:");
text2=new JLabel("消耗时间:");
remainMine=new JLabel("10");
time=new JLabel("0");
reset=new JButton("重新开始");
reset.addActionListener(this);
JMenuBar bar=new JMenuBar();
JMenu game=new JMenu("游戏");
JMenu help=new JMenu("帮助");
JMenuItem item;
game.add(item=new JMenuItem("开局"));item.addActionListener(this);
game.addSeparator();
ButtonGroup bg=new ButtonGroup();
game.add(item=new JCheckBoxMenuItem("初级",true));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("中级"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("高级"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("自定义..."));bg.add(item);item.addActionListener(this);
game.addSeparator();
game.add(item=new JMenuItem("退出"));item.addActionListener(this);
help.add(item=new JMenuItem("查看帮助"));item.addActionListener(this);
help.add(item=new JMenuItem("关于扫雷..."));item.addActionListener(this);
bar.add(game);
bar.add(help);
this.setJMenuBar(bar);
init();
}
private void init(){
JPanel north=new JPanel();
north.add(text1);
north.add(remainMine);
north.add(reset);
north.add(text2);
north.add(time);
this.add(north,BorderLayout.NORTH);
this.row=9;
this.col=9;
this.mine=10;
restart();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Thread(){
public void run(){
while(Integer.parseInt(remainMine.getText())>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
time.setText((Integer.parseInt(time.getText())+1)+"");
}
}
}.start();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("初级")){
this.row=9;
this.col=9;
this.mine=10;
restart();
return;
}
if(e.getActionCommand().equals("中级")){
this.row=16;
this.col=16;
this.mine=40;
restart();
return;
}
if(e.getActionCommand().equals("高级")){
this.row=16;
this.col=30;
this.mine=99;
restart();
return;
}
if(e.getActionCommand().equals("重新开始")){
restart();
return;
}
}
private void restart(){
if(center!=null){
this.remove(center);
}
center=new AllButtonPanel(row,col,mine);
this.add(center,BorderLayout.CENTER);
this.remainMine.setText(mine+"");
this.time.setText("0");
this.setSize(col*30,row*30+10);
this.setResizable(false);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new CleanMine();
}
}
第三个JAVA文件.
import javax.swing.JButton;
import java.awt.*;
public class MineButton extends JButton {
private int row;
private int col;
private boolean cleared=false;
private int countOfSurroundMines;//周围地雷数,如果本按钮是雷,则为-1;
public MineButton(int row,int col){
this.row=row;
this.col=col;
this.setMargin(new Insets(0,0,0,0));
}
public int getCol() {
return col;
}
public int getRow() {
return row;
}
public boolean isCleared() {
return cleared;
}
public void setCleared(boolean cleared) {
this.cleared = cleared;
}
public int getCountOfSurroundMines() {
return countOfSurroundMines;
}
public void setCountOfSurroundMines(int countOfSurroundMines) {
this.countOfSurroundMines = countOfSurroundMines;
}
}
全部编译以后就可以执行了
展开全部
public int time=1000* 60; //60秒倒计时
public boolean running=true;//是否一直运行
JLabel label=new JLable();//显示时间的标签
//启动计时
public void startTimer(){
new java.lang.Thread(new Runnable(){
public void run(){
while(running){
try{
Thread.sleep(1000);//睡一秒
}catch(Exception e){}
time--;
lable.setText(String.valueof(time));
this.update();// 把你的界面刷新一下
if(time<0){//倒计时到零,满足条件
//your code: 游戏失败,做点处理
running=false;//记得置成false否则不退出
}
}
}
}).start();
}
使用时,在你需要使用的时候 调用 startTimer()方法即可
你可以看到, startTimer方法里的线程在不断地改变time的值,每秒减一
所以你需要在你的GUI界面上安装一个 JLabel label,不断地改变label的内容为time就行了
public boolean running=true;//是否一直运行
JLabel label=new JLable();//显示时间的标签
//启动计时
public void startTimer(){
new java.lang.Thread(new Runnable(){
public void run(){
while(running){
try{
Thread.sleep(1000);//睡一秒
}catch(Exception e){}
time--;
lable.setText(String.valueof(time));
this.update();// 把你的界面刷新一下
if(time<0){//倒计时到零,满足条件
//your code: 游戏失败,做点处理
running=false;//记得置成false否则不退出
}
}
}
}).start();
}
使用时,在你需要使用的时候 调用 startTimer()方法即可
你可以看到, startTimer方法里的线程在不断地改变time的值,每秒减一
所以你需要在你的GUI界面上安装一个 JLabel label,不断地改变label的内容为time就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询