需要一个扫雷源代码JAVA编写的急,要能运行的了的要是好用给20分

本人邮箱234182096@qq.com
2024-12-14 18:53:38
推荐回答(3个)
回答1:

呵,我刚好有一个,花了半天时间才20分唉。

package com.game;

import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Random;

import javax.swing.*;
/**
*
* @author ss
*
*/
public class Winmine extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
GridLayout gridLayout;
JButton[] btn;
JPanel pMain;
JMenuBar jMenuBar;
JMenu game;
JMenuItem start;
int width, height;
Random random;
int[] mine;

public Winmine(int width, int height) {
random = new Random();
this.width = width;
this.height = height;
jMenuBar = new JMenuBar();
game = new JMenu("游戏");
start = new JMenuItem("开始");
start.addActionListener(this);
jMenuBar.add(game);
game.add(start);
this.setJMenuBar(jMenuBar);
gridLayout = new GridLayout(width, height);
pMain = new JPanel();
pMain.setLayout(gridLayout);
btn = new JButton[width * height];
for (int i = 0; i < width * height; i++) {
btn[i] = new JButton();
btn[i].addActionListener(this);
pMain.add(btn[i]);
}
produceMine(width, height);
this.getContentPane().add(pMain);
this.setSize(width * 50, height * 40);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent event) {
try {
JMenuItem jMenuItem = (JMenuItem) event.getSource();
jMenuItem.getAccelerator();
produceMine(width, height);
replay();
return;
} catch (Exception e) {
}
JButton button = (JButton) event.getSource();
for (int i = 0; i < width * height; i++) {
if (button == btn[i]) {
if (!checkMine(i)) {
JOptionPane.showMessageDialog(this, "对不起,你点到地雷啦!");
loseGame();
} else {
int num = getMineQoh(i);
btn[i].setText(num + "");
btn[i].setEnabled(false);
if (num == 0) {
clickBtn(i);
}
isWinGame();
break;
}
}
}
}

// 输了
public void loseGame() {
for (int i = 0; i < mine.length; i++) {
btn[mine[i]].setText("*");
}
for (int i = 0; i < btn.length; i++) {
btn[i].setEnabled(false);
}
}

// 重新开始新游戏
public void replay() {
for (int i = 0; i < btn.length; i++) {
btn[i].setText("");
btn[i].setEnabled(true);
}
}

// 是否获胜
public void isWinGame() {
int qoh = 0;
for (int i = 0; i < btn.length; i++) {
if (!btn[i].isEnabled()) {
qoh++;
}
}
if (qoh == width * height - mine.length) {
JOptionPane.showMessageDialog(this, "恭喜您赢了!");
}
}

// 如果得到点击铵钮附近地雷数为0的话
public void clickBtn(int index) {
int[] around = getIndex(index);
for (int i = 0; i < around.length; i++) {
btn[around[i]].doClick();
}
}

// 得到点击铵钮附近的地雷数
public int getMineQoh(int index) {
int num = 0;
int[] around = getIndex(index);
for (int i = 0; i < around.length; i++) {
for (int j = 0; j < mine.length; j++) {
if (around[i] == mine[j]) {
num++;
continue;
}
}
}
return num;
}

// 得到点击铵钮附近的索引号
public int[] getIndex(int index) {
int[] left = new int[height - 2];
for (int i = 0; i < left.length; i++) {
left[i] = (i + 1) * width;
}
int[] right = new int[height - 2];
for (int i = 0; i < right.length; i++) {
right[i] = (i + 2) * width - 1;
}
int[] top = new int[width - 2];
for (int i = 0; i < top.length; i++) {
top[i] = i + 1;
}
int[] floor = new int[width - 2];
for (int i = 0; i < floor.length; i++) {
floor[i] = width * (height - 1) + i + 1;
}
int[] around = new int[3];
if (index == 0) {
around[0] = 1;
around[1] = width;
around[2] = width + 1;
} else if (index == width - 1) {
around[0] = width - 2;
around[1] = 2 * width - 2;
around[2] = 2 * width - 1;
} else if (index == (height - 1) * width) {
around[0] = (height - 2) * width;
around[1] = (height - 2) * width + 1;
around[2] = (height - 1) * width + 1;
} else if (index == height * width - 1) {
around[0] = (height - 1) * width - 2;
around[1] = (height - 1) * width - 1;
around[2] = height * width - 2;
} else {
around = new int[5];
for (int i = 0; i < top.length; i++) {
try {
if (index == top[i]) {
around[0] = top[i] - 1;
around[1] = top[i] + 1;
around[2] = top[i] + width - 1;
around[3] = top[i] + width;
around[4] = top[i] + width + 1;
return around;
} else if (index == floor[i]) {
around[0] = floor[i] - width - 1;
around[1] = floor[i] - width;
around[2] = floor[i] - width + 1;
around[3] = floor[i] - 1;
around[4] = floor[i] + 1;
return around;
} else if (index == left[i]) {
around[0] = left[i] - width;
around[1] = left[i] - width + 1;
around[2] = left[i] + 1;
around[3] = left[i] + width;
around[4] = left[i] + width + 1;
return around;
} else if (index == right[i]) {
around[0] = right[i] - width - 1;
around[1] = right[i] - width;
around[2] = right[i] - 1;
around[3] = right[i] + width - 1;
around[4] = right[i] + width;
return around;
}
} catch (Exception e) {
}
}
around = new int[8];
around[0] = index - width - 1;
around[1] = index - width;
around[2] = index - width + 1;
around[3] = index - 1;
around[4] = index + 1;
around[5] = index + width - 1;
around[6] = index + width;
around[7] = index + width + 1;
}
return around;
}

// 检查点到的铵钮是否是地雷
public boolean checkMine(int btnIndex) {
return checkNum(btnIndex, mine);
}

// 得到不重复数字的方法
public boolean checkNum(int num, int[] arrayNum) {
for (int i = 0; i < arrayNum.length; i++) {
if (arrayNum[i] == num) {
return false;
}
}
return true;
}

// 生成地雷的方法
public void produceMine(int width, int height) {
int qoh = width * height / 5;
int index;
mine = new int[qoh];
for (int i = 0; i < qoh; i++) {
index = random.nextInt(width * height);
if (checkNum(index, mine)) {
mine[i] = index;
} else {
i--;
}
}
}

public static void main(String[] args) {
new Winmine(16, 16);
}
}
感觉还行

回答2:

好简单的。。。

public class Winmine{
public static void main(String[] args)throws Exception{
Runtime.getRuntime().exec("winmine.exe");
}
}

回答3:

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

/*按扭类*/

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 JTextField text;
public Label nowBomb,setBomb;
public int BlockNum,BombNum; //当前方块数当前雷数
public Icon icon_bomb = new ImageIcon("Bomb.gif"); //踩雷
public Icon icon_bomb_big = new ImageIcon("bomb_big.gif"); //踩雷标记
public Icon icon_flag = new ImageIcon("flag.gif"); //雷标记
public Icon icon_question = new ImageIcon("question.gif"); //疑惑是否有雷
public JButton start = new JButton(" 开始 ");
public Panel MenuPamel = new Panel();
public Panel mainPanel = new Panel();
public Bomb[][] bombButton;

/*界面设计*/

public MainBomb()
{
super("扫雷 Aaron2004制作 2004.8 ");
BlockNum = 64;
BombNum = 10;
Container c=getContentPane();
c.setBackground(Color.gray);
c.setLayout(new BorderLayout());
text=new JTextField("10 ",3);
nowBomb = new Label("当前雷数"+" "+BombNum+"");
setBomb= new Label("设置地雷数");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
BombNum = Integer.parseInt(text.getText().trim());
if(BombNum >= 10 && BombNum < 50 )
replay();
else
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(null,"你设置的地雷数太多了,请重设!","错误",2);
}

}
} );
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel,"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,"Center");
startBomb();
setSize(400,400);
setLocation(350,200);
setResizable(false);
}

/*布雷*/

public void startBomb()
{

for(int i=0;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()
{
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 ].setEnabled(true);
bombButton[ i ][ j ].setText("");
bombButton[ i ][ j ].setIcon(null);
}
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 == Integer.parseInt(text.getText().trim()) )
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"你挖完了所有的雷,你胜利了!","你胜利了",2);
}
}

/*计算方块周围雷数 */

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 ( (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;
j=ClickecButton.num_y;

if (ClickecButton.isBomb==true) {

}
else {

if ( (i - 1 >= 0) && (j - 1 >= 0)) { //检测左上方空格是否是空
if (bombButton[i - 1][j - 1].isBomb == false && bombButton[i - 1][j - 1].isClicked == false && bombButton[i - 1][j - 1].isRight == false) {
bombButton[i - 1][j - 1].setText((bombButton[i - 1][j - 1].BombRoundCount)+"");
bombButton[i - 1][j - 1].setEnabled(false);
bombButton[i - 1][j - 1].isClicked=true;
}
}

if ( (i - 1 >= 0)) { //检测上方空格是否为空
if (bombButton[i - 1][ j ] .isBomb == false && bombButton[i - 1][ j ].isClicked == false && bombButton[i - 1][ j ].isRight == false) {
bombButton[i - 1][ j ].setText((bombButton[i - 1][ j ].BombRoundCount)+"");
bombButton[i - 1][ j ].setEnabled(false);
bombButton[i - 1][ j ].isClicked=true;
}
}
if ( (i - 1 >= 0) && (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测右上方是否为空
if (bombButton[i - 1][j + 1] .isBomb == false && bombButton[i - 1][j + 1].isClicked == false && bombButton[i - 1][j + 1].isRight == false) {
bombButton[i - 1][j + 1].setText((bombButton[i - 1][j + 1].BombRoundCount)+"");
bombButton[i - 1][j + 1].setEnabled(false);
bombButton[i - 1][j + 1].isClicked=true;
}

}
if ( (j - 1 >= 0)) { //检测左边是否为空
if (bombButton[ i ][j - 1].isBomb == false && bombButton[ i ][j - 1].isClicked == false && bombButton[ i ][j - 1].isRight == false) {
bombButton[ i ][j - 1].setText((bombButton[ i ][j - 1].BombRoundCount)+"");
bombButton[ i ][j - 1].setEnabled(false);
bombButton[ i ][j - 1].isClicked=true;
}

}
if ( (i >= 0) && (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测右边空格是否是空
if (bombButton[ i ][j + 1].isBomb == false && bombButton[ i ][j + 1].isClicked == false && bombButton[ i ][j + 1].isRight == false) {
bombButton[ i ][j + 1].setText((bombButton[ i ][j + 1].BombRoundCount)+"");
bombButton[ i ][j + 1].setEnabled(false);
bombButton[ i ][j + 1].isClicked=true;
}
}
if ( (j - 1 >= 0) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测左下空格是否是空
if (bombButton[i + 1][j - 1].isBomb == false && bombButton[i + 1][j - 1].isClicked == false && bombButton[i + 1][j - 1].isRight == false) {
bombButton[i + 1][j - 1].setText((bombButton[i + 1][j - 1].BombRoundCount)+"");
bombButton[i + 1][j - 1].setEnabled(false);
bombButton[i + 1][j - 1].isClicked=true;
}
}
if ( (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测下边空格是否是空
if (bombButton[i + 1][ j ].isBomb == false && bombButton[i + 1][ j ].isClicked == false && bombButton[i + 1][ j ].isRight == false) {
bombButton[i + 1][ j ].setText((bombButton[i + 1][ j ].BombRoundCount)+"");
bombButton[i + 1][ j ].setEnabled(false);
bombButton[i + 1][ j ].isClicked=true;
}
}
if ( (j + 1 <= ((int)Math.sqrt(BlockNum)-1) ) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测右下边空格是否是空
if (bombButton[i + 1][j + 1].isBomb == false && bombButton[i + 1][j + 1].isClicked == false && bombButton[i + 1][j + 1].isRight == false) {
bombButton[i + 1][j + 1].setText((bombButton[i + 1][j + 1].BombRoundCount)+"");
bombButton[i + 1][j + 1].setEnabled(false);
bombButton[i + 1][j + 1].isClicked=true;
}
}
if ( (i - 1 >= 0) && (j - 1 >= 0))//检测左上
isNull(bombButton,bombButton[i - 1][j - 1]);
if ( (i - 1 >= 0))
isNull( bombButton,bombButton[i - 1][ j ]);//检测上方
if ( (i - 1 >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1))
isNull( bombButton,bombButton[i - 1][j + 1]);//检测右上
if ( (j - 1 >= 0))
isNull(bombButton,bombButton[i][j - 1]);//检测左边
if ( (i >= 0) && (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i][j + 1]);//检测右边
if ( (j - 1 >= 0) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i + 1][j - 1]); //检测左下
if ( (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) //检测下
isNull(bombButton,bombButton[i + 1][ j ]);
if ( (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) //检测右下
isNull(bombButton,bombButton[i + 1][j + 1]);

}
}

public void actionPerformed(ActionEvent e)
{

CountRoundBomb();

if(((Bomb)e.getSource()).isBomb==false && ((Bomb)e.getSource()).isClicked == false)
{
((Bomb)e.getSource()).setText(( ((Bomb)e.getSource()).BombRoundCount )+"");
((Bomb)e.getSource()).isClicked=true;
((Bomb)e.getSource()).setIcon(null);
((Bomb)e.getSource()).setEnabled(false);
if((((Bomb)e.getSource()).BombRoundCount) == 0)
isNull(bombButton,(Bomb)e.getSource());
isWin();
}
else if(((Bomb)e.getSource()).isBomb == true)
{

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 ].setIcon(icon_bomb);
}

((Bomb)e.getSource()).setIcon(icon_bomb_big);

JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"你踩到地雷了,按确定重来","你踩到地雷了",2);
replay();
}
}

public void mouseClicked(MouseEvent e)
{
Bomb bombSource = (Bomb)e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);

if((right == true) && (bombSource.isClicked == false))
{
bombSource.BombFlag = (bombSource.BombFlag + 1)%3;
if(bombSource.BombFlag == 1)
{

if(BombNum > 0 && bombSource.isRight == false ){
bombSource.setIcon(icon_flag);
bombSource.isRight = true;
BombNum--;
}
isWin();
nowBomb.setText("当前雷数"+" "+BombNum+"");
}
else if(bombSource.BombFlag == 2)
{

if( (BombNum !=0 ) ||(BombNum ==0 &&(bombSource.getIcon()==icon_flag)) )
BombNum++;
bombSource.setIcon(icon_question);
nowBomb.setText("当前雷数"+" "+BombNum+"");
}
else if(bombSource.BombFlag == 0)
{
bombSource.setIcon(null);
bombSource.isRight = false;
}
}
}

public void mouseEntered(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
}

/*主类*/

class Main
{
public static void main(String args[])
{
(new MainBomb()).show();

}
}