求高手帮忙一个“扫雷”游戏的JAVA的程序代码import javax.swing.ImageIcon;//程序入口
public class Block {
String name;//名字,比如"雷"或数字
int aroundMineNumber;//周围雷扫雷游戏原代码java的数目
ImageIcon mineIcon;//雷扫雷游戏原代码java的图标
boolean isMine=false;//是否是雷
boolean isMark=false;//是否被标记
boolean isOpen=false;//是否被挖开
public void setName(String name) {
this.name=name;
}
//设置周围扫雷游戏原代码java的雷数
public void setAroundMineNumber(int n) {
aroundMineNumber=n;
}
//获得周围的雷数
public int getAroundMineNumber() {
return aroundMineNumber;
}
public String getName() {
return name;
}
//判断是否是雷
public boolean isMine() {
return isMine;
}
//设置是否为雷
public void setIsMine(boolean b) {
isMine=b;
}
//设置雷的图标
public void setMineIcon(ImageIcon icon){
mineIcon=icon;
}
//获得雷的图标
public ImageIcon getMineicon(){
return mineIcon;
}
//确定雷是否被挖开
public boolean getIsOpen() {
return isOpen;
}
//设置为已经被挖开
public void setIsOpen(boolean p) {
isOpen=p;
}
//返回此处是否已经被标记
public boolean getIsMark() {
return isMark;
}
//设置此处是否已经被标记
public void setIsMark(boolean m) {
isMark=m;
}
import javax.swing.*;
import java.awt.*;
public class BlockView extends JPanel{
JLabel blockNameOrIcon; //用来显示Block对象的name、number和mineIcon属性
JButton blockCover;//用来遮挡blockNameOrIcon.
CardLayout card;//卡片式布局
BlockView(){
card=new CardLayout();
setLayout(card);
blockNameOrIcon=new JLabel("",JLabel.CENTER);
blockNameOrIcon.setHorizontalTextPosition(AbstractButton.CENTER);
blockNameOrIcon.setVerticalTextPosition(AbstractButton.CENTER);
blockCover=new JButton();
add("cover",blockCover);
add("view",blockNameOrIcon);
}
//给出视觉效果变化
public void giveView(Block block){
// 如果是雷扫雷游戏原代码java,将对应的图标和文字更改
if(block.isMine){
blockNameOrIcon.setText(block.getName());
blockNameOrIcon.setIcon(block.getMineicon());
}
else {
int n=block.getAroundMineNumber();
if(n=1)
blockNameOrIcon.setText("" n);
else
blockNameOrIcon.setText(" ");
}
}
public void seeBlockNameOrIcon(){
card.show(this,"view");
validate();
}
public void seeBlockCover(){
card.show(this,"cover");
validate();
}
public JButton getBlockCover(){
return blockCover;
}
}
java扫雷游戏代码import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame
extends JFrame {
JTextField text;
JLabel nowBomb, setBomb;
int BombNum, BlockNum; // 当前雷数,当前方块数
int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数
JButton start = new JButton(" 开始 ");
JPanel MenuPamel = new JPanel();
JPanel bombPanel = new JPanel();
Bomb[][] bombButton;
JPanel c;
BorderLayout borderLayout1 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout();
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
c = (JPanel) getContentPane();
setTitle("扫雷");
c.setBackground(Color.WHITE);
MenuPamel.setBackground(Color.GRAY);
c.setLayout(borderLayout1);
setSize(new Dimension(600, 600));
setResizable(false);
BlockNum = 144;
BombNum = 10;
text = new JTextField("10 ", 3);
nowBomb = new JLabel("当前雷数"":"BombNum);
setBomb = new JLabel("设置地雷数");
start.addActionListener(new Frame1_start_actionAdapter(this));
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel, java.awt.BorderLayout.SOUTH);
bombPanel.setLayout(gridLayout1);
gridLayout1.setColumns( (int) Math.sqrt(BlockNum));
gridLayout1.setRows( (int) Math.sqrt(BlockNum));
bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (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].setSize(10, 10);
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小
bombButton[i][j].setForeground(Color.white);
bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
bombPanel.add(bombButton[i][j]);
}
}
c.add(bombPanel, java.awt.BorderLayout.CENTER);
startBomb();
}
/* 开始按钮 */
public void start_actionPerformed(ActionEvent e) {
int num=Integer.parseInt(text.getText().trim());
if (num = 5num50) {
BombNum = num;
startBomb();
}
else if (num5) {
JOptionPane.showMessageDialog(null, "您设置的地雷数太少了,请重设!", "错误",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
else {
JOptionPane.showMessageDialog(null, "您设置的地雷数太多了,请重设!", "错误",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
}
/* 开始,布雷 */
public void startBomb() {
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].BombRoundCount = 9;
bombButton[i][j].setEnabled(true);
bombButton[i][j].setText("");
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小
bombButton[i][j].setForeground(Color.BLUE);
rightBomb = 0;
restBomb = BombNum;
restBlock = BlockNum - BombNum;
}
}
for (int i = 0; iBombNum; ) {
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) {
bombButton[x][y].isBomb = true;
i;
}
}
CountRoundBomb();
}
/* 计算方块周围雷数 */
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) {
for (int x = i - 1; xi2; x) {
for (int y = j - 1; yj2; y) {
if ( (x = 0)(y = 0)
(x( (int) Math.sqrt(BlockNum)))
(y( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == true) {
count;
}
}
}
}
bombButton[i][j].BombRoundCount = count;
}
}
}
}
/* 是否挖完了所有的雷 */
public void isWin() {
restBlock = BlockNum - BombNum;
for (int i = 0; i(int) Math.sqrt(BlockNum); i) {
for (int j = 0; j(int) Math.sqrt(BlockNum); j) {
if (bombButton[i][j].isClicked == true) {
restBlock--;
}
}
}
if (rightBomb == BombNum || restBlock == 0) {
JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",
JOptionPane.INFORMATION_MESSAGE);
startBomb();
}
}
/** 当选中的位置为空,则翻开周围的地图* */
public void isNull(Bomb ClickedButton) {
int i, j;
i = ClickedButton.num_x;
j = ClickedButton.num_y;
for (int x = i - 1; xi2; x) {
for (int y = j - 1; yj2; y) {
if ( ( (x != i) || (y != j))(x = 0)(y = 0)
(x( (int) Math.sqrt(BlockNum)))
(y( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == false
bombButton[x][y].isClicked == false
bombButton[x][y].isRight == false) {
turn(bombButton[x][y]);
}
}
}
}
}
/* 翻开 */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false);
ClickedButton.isClicked = true;
if (ClickedButton.BombRoundCount0) {
ClickedButton.setText(ClickedButton.BombRoundCount"");
}
else {
isNull(ClickedButton);
}
}
/* 左键点击 */
public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false
( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()));
isWin();
}
else {
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].setText("b");
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED);
( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));
( (Bomb) e.getSource()).setText("X");
JOptionPane.showMessageDialog(this, "你踩到地雷了,按确定重来", "踩到地雷", 2);
startBomb();
}
}
}
/* 右键点击 */
public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);
if ( (right == true)(bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb0) {
bombSource.setForeground(Color.RED);
bombSource.setText("F");
bombSource.isRight = true;
restBomb--;
}
else {
bombSource.BombFlag = 0;
}
}
else if (bombSource.BombFlag == 2) {
restBomb;
bombSource.setText("Q");
bombSource.isRight = false;
}
else {
bombSource.setText("");
}
if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
rightBomb;
}
else if (bombSource.BombFlag == 2) {
rightBomb--;
}
}
nowBomb.setText("当前雷数"":"restBomb);
isWin();
}
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
}
}
class Frame1_start_actionAdapter
implements ActionListener {
private Frame adaptee;
Frame1_start_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.start_actionPerformed(e);
}
}
////////////////////////////
class Bomb
extends JButton {
int num_x, num_y; // 第几号方块
int BombRoundCount; // 周围雷数
boolean isBomb; // 是否为雷
boolean isClicked; // 是否被点击
int BombFlag; // 探雷标记
boolean isRight; // 是否点击右键
public Bomb(int x, int y) {
num_x = x;
num_y = y;
BombFlag = 0;
BombRoundCount = 9;
isBomb = false;
isClicked = false;
isRight = false;
}
}
class Bomb_actionAdapter
implements ActionListener {
private Frame adaptee;
Bomb_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e);
【扫雷游戏原代码java 扫雷的源代码编程游戏】}
}
class Bomb_mouseAdapter
extends MouseAdapter {
private Frame adaptee;
Bomb_mouseAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.mouseClicked(e);
}
}
java 扫雷源代码import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ScanLei1 extends JFrameimplements ActionListener{
private static final long serialVersionUID = 1L;
private Container contentPane;
private JButton btn;
private JButton[] btns;
private JLabel b1;
private JLabel b2;
private JLabel b3;
private Timer timer;
private int row=9;
private int col=9;
private int bon=10;
private int[][] a;
private int b;
private int[] a1;
private JPanel p,p1,p2,p3;
public ScanLei1(String title){
super(title);
contentPane=getContentPane();
setSize(297,377);
this.setBounds(400, 100, 400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
timer =new Timer(1000,(ActionListener) this);
a = new int[row 2][col 2];
initGUI();
}
public void initGUI(){
p3=new JPanel();
b=bon;
JMenuBar menuBar=new JMenuBar();
JMenu menu1=new JMenu("游戏");
JMenu menu2=new JMenu("帮助");
JMenuItem mi1=new JMenuItem("初级");
JMenuItem mi2 = new JMenuItem("中级");
JMenuItem mi3 =new JMenuItem("高级");
mi1.addActionListener(this);
menu1.add(mi1);
mi2.addActionListener(this);
menu1.add(mi2);
mi3.addActionListener(this);
menu1.add(mi3);
menuBar.add(menu1);
menuBar.add(menu2);
p3.add(menuBar);
b1=new JLabel(bon "");
a1=new int[bon];
btn =new JButton("开始");
btn.addActionListener(this);
b2=new JLabel("0");
b3=new JLabel("");
btns=new JButton[row*col];
p=new JPanel();
p.setLayout(new BorderLayout());
contentPane.add(p);
p.add(p3,BorderLayout.NORTH);
//combo=new JComboBox(new Object[]{"初级","中级","高级"} );
//加监听
/*combo.addItemListener(new ItemListener(){
}});*/
p1=new JPanel();
//在那个位置
//(( FlowLayout)p1.getLayout()).setAlignment( FlowLayout.RIGHT);
p1.add(b1);
p1.add(btn);
p1.add(b2);
p1.add(b3);
p.add(p3,BorderLayout.NORTH);
p.add(p1,BorderLayout.CENTER);
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;irow*col;i){
btns[i]=new JButton("");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p,BorderLayout.NORTH);
contentPane.add(p2,BorderLayout.CENTER);
}
public void go(){
setVisible(true);
}
public static void main(String[] args){
new ScanLei1("扫雷").go();
}
public void out(int[][] a,JButton[] btns,ActionEvent e,int i,int x,int y){
int p=1;
if(a[x][y]==0){
a[x][y]=10;
btns[i].setEnabled(false); //33
for(int l=y-1;l=y 1;l){
int m=x-1-1;
int n=l-1;
p=1;
System.out.println(a[1][2]);
if(n-1ncolm-1mrow)
{
for(int q=0;qrowp==1;q){//col--row;
if(((n col*q)=(m*col))((n col*q)(m 1)*col)){
if(a[x-1][l]!=0a[x-1][l]!=10){
btns[n col*q].setText(a[x-1][l] "");
a[x-1][l]=10;
btns[n col*q].setEnabled(false);
}
else if(a[x-1][l]==0){
//a[x-1][l]=10;
btns[n col*q].setEnabled(false);
out(a,btns,e,n col*q,x-1,l); ////55////
a[x-1][l]=10;
btns[n col*q].setEnabled(false);
}
p=0;
}
}
}
p=1;
m=x;
if(n-1ncolm-1mcol)
{
for(int q=0;qrowp==1;q){
if(((n col*q)=(m*col))((n col*q)(m 1)*col)){
if(a[x 1][l]!=0a[x 1][l]!=10){
btns[n col*q].setText(a[x 1][l] "");
a[x 1][l]=10;
btns[n col*q].setEnabled(false);
}
else if(a[x 1][l]==0){
out(a,btns,e,n col*q,x 1,l);///55////
a[x 1][l]=10;
btns[n col*q].setEnabled(false);
}
p=0;
}
}
}
}
int m=x-1;
int n=y-1-1;
p=1;
if(n-1ncolm-1mcol)
{
for(int q=0;qrowp==1;q){
if(((n col*q)=(m*col))((n col*q)(m 1)*col)){
if(a[x][y-1]!=0a[x][y-1]!=10){
btns[n col*q].setText(a[x][y-1] "");
a[x][y-1]=10;
btns[n col*q].setEnabled(false);
}
else if(a[x][y-1]==0){
out(a,btns,e,n col*q,x,y-1);
a[x][y-1]=10;
btns[n col*q].setEnabled(false);
}
p=0;
}
}
}
p=1;
m=x-1;
n=y 1-1;
if(n-1ncolm-1mcol)
{
for(int q=0;qrowp==1;q){
if(((n col*q)=(m*col))((n col*q)(m 1)*col)){
if(a[x][y 1]!=0a[x][y 1]!=10){
btns[n col*q].setText(a[x][y 1] "");
a[x][y 1]=10;
btns[n col*q].setEnabled(false);
}
else if(a[x][y 1]==0){
out(a,btns,e,n col*q,x,y 1);
a[x][y 1]=10;
btns[n col*q].setEnabled(false);
}
p=0;
}
}
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="初级"){
row=9;
col=9;
bon=10;
a1=new int[bon];
b=bon;
//setSize(297,377);
a = new int[row 2][col 2];
this.remove(p2);
timer.stop();
b1.setText("10");
b2.setText("0");
b3.setText("");
btns=new JButton[row*col];
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;irow*col;i){
btns[i]=new JButton("");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p2,BorderLayout.CENTER);
//setSize(297,377);
this.pack();
for(int i=0;irow*col;i){
btns[i].setText(" ");
btns[i].setEnabled(true);
}
for(int i=0;irow 2;i){
for(int j=0;jcol 2;j){
a[i][j]=0;
}
}
}else if(e.getActionCommand()=="中级"){
row=16;
col=16;
bon=40;
//setSize(33*col,33*row 80);
a1=new int[bon];
a = new int[row 2][col 2];
b=bon;
this.remove(p2);
timer.stop();
b1.setText("40");
b2.setText("0");
b3.setText("");
btns=new JButton[row*col];
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;irow*col;i){
btns[i]=new JButton("");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p2,BorderLayout.CENTER);
this.pack();
//setSize(33*col,33*row 80);
for(int i=0;irow*col;i){
btns[i].setText("");
btns[i].setEnabled(true);
}
for(int i=0;irow 2;i){
for(int j=0;jcol 2;j){
a[i][j]=0;
}
}
}else if(e.getActionCommand()=="高级"){
row=16;
col=32;
bon=99;
setSize(33*col,33*row 80);
a1=new int[bon];
a = new int[row 2][col 2];
b=bon;
this.remove(p2);
timer.stop();
b1.setText("99");
b2.setText("0");
b3.setText("");
btns=new JButton[row*col];
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;irow*col;i){
btns[i]=new JButton("");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p2,BorderLayout.CENTER);
//setSize(33*col,33*row 80);
this.pack();
for(int i=0;irow*col;i){
btns[i].setText("");
btns[i].setEnabled(true);
}
for(int i=0;irow 2;i){
for(int j=0;jcol 2;j){
a[i][j]=0;
}
}
}
if(e.getSource()==btn){
timer.start();
b=bon;
b3.setText("");
//System.out.println(bon);
//清空
for(int i=0;irow*col;i){
btns[i].setText("");
btns[i].setEnabled(true);
}
for(int i=0;irow 2;i){
for(int j=0;jcol 2;j){
a[i][j]=0;
}
}
//产生随机数
for(int i=0;ibon;i)
{int p=1;
int m=(int)(Math.random()*row*col);
while(p==1){
int l=1;
int j;
for( j=0;jil==1;j){
if(a1[j]==m){
m=(int)(Math.random()*row*col);
l=0;
}
}
if(j==i){
a1[i]=m;
p=0;
}
}
}
b1.setText(bon "");
b2.setText("0");
//布雷
for(int i=0;ibon;i){
int x=(a1[i]/col 1);
int y=(a1[i]%col 1);
a[x][y]=100;
}
for(int i=0;irow 2;i){
for(int j=0;jcol 2;j){
if(i==0||j==0||i==row 1||j==col 1){
a[i][j]=0;
}
}
}
for(int i=1;i=row;i){
for(int j=1;j=col;j){
if(a[i][j]!=100){
for(int l=j-1;l=j 1;l){
if(a[i-1][l]==100){
a[i][j];
}
if(a[i 1][l]==100){
a[i][j];
}
}
if(a[i][j-1]==100){
a[i][j];
}
if(a[i][j 1]==100){
a[i][j];
}
}
}
}
}
if(e.getSource()==timer)
{
String time=b2.getText().trim();
int t=Integer.parseInt(time);
//System.out.println(t);
if(t=600){
timer.stop();
}else{
t;
b2.setText(t "");
}
}
for(int i=0;icol*row;i){
if(btns[i].getText()!="
推荐阅读
- 怎么测显卡是不是改的,怎么查看显卡有没有被改过
- tcl电视显卡怎么样的简单介绍
- 怎么制作防震减灾ppt,防震减灾ppt 百度网盘
- rbf函数python rbf函数空间插值
- 输入输出java代码,java编程输入
- 厦门沧众跨境电商如何做,厦门沧众跨境电商如何做销售
- 观看虎牙直播的观后感,我想看虎牙直播的
- linux命令自动开机 linux配置开机自动启动进程
- giscad高程分析,arcgis高程分析