java编写扫雷程序的流程图,哪位大侠帮下,小弟急用~~~

现在就差流程图了,哪位高手帮忙编个

我有源代码:绝对可以通过,不过比较简单而已,对学生而言应该可以了吧,这是以前写的:
一下两个文件放在一个包里就行了
/*
This class defines a class that contains some useful
attributions and some methods to set or get these attributions
*/
import javax.swing.JButton;
public class ExButton extends JButton
{
//if the button is a mine,the isMine will be true
private boolean isMine;
//to check if a button has been visited is useful
//when using the recursion in the Game class
private boolean isVisited;
//the row number of the button
int btnRowNumber;
//the column number of the button
int btnColumnNumber;
//the mines around a button
int minesAround=0;
public void setIndex(int btnRowNumber,int btnColumnNumber)
{
this.btnRowNumber=btnRowNumber;
this.btnColumnNumber=btnColumnNumber;

}
public int getRowNumber()
{
return this.btnRowNumber;
}
public int getColumnNumber()
{
return this.btnColumnNumber;
}
public void setVisited(boolean isVisited)
{
this.isVisited=isVisited;
}
public boolean getVisited()
{
return this.isVisited;
}
public void setMine(boolean isMine)
{
this.isMine=isMine;
}
public boolean getMine()
{
return this.isMine;
}
//the attribute of minesAround add one each
//time a mine is put down around the button

public void addMinesAround()
{
this.minesAround++;
}
public int getMinesAround()
{
return this.minesAround;
}
}
-------------------------------------------------
/*
File Name: Game.java
Author: Tian Wei Student Number: Email: [email protected]
Assignment number: #4
Description: In this program ,a frame will be created which contains
ten "mines".When you click a button ,it will present the
number of mines around or a message of losing the game
(if the button is a mine).You can make a right click to
sign a dengerous button as well.When all the mines have
been signed ,a message box of winning the game will jump
to the screen.And the the message of the time you used in
the game.More over,you can click the button on the bottom
to restart the game.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import java.util.Timer;
public class Game extends JFrame{
//define some menber variables
private long minute=0,second=0;//take down time used int the game
private ExButton[][] btn;//two-dimension array present the buttons
private JLabel label;
private JButton restart;//restart button
private int minesRemained;//remained mines that you have not signed
private boolean thisTry=true;
private JLabel timeUsed=new JLabel ();
private Random rand=new Random();
private final int ROWS,COLUMNS;
private final int MINES;
// the constuctor
public Game(int rows,int columns,int mines)
{
super("Find mines");
this.ROWS=rows;
this.COLUMNS=columns;
this.MINES=mines;
minesRemained=MINES;
Timer timer=new Timer();//Timer's object to timer the game
timer.schedule(new MyTimer(), 0, 1000);//do the function every second
Container container=getContentPane();
container.setLayout(new BorderLayout());
//Jpanel in the Container
JPanel jpanel=new JPanel();
jpanel.setLayout(new GridLayout(ROWS,COLUMNS));
restart=new JButton("click me to restart the game");
JPanel jpanel2=new JPanel();
//Another JPanel in the Container
jpanel2.setLayout(new FlowLayout());
jpanel2.add(timeUsed);
jpanel2.add(restart);
ButtonListener restartHandler=new ButtonListener();
restart.addActionListener(restartHandler);
container.add(jpanel2,BorderLayout.SOUTH);
btn=new ExButton[ROWS+2][COLUMNS+2];
//initialize the buttons
for(int i=0;i<=ROWS+1;i++)
{
for(int j=0;j<=COLUMNS+1;j++)
{
btn[i][j]=new ExButton();
btn[i][j].addMouseListener(new MouseClickHandler());
btn[i][j].setIndex(i,j);
btn[i][j].setVisited(false);
}

}
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
jpanel.add(btn[i][j]);

container.add(jpanel,BorderLayout.CENTER);
JPanel jpanel3=new JPanel ();
label=new JLabel();
label.setText("Mines remaining "+MINES);
jpanel3.add(label);

container.add(jpanel3,BorderLayout.NORTH );
this.addMines();
this.addMinesAround();
}
//randomly put ten mines
private void addMines()
{
for(int i=1;i<=MINES;i++)
{
int raInt1=rand.nextInt(ROWS);
int raInt2=rand.nextInt(COLUMNS);
if((raInt1==0)||(raInt2==0)||btn[raInt1][raInt2].getMine())
i--;
else
btn[raInt1][raInt2].setMine(true);
}
}
//take down the mines around a button
private void addMinesAround()
{
for(int i=1;i<=ROWS;i++)
{
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine())
{
btn[i][j-1].addMinesAround();
btn[i][j+1].addMinesAround();
btn[i-1][j-1].addMinesAround();
btn[i-1][j].addMinesAround();
btn[i-1][j+1].addMinesAround();
btn[i+1][j-1].addMinesAround();
btn[i+1][j].addMinesAround();
btn[i+1][j+1].addMinesAround();

}
}
}
}
//if a button clicked is a empty one,then use a recursion
//to find all the empty buttons around
private void checkEmpty(ExButton button)
{
button.setVisited(true);
int x=button.getRowNumber();
int y=button.getColumnNumber();
button.setBackground(Color.white);
if((button.getMinesAround()==0)&&(x>=1)&&(x<=ROWS)
&&(y>=1)&&(y<=COLUMNS))
{
if(!btn[x][y-1].getVisited())
checkEmpty(btn[x][y-1]);
if(!btn[x][y+1].getVisited())
checkEmpty(btn[x][y+1]);
if(!btn[x-1][y].getVisited())
checkEmpty(btn[x-1][y]);
if(!btn[x+1][y].getVisited())
checkEmpty(btn[x+1][y]);
if(!btn[x-1][y-1].getVisited())
checkEmpty(btn[x-1][y-1]);
if(!btn[x-1][y+1].getVisited())
checkEmpty(btn[x-1][y+1]);
if(!btn[x+1][y-1].getVisited())
checkEmpty(btn[x+1][y-1]);
if(!btn[x+1][y+1].getVisited())
checkEmpty(btn[x+1][y+1]);
}
else if(button.getMinesAround()>0)
button.setText(""+button.getMinesAround());

}
//the main function
public static void main(String args[])
{
String rows,columns,mines;
int rowNumber,columnNumber,mineNumber;
rows=JOptionPane.showInputDialog("Enter the rows of the game");
columns=JOptionPane.showInputDialog("Enter the columns of the game");
mines=JOptionPane.showInputDialog("Enter the mines of the game");
rowNumber=Integer.parseInt(rows);
columnNumber=Integer.parseInt(columns);
mineNumber=Integer.parseInt(mines);
Game frame=new Game(rowNumber,columnNumber,mineNumber);
frame.setTitle("Find Mines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(220, 80);
frame.setSize(600, 600 );
frame.setVisible(true);
}
//there are three inner class below

//The first inner class is used to do the mouse listener's
//function.When you click a button ,it will present the
//number of mines around or a message of losing the game
//(if the button is a mine).You can make a right click to
//sign a dengerous button as well.When ten mines have been
//signed,it will check whether all the signed ones are mines
private class MouseClickHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
//get the button that been clicked
ExButton eventButton=new ExButton();
eventButton=(ExButton)event.getSource();
eventButton.setVisited(true);
//when it is a right click
if(event.isMetaDown())
{
if(eventButton.getText()=="#")
{
minesRemained++;
eventButton.setText("");

}
else
{
if((eventButton.getBackground()==Color.white)||
(eventButton.getText()!=""))
{
//do nothing
}

else
{
minesRemained--;
eventButton.setText("#");

}
}
label.setText("Mines remaining "+minesRemained);
//check if all the signed buttons are mines
if(minesRemained==0)
{
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine()&&btn[i][j].getText()!="#")
thisTry=false;
if(!btn[i][j].getMine()&&btn[i][j].getText()=="#")
thisTry=false;

}
if(thisTry)
{
//win the game
JOptionPane.showMessageDialog(null, "You succeed" +
" in this experience!");
JOptionPane.showMessageDialog(null, "Time used:"+
timeUsed.getText());
}

else//you have wrongly signed one or more mines
JOptionPane.showMessageDialog(null, "You have wrongly " +
"signed one or more mines,please check it and go on!");

}
}
else if(event.isAltDown())
{
//do nothing
}
else
{//normally click(left click)
if(eventButton.getText()=="#")
{
//do nothing
}
else if(eventButton.getMine())
{
//lose the game
JOptionPane.showMessageDialog(null, "What a pity!" +
"You failed!" );
//show all the mines to the loser
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine())
btn[i][j].setBackground(Color.BLACK);
}
JOptionPane.showMessageDialog(null, "Time used: 0"+
minute+":"+second);

}
else
{
if(eventButton.getMinesAround()==0)
{
//call the function to find all the empty buttons around
checkEmpty(eventButton);
}
else
eventButton.setText(""+eventButton.getMinesAround());

}
}

}

}
//The second class is to listen to the button which used to
//restart the game.In this class,it will dispose the old frame
//and create a new one(Of course,the mines's position have
//been changed).
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//what to dispose is the object of Game class
Game.this.dispose();
//the same code as in the main function
Game frame=new Game(ROWS,COLUMNS,MINES);
frame.setTitle("Find Mines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600 );
//make sure the frame is at the center of the screen
frame.setLocation(220, 80);
frame.setVisible(true);
}
}
//The last class is the class that will be used in the
//Timer's object timer.It should inherit the class TimerTask
//It is the task that the Timer will do every second
private class MyTimer extends TimerTask
{
public void run()
{
second+=1;
minute+=second/60;
second=second%60;
//change the text of the time used in the game
timeUsed.setText("Time used 0"+minute+":"+second);
}

}
}//end of the class Game
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-04-01
扫雷是一款相当经典的小游戏。他提供了非常友好的界面。

???下面就来讲解我的扫雷程序思想。首先我们在雷区上随机地放上雷,这可以用random类来实现。当没有雷的地方被点击后就会显示一个数字表示它周围有几个雷,这是怎么实现的呢?我们可以把整个雷区看成一个二维数组a[?i ][ j ],如雷区:

????????????? 11 ?12 ?13 ?14 ?15 ?16 ?17 ?18
???????????? ?21 ?22 ?23 ?24 ?25 ?26 ?27 ?28
??????????????31 ?32 ?33 ?34 ?35 ?36 ?37 ?38
????????????? 41 ?42 ?43 ?44 ?45 ?46 ?47 ?48
??????????????51 ?52 ?53 ?54 ?55 ?56 ?57 ?58
???? 我们可以发现a[ I ][ j ]周围存在着如下关系:

?????????? a[i– 1 ][ j – 1 ]????????? a[?i – 1 ] [ j ]? ??????????? a[ I – 1 ][ j + 1 ]

a[????????????? a[ i ][ j – 1 ] ????????????? ?a[?i ][ j ]????????????????????a[ i ][ j + 1 ]

a[????????????? a[?i + 1 ][ j - 1]??????????a[?i + 1 ][ j ]? ???????????? a[ i + 1][ j + 1 ]

????????????于是 ,可以从a[ i ][ j ]的左上角顺时针开始检测。当然,如果超出边界,要用约束条件再加以判断!

???????????? 扫雷程序还会自动展开已确定没有雷的雷区。如果a[3][4]周围雷数为1,a[2][3]已被标示为地雷,那么a[2][4],a[2][5],a[3][3],a[3][5],a[4][3],a[4][4],a[4][5]将被展开,一直波及到不可确定的雷区。这也是实现的关键。我们可以把数组的元素设定为一个类对象,它们所属的类设定这样的一个事件:在被展开时,检查周围的雷数是否与周围标示出来的雷数相等,如果相等则展开周围未标示的雷区。这样新的雷区展开又触发这个事件,就这样递归下去,一直蔓延到不可展开的雷区。相信在了解以上两个要点后,把雷区这个类编写完全(如添加是否有雷标记,是否展开标记,周围雷数等,双击,左右单击的鼠标事件等),实现扫雷程序应是十分简单的一件事。

--------------------------------------

自定义JButton子类:

//ExtendButton.java

package ly.java;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ExtendButton extends JButton
{
private int Button_Pos;
private boolean Button_Status;
private boolean Button_Visited;

public int SetPostion(int pos)
{
this.Button_Pos = (pos >= 0 && pos <= 81) ? pos : 0;
return this.Button_Pos ;
}
public int GetPostion()
{
return this.Button_Pos;
}
public boolean SetStatus(boolean sta)
{
this.Button_Status = sta;
return this.Button_Status;
}
public boolean GetStatus()
{
return this.Button_Status;
}
public boolean Visited()
{
return this.Button_Visited;
}
public boolean SetVisited(boolean vis)
{
this.Button_Visited = vis;
return this.Button_Visited;
}
}

游戏类

//Game.java

package ly.java.game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import ly.java.ExtendButton;

public class Game extends JFrame implements ActionListener{
private Container myContainer;
private GridLayout myLayout;
ExtendButton[] myButton = new ExtendButton[81];

private Game()
{
this.setTitle("Game");
this.setSize( 500,500 );
this.InitButton();
}

private void InitButton()
{
myContainer = getContentPane();
myLayout = new GridLayout( 9, 9, 1, 1 );
myContainer.setLayout( myLayout );
for(int i=0; i < 81; i++)
{
myButton[i] = new ExtendButton();
myButton[i].SetPostion(i);
myContainer.add(myButton[i]);
myButton[i].addActionListener( this );
}
System.gc();
this.SetBomb(13);
show();
}

private void SetBomb(int count)
{
int counter = 0;
int tempint;
while(counter != count)
{
tempint = ( int )(Math.random() * 81);
if(!myButton[tempint].GetStatus())
{
myButton[tempint].SetStatus(true);
counter++;
}
}
}

private void ShowBomb()
{
for(int i = 0; i < 81; i++)
{
if(myButton[i].GetStatus())
{
myButton[i].setBackground( new Color(0,0,0) );
}
}
}

private void CheckButton(ExtendButton TempButton)
{

if(TempButton.GetStatus())
{
JOptionPane.showMessageDialog( null, "You Failed!", "Game",JOptionPane.INFORMATION_MESSAGE );
this.ShowBomb();
return;
}
int[] CircleNum = new int[8];
int temp = 0;
if(!TempButton.Visited())
{

CircleNum[0] = TempButton.GetPostion() - 9;
CircleNum[0] = (CircleNum[0] < 0) ? -1 : CircleNum[0];
CircleNum[1] = TempButton.GetPostion() - 8;
CircleNum[1] = (CircleNum[1] < 0 || CircleNum[1] % 9 == 0) ? -1 : CircleNum[1];
CircleNum[2] = TempButton.GetPostion() + 1;
CircleNum[2] = (CircleNum[2] % 9 == 0) ? -1 : CircleNum[2];
CircleNum[3] = TempButton.GetPostion() + 10;
CircleNum[3] = (CircleNum[3] > 80 || CircleNum[3] % 9 == 0) ? -1 : CircleNum[3];
CircleNum[4] = TempButton.GetPostion() + 9;
CircleNum[4] = (CircleNum[4] > 80) ? -1 : CircleNum[4];
CircleNum[5] = TempButton.GetPostion() + 8;
CircleNum[5] = (CircleNum[5] > 80 || CircleNum[5] % 8 == 0) ? -1 : CircleNum[5];
CircleNum[6] = TempButton.GetPostion() - 1;
CircleNum[6] = (CircleNum[6] % 8 == 0) ? -1 : CircleNum[6];
CircleNum[7] = TempButton.GetPostion() - 10;
CircleNum[7] = (CircleNum[7] < 0 || CircleNum[7] % 8 == 0) ? -1 : CircleNum[7];
for(int i = 0; i < 8; i++)
{
if(CircleNum[i] != -1)
{
if(myButton[CircleNum[i]].GetStatus() && !myButton[CircleNum[i]].Visited()) temp++;
}
}
if(temp > 0)
{
TempButton.SetVisited( true );
TempButton.setText( String.valueOf(temp) );
temp = 0;
}
else if(temp == 0)
{
TempButton.SetVisited( true );
TempButton.setBackground(new Color( 125,152,0 ));
for(int i = 0; i < 8; i++)
{
if(CircleNum[i] != -1 && !myButton[CircleNum[i]].Visited())
{
CheckButton(myButton[CircleNum[i]]);
}
}
}
}
}

public void actionPerformed(ActionEvent e)
{
CheckButton((ExtendButton)e.getSource());
}

public static void main(String[] args)
{
Game newGame = new Game();
newGame.addWindowListener(
new WindowAdapter(){
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
);
}
}

参考资料:http://www.alixixi.com/Dev/Web/JSP/jsp2/2007/2007020812689.html

第2个回答  2008-04-05
前辈 Game.java 编译失败哦!!!!
有14个错误 ~~~
第3个回答  2008-04-09
...难啊...难于上青天.好好想下
第4个回答  2008-04-10
我给你答案吧。
等过两天的,我们现在正有一批小分队做着呢,呵呵。
我们做的是扫雷加强版本,
相似回答