📄 miner21.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/*按扭类*/
class Bomb extends JButton
{
public int num_x,num_y; //第几号方块
public int BombRoundCount; //周围雷数
public boolean isBomb; //是否为雷
public boolean isClicked; //是否被点击
public int BombFlag; //探雷标记
public boolean isRight; //是否点击右键
public Bomb(int x,int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
BombRoundCount = 0;
isBomb = false;
isClicked = false;
isRight = false;
}
}
/*窗口及算法实现类*/
class MainBomb extends JFrame implements ActionListener,MouseListener
{
public Label nowBomb;
public int BlockNum,BombNum,Bomb2,Bomb3,sizex,sizey; //当前方块数当前雷数
public JButton start = new JButton(" 开始 ");
public Panel MenuPamel = new Panel();
public Panel mainPanel = new Panel();
public Bomb[][] bombButton;
public JList List;
public final String name[] = {"easy","normal","hard"};
/*界面设计*/
public MainBomb()
{
super("扫雷游戏 guanchuang制作 2007.5 ");
BlockNum = 100;
BombNum = 10;
Bomb2 = Bomb3=10;
sizex = 500;
sizey = 500;
Container c=getContentPane();
c.setBackground(Color.gray);
c.setLayout(new BorderLayout());
List = new JList(name);
List.setVisibleRowCount(1);
List.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
MenuPamel.add(List);
List.addListSelectionListener(
new ListSelectionListener(){
public void valueChanged(ListSelectionEvent Event){
if (List.getSelectedIndex()==0){
BlockNum = 100;
BombNum = 10;
Bomb2 = Bomb3=10;
sizex = 500;
sizey = 500;}
if (List.getSelectedIndex()==1){
BlockNum = 150;
BombNum = 20;
Bomb2 =Bomb3= 20;
sizex = 650;
sizey = 650; }
if (List.getSelectedIndex()==2){
BlockNum = 200;
BombNum = 30;
Bomb2 =Bomb3= 30;
sizex = 800;
sizey = 800; }
}
}
);
nowBomb = new Label("当前剩下雷数"+" "+BombNum+"");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
replay(); //重新开始
}
} );
MenuPamel.add(start);
MenuPamel.add(nowBomb);
MenuPamel.setLayout(new FlowLayout() );
c.add(MenuPamel,BorderLayout.NORTH);
mainPanel.setLayout(new GridLayout( (int)Math.sqrt(BlockNum) , (int)Math.sqrt(BlockNum)) );
bombButton=new Bomb[ (int)Math.sqrt(BlockNum) ][];
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++)
{
bombButton[ i ]=new Bomb[ (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 ].setForeground( Color.gray);
bombButton[ i ][ j ].addActionListener(this);
bombButton[ i ][ j ].addMouseListener(this);
}
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++ )
for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++ )
mainPanel.add(bombButton[ i ][ j ]);
c.add(mainPanel,BorderLayout.CENTER);
startBomb(); //布置地雷
setSize(sizex,sizey);
setLocation(350,200);
setResizable(false);
setVisible(true);
}
//布置地雷
public void startBomb()
{
for(int i=0;i<BombNum;i++)
{
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)
i--;
else
bombButton[ x ][ y ].isBomb=true ;
}
}
//重新开始
public void replay()
{
BombNum = Bomb3;
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 ].setEnabled(true);
bombButton[ i ][ j ].setText("");
}
startBomb();
}
/*是否挖完了所有的雷*/
public void isWin()
{
int findBomb=0; //找到的地雷数
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 ].isRight == true)
findBomb++;
}
if( findBomb == BombNum )
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"您挖完了所有的雷,您胜利了!","您胜利了",JOptionPane.INFORMATION_MESSAGE);
}
}
/*计算方块周围雷数 */
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) {
if ( (i - 1 >= 0) && (j - 1 >= 0)) {
if (bombButton[i - 1][j - 1].isBomb == true) {
count += 1; //检测左上方空格是否是地雷
}
}
if ( (i - 1 >= 0)) {
if (bombButton[i - 1][ j ].isBomb == true) {
count += 1; //检测上方空格是否为地雷
}
}
if ( (i - 1 >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i - 1][j + 1] .isBomb == true) {
count += 1; //检测右上方是否为地雷
}
}
if ( (i >= 0) &&(j - 1 >= 0)) {
if (bombButton[ i ][j - 1] .isBomb == true) {
count += 1; //检测左边是否为地雷
}
}
if ( (i >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[ i ][j + 1].isBomb == true) {
count += 1; //右边
}
}
if ( (j - 1 >= 0) && (i + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][j - 1].isBomb == true) {
count += 1; //左下
}
}
if ( (i + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][ j ].isBomb == true) {
count += 1; //下
}
}
if ( (j + 1 <= (int)Math.sqrt(BlockNum)-1) && (i + 1 <= Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][j + 1].isBomb == true) {
count += 1; //右下
}
}
bombButton[ i ][ j ].BombRoundCount = count;
}
}
}
}
/**当选中的位置为空,则翻开周围的地图**/
public void isNull(Bomb[][] bombButton,Bomb ClickecButton)
{
int i,j;
i=ClickecButton.num_x;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -