java游戏之扫雷代码 java 扫雷代码

JAVA , 编一个游戏小游戏 , 比如扫雷,这个程序大概的代码是什么 , 谁能教教我?我比较笨,但是我认学 。import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main
{
public static void main(String[] argus)
{
Landmine Zhang = new Landmine();
}
}
//
//Landmine类主界面
class Landmine extends JFrame
{
static Resources resources = new Resources();
Playinglistener = new Playing(this);//主要监听者java游戏之扫雷代码,监听地雷面板java游戏之扫雷代码的动作
Help helpListener = new Help(this);//辅助监听者 , 监听“帮助”、“关于”
JPanel landminePanel = new JPanel();//创建地雷面板
JPanel topPanel = new JPanel();//创建顶部面板
JPanel lowerPanel = new JPanel();//创建底部面板
public static MyButton [][] lei;//主区按钮组
public static int numberOfUnflaged ;//剩余java游戏之扫雷代码的雷数 , 显示在topPanel上,用于提示用户
public static int numberOfClicked;//已经翻开的格子数,当数字数字到"总格子数—雷数"时,即胜利
public static int usedTime;//已用时间
public static JLabel numberOfUnflagedLabel = new JLabel(); //创建剩雷数标签
public static JLabel timeLabel = new JLabel();//创建时间标签
public static Timer timer;//创建计时
Keylistener keyListener = new Keylistener(this);
public Landmine()
{
super("扫雷__1.2版__小老头");//标题
setBounds(300,90,800,800);//设置窗口位置和大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//最大化、最小化、关闭按钮
BorderLayout ff = new BorderLayout();//创建布局管理器
setLayout(ff);//关联布局管理器
setResizable(false);//禁止改变窗口大小
/*初始化一些数据*/
numberOfClicked = 0;
numberOfUnflaged = 40;
usedTime = 0;
/*设置顶部面板*/
numberOfUnflagedLabel.setText("剩余雷数:" numberOfUnflaged);//显示剩余雷数
numberOfUnflagedLabel.setFont(resources.fontOne);//设置剩雷数标签字体
numberOfUnflagedLabel.setIcon(resources.bombIconForLabel);//剩雷数标签图标(地雷形)
topPanel.add(numberOfUnflagedLabel);//剩雷数标签加入topPanel
timeLabel.setText("用时:"usedTime); //显示剩余时间
timeLabel.setFont(resources.fontOne);//设置时间标签字体
timeLabel.setIcon(resources.clockIcon); //设置时间标签图标
topPanel.add(timeLabel);//时间标签加入topPanel
add(topPanel,BorderLayout.NORTH);//加入主面板上部
timer = new Timer(1000,new TimerListener());//计算器注册监听者
/*设置底部面板*/
JButton aboutJB = new JButton("关于"); //创建“关于”按钮
JButton helpJB = new JButton("求救");//创建“求救”按钮
helpJB.addActionListener(helpListener); //"求救"按钮加入监听者
aboutJB.addActionListener(helpListener);//"关于"按钮加入监听者
helpJB.addKeyListener(keyListener);
aboutJB.addKeyListener(keyListener);//注册按键监听
lowerPanel.add(aboutJB);//“关于”按钮加入lowerPanel
lowerPanel.add(helpJB);//“帮助”按钮加入lowerPanel
add(lowerPanel,BorderLayout.SOUTH);
/*设置地雷面板*/
GridLayout dd = new GridLayout(16,16);
landminePanel.setLayout(dd);//布局管理
lei = new MyButton[18][18];
for(int i=0; i18;i)
{//创建下标0—17的按钮,18*18矩阵
for(int j=0; j18;j)
{
lei[i][j] = new MyButton(i,j);
}
}
for(int i=1; i17;i)
{//将下标1-16的按钮,加入面板、设置图标、翻开标记为假、加入监听者
for(int j=1; j17;j)
{
landminePanel.add(lei[i][j]);//按钮加入地雷面板
lei[i][j].setIcon(resources.smallIcon); //设置按钮图标
lei[i][j].isClicked = false;//翻开标记设置为 假lei[i][j].setIcon(dead);
lei[i][j].addActionListener(listener);//加入监听者
lei[i][j].addMouseListener(listener);//加入鼠标事件监听者
lei[i][j].addKeyListener(keyListener);//按钮注册按键监听,当焦点在按钮上是能监听按键
}
}
add(landminePanel,BorderLayout.CENTER);//landminePanel加入主框架中央
addLandmine();//布雷
timer.start();//启动计时器
setVisible(true);//显示之
}
/*布雷*/
public static void addLandmine()
{//随机将40的按钮的是否为雷的标记isBomb设为真
for(int count = 0; count40; /*blank*/)
{
int i = (int)(Math.random()*100 % 161 ) ;
int j = (int)(Math.random()*100 % 161 ) ;
if(lei[i][j].isBomb == false)
{
lei[i][j].isBomb = true;
count;
}
}
}
class TimerListener implements ActionListener
{//内部类,时间监听
public void actionPerformed(ActionEvent e)
{
usedTime;
timeLabel.setText("用时:"usedTime);
}
}
}
//
//Playing类 执行主要游戏操作
class Playing implements ActionListener,MouseListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Playing(Landmine in )
{
gui = in;
}
public void actionPerformed(ActionEvent event)
{
MyButton receive = (MyButton)event.getSource();
if(receive.isBomb)
{//如果翻到了雷 。。
for(int i=1; i17;i)
{//将所有的雷图标设为 “地雷”
for(int j=1; j17;j)
{
if(gui.lei[i][j].isBomb)
gui.lei[i][j].setIcon(resources.bombIcon);
}
}
receive.setIcon(resources.deadIcon);//将踩到的地雷图标设为 “衰”
gui.timer.stop();//停止计时器
JOptionPane.showMessageDialog(null,"小朋友,你挂了…","失败!",
JOptionPane.INFORMATION_MESSAGE,
resources.deadIcon);//提示失败
int yourChose = JOptionPane.showConfirmDialog(null,"你可能是一不小心点错了,再来一局?" );
if(yourChose == JOptionPane.OK_OPTION)
{//点击“是”时
replay();
}
else
{//点击 “否” 或 “取消” 时退出程序
System.exit(0);
}
}
else if(receive.isClicked ==false)
{//未翻到雷
showBombNumber(receive);
}
}
public static void showBombNumber(MyButton in)
{//翻开点击的按钮
int numberOfLandmine = 0;//记录雷的个数
in.isClicked = true;//翻开标记设为真
/*检测周围8个方块是否为雷*/
if(gui.lei[in.num_x-1][in.num_y-1].isBomb == true) numberOfLandmine;//左上
if(gui.lei[in.num_x][in.num_y-1].isBomb == true) numberOfLandmine; //上
if(gui.lei[in.num_x 1][in.num_y-1].isBomb == true) numberOfLandmine;//右上
if(gui.lei[in.num_x 1][in.num_y].isBomb == true) numberOfLandmine; //右
if(gui.lei[in.num_x 1][in.num_y 1].isBomb == true) numberOfLandmine;//右下
if(gui.lei[in.num_x][in.num_y 1].isBomb == true) numberOfLandmine; //下
if(gui.lei[in.num_x-1][in.num_y 1].isBomb == true) numberOfLandmine;//左下
if(gui.lei[in.num_x-1][in.num_y].isBomb == true) numberOfLandmine; //左
in.setIcon(new ImageIcon("images/" numberOfLandmine ".png"));//根据周围的雷数显示数字图标
gui.numberOfClicked;//翻开格子数 1
if(gui.numberOfClicked==216)
{//翻开216个格子时游戏成功,用户选择是否再来一局
int yourChoice = JOptionPane.showConfirmDialog(null,"恭喜你成功了!再来一盘吗?");
if(yourChoice == JOptionPane.OK_OPTION)
replay();
else
System.exit(0);
}
if(numberOfLandmine==0)
{//如果周围无雷,则将周围未翻开格子的全部翻开
if(gui.lei[in.num_x-1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y-1]);
if(gui.lei[in.num_x][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y-1]);
if(gui.lei[in.num_x 1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x 1][in.num_y-1]);
if(gui.lei[in.num_x 1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x 1][in.num_y]);
if(gui.lei[in.num_x 1][in.num_y 1].isClicked == false)
showBombNumber(gui.lei[in.num_x 1][in.num_y 1]);
if(gui.lei[in.num_x][in.num_y 1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y 1]);
if(gui.lei[in.num_x-1][in.num_y 1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y 1]);
if(gui.lei[in.num_x-1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y]);
}
}
public static void replay()
{//重新开始
gui.dispose();//释放框架资源
gui.timer.stop();//终止计时器
Landmine ff = new Landmine();//重新创建一个主类的实例
//这几条语句实现了重新开始————关闭上一个窗口,重新开启一个
//但是这种方法会造成内存的浪费,一个改进的方法是不关闭当年窗口 , 而是将当前窗口重新初始化
}
public void mousePressed(MouseEvent e)
{//当鼠标右键点击时自动调用此函数
int mods = e.getModifiers();
MyButton receive = (MyButton)e.getSource();
if((modsInputEvent.BUTTON3_MASK) != 0)
{//鼠标右键
if(receive.isClicked == false)
{
receive.isRight = receive.isRight ? false : true;//改变receive.isRight的值
if(receive.isRight)
{//如果添加标记 , 则剩余雷数-1,设置标签为“旗帜”
gui.numberOfUnflaged--;
receive.setIcon(resources.flagIcon);
}
else
{//如果清除标记,则剩余雷数 1,设置标签为“未翻开”
gui.numberOfUnflaged;
receive.setIcon(resources.smallIcon);
}
gui.numberOfUnflagedLabel.setText("剩余雷数:" gui.numberOfUnflaged);
//更新剩余雷数标签
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
}
//
//Help类,响应“关于”、“求救”
class Help implements ActionListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Help(Landmine in)
{
gui = in ;
}
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand()=="关于")
JOptionPane.showMessageDialog(null,"扫雷1.2版 。。小老头出品");
if(event.getActionCommand()=="求救")
help();
}
public static void help()
{//求救
int stopNumber = (int)(Math.random() * gui.numberOfUnflaged1 );
int count = 0;
for(int i=1; i17;i )
{
for(int j=1; j17;j)
{
if( gui.lei[i][j].isBomb!gui.lei[i][j].isClicked!gui.lei[i][j].isRight )
{
count;
}
if(count == stopNumber)
{
gui.lei[i][j].setIcon(resources.badIcon);
return;
}
}
}
}
}
//
//Keylistener类 , 响应键盘事件
class Keylistener implements KeyListener
{
static Resources resources = new Resources();
Landmine gui;
public Keylistener(Landmine in)
{
gui = in;
}
public void keyPressed(KeyEvent e)
{//有键按下时自动执行该方法
if(e.getKeyCode() == KeyEvent.VK_UP)
{//按键为 向上 时,将所有未标记的地雷显示出
for(int i=1; i17;i)
{
for(int j=1; j17;j)
{
if(gui.lei[i][j].isBomb!gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.badIcon);
}
}
}
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{//按键为 向下 时,将所有未标记的地雷恢复为未点击的图标
for(int i=1; i17;i)
{
for(int j=1; j17;j)
{
if(gui.lei[i][j].isBomb!gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.smallIcon);
}
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
//
//按钮类 MyBtton
class MyButton extends JButton
{
public int num_x,num_y;//第几号方块
public boolean isBomb;//是否为雷
public boolean isClicked;//是否被点击
public int BombFlag;//探雷标记
public boolean isRight;//是否点击右键
public MyButton(int x, int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
isBomb = false;
isClicked = true;
isRight = false;
}
}
//
//资源类 其java游戏之扫雷代码他类中用到的图标,字体等
class Resources
{
public static ImageIcon deadIcon;
public static ImageIcon smallIcon;
public static ImageIcon clockIcon;
public static ImageIcon bombIcon;
public static ImageIcon flagIcon;
public static ImageIcon badIcon;
public static ImageIcon bombIconForLabel;
public static Font fontOne;
public Resources()
{
deadIcon = new ImageIcon("images/dead.gif");
smallIcon= new ImageIcon("images/smallIcon.png");
clockIcon = new ImageIcon("images/clock2.png");
bombIcon = new ImageIcon("images/bomb.png");
flagIcon = new ImageIcon("images/flag_2.png");
badIcon = new ImageIcon("images/bad.gif");
bombIconForLabel = new ImageIcon("images/bombForLabel.gif");
fontOne = new Font("null",Font.BOLD,20);
}
}
悬赏100元钱 。注释一个java扫雷游戏源代码,就是把每行的意思写到后面就可以了!在线等 。import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
class Min extends JPanel//雷的类
{
//备注:鼠标的左键 = 1;右键 = 3;中键 = 2
private int flag = 0,statu = 0; //定义雷的属性 0:没有打开 1:打开 2:标示为雷 3:不确定
//flag = 0 不是雷; flag = 1是雷
private int but,count = 0; //but:哪一个鼠标键被按下去了count:这个区域周围有多少个雷
private int mx = 0,my = 0,mw = 10; //定义雷的坐标和宽度
public Min()//构造函数
{
statu = 0;
}
public Min(int f,int x,int y,int w)
//构造函数
{
flag = f;
mx= x;
my= y;
mw= w;
}
public int getFlag(){return flag;}
public int getStatu(){return statu;}
public int getMx(){return mx;}
public int getMy(){return my;}
public int getMw(){return mw;}
public int getCount(){return count;}
public void setFlag(int f){flag = f;}
public void setCount(int c){count = c;}
public void setData(int f,int x,int y,int w,int s)
//传递值
{
flag = f;
mx= (x-1)*w;
my= (y-1)*w;
mw= w-1;
statu = s;
}
//根据你点击鼠标的不同来改变雷的属性
public int sendKey(int key)
{
//返回值,如果游戏结束则返回-1
int rtn = 1;
if(key == 3)
{
switch(statu)
{
case 1:
break;
case 2:
statu = 3;
break;
case 3:
statu = 0;
break;
case 0:
statu = 2;
break;
}
rtn = 1;
}
if(key == 1statu == 0)
{
switch(flag)
{
case 0:
statu = 1;
rtn = 2;
break;
case 1:
statu = 1;
rtn = -1;
break;
}
}
return rtn;
}
}
class DrawPanel extends JPanel
{
private int i,j;
private int f = 0;//if f = 1 then game over ,if f =2 then win
private int chx = 0,chy = 0; //专门记录坐标x,y的值
private int msum = 6,ksum = 0; //msum:雷的个数,ksum:标示雷的个数
private int bx = 10,by = 10,bw = 40; //bx,by:棋盘的大?。?bw:棋子的大小
public Min board[][] = {
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
};
// 画坐标为ax,ay区域的雷的状态
public void draw(Graphics g,int ax,int ay)
{
int x,y,w; // 坐标x,y;和宽度:w
int s,c,flag; //状态;雷的个数;
int cx = bw/2 - 4;
int cy = bw/24;
x = board[ax][ay].getMx();
y = board[ax][ay].getMy();
w = board[ax][ay].getMw();
s = board[ax][ay].getStatu();
c = board[ax][ay].getCount();
flag= board[ax][ay].getFlag();
switch(s)
{
case 0: //没有打开状态
{
g.setColor(Color.black);
g.fillRect(x,y,w,w);
break;
}
case 1: //打开状态
{
g.setColor(Color.blue);
g.fillRect(x,y,w,w);
if(c != 0flag == 0) //此处没有雷
{
g.setColor(Color.red);
g.drawString(String.valueOf(c),xcx,ycy);
}
if(flag == 1) //此处有雷
{
g.setColor(Color.red);
g.fillRect(x,y,w,w);
g.setColor(Color.blue);
g.drawString(" 雷",xcx,ycy);
}
break;
}
case 2: //标雷状态
{
g.setColor(Color.green);
g.fillRect(x,y,w,w);
g.setColor(Color.blue);
g.drawString(" 旗",xcx,ycy);
break;
}
case 3: //不确定状态
{
g.setColor(Color.black);
g.fillRect(x,y,w,w);
g.setColor(Color.red);
g.drawString("?",xcx,ycy);
break;
}
default:
break;
}
}
// 没有图形器的绘图函数:画出坐标ax,ay的雷的状态和图形
public void draw(int ax,int ay)
{
Graphics g;
g = this.getGraphics();
draw(g,ax,ay);
}
//打开周围没有雷的地方,并且绘画所在区域点击左键触发
public int openNoMin(int ax,int ay)
{
int i,j;
if(ax1||ay1||axbx||ayby) return 0;//鼠标点击的区域出界了
if(board[ax][ay].getStatu() != 0) return 0;//如果此区域打开了 , 返回
board[ax][ay].sendKey(1); //如果返回值等于-1 , 就说明游戏结束
draw(ax,ay);
if(board[ax][ay].getFlag() == 1)
//如果游戏结束,把所有的雷都显示出来
{
for(i = 1;i=bx;i)
{
for(j = 1;j = by;j)
{
if(board[i][j].getFlag() == 1)
{
board[i][j].sendKey(1);
draw(i,j);
}
}
}
return -1;
}
//如果游戏没有结束
if(board[ax][ay].getCount()0)
{
ksum;
return 1; //周围有雷,就不用打开周围地区
}
if(board[ax][ay].getCount() == 0board[ax][ay].getFlag() == 0)
//周围没有雷 , 打开周围地区,直到有雷的地区
{
openNoMin(ax-1,ay-1);openNoMin(ax,ay-1);openNoMin(ax 1,ay-1);
openNoMin(ax-1,ay);openNoMin(ax 1,ay);
openNoMin(ax-1,ay 1);openNoMin(ax,ay 1);openNoMin(ax 1,ay 1);
}
ksum;
return 1;
}
//计算坐标x,y的周围雷的个数
public int getCount(int ai,int aj)
{
int sum = 0;
if(board[ai][aj].getFlag() == 1)
{
return sum;
}
if(ai1aj1aibxajby)
{
sum =board[ai-1][aj-1].getFlag()board[ai][aj-1].getFlag()board[ai 1][aj-1].getFlag()
board[ai-1][aj].getFlag()board[ai 1][aj].getFlag()
board[ai-1][aj 1].getFlag()board[ai][aj 1].getFlag()board[ai 1][aj 1].getFlag();
}
if(ai==1aj==1)
{
sum =board[ai 1][aj].getFlag()
board[ai][aj 1].getFlag()board[ai 1][aj 1].getFlag();
}
if(ai==1aj==by)
{
sum =board[ai][aj-1].getFlag()board[ai 1][aj-1].getFlag()
board[ai 1][aj].getFlag();
}
if(ai==bxaj==1)
{
sum =board[ai-1][aj].getFlag()
board[ai-1][aj 1].getFlag()board[ai][aj 1].getFlag();
}
if(ai==bxaj==by)
{
sum =board[ai-1][aj-1].getFlag()board[ai][aj-1].getFlag()
board[ai-1][aj].getFlag();
}
if(ai==1aj1ajby)
{
sum =board[ai][aj-1].getFlag()board[ai 1][aj-1].getFlag()
board[ai 1][aj].getFlag()
board[ai][aj 1].getFlag()board[ai 1][aj 1].getFlag();
}
if(ai==bxaj1ajby)
{
sum =board[ai-1][aj-1].getFlag()board[ai][aj-1].getFlag()
board[ai-1][aj].getFlag()
board[ai-1][aj 1].getFlag()board[ai][aj 1].getFlag();
}
if(ai1aibxaj==1)
{
sum =board[ai-1][aj].getFlag()board[ai 1][aj].getFlag()
board[ai-1][aj 1].getFlag()board[ai][aj 1].getFlag()board[ai 1][aj 1].getFlag();
}
if(ai1aibxaj==by)
{
sum =board[ai-1][aj-1].getFlag()board[ai][aj-1].getFlag()board[ai 1][aj-1].getFlag()
board[ai-1][aj].getFlag()board[ai 1][aj].getFlag();
}
return sum;
}
// 传入参数:几列,几行 , 宽度,雷数
public void initMin(int ax,int ay,int aw,int as)
{
int k = 1;//表明产生的第几个雷
Random r;//随机数
f = 0;//f=0表示游戏还没有结束
ksum = 0;
bx = ax;
by = ay;
bw = aw;
msum = as;
r = new Random();
//初始化底盘的值
for(i = 1;i = bx;i)
{
for(j=1;j=by;j)
{
board[i][j].setData(0,i,j,bw,0);
}
}
// 随机产生雷
while(k = msum)
{
i = r.nextInt(bx) 1;
j = r.nextInt(by) 1;
if(board[i][j].getFlag() != 1)
{
board[i][j].setFlag(1);
k;
}
}
// 非雷区的周围有几个雷,初始化其值
for(i = 1;i = bx;i)
{
for(j=1;j=by;j)
{
board[i][j].setCount(getCount(i,j));
}
}
setBackground(Color.white);
repaint();
}
// 构造函数
public DrawPanel(int ax,int ay,int aw,int as)
{
initMin(ax,ay,aw,as);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
int r;
if(f != 0) return;//如果游戏结束,返回
chx= me.getX();
chy= me.getY();
if(me.getButton() != 1)
{
board[chx/bw 1][chy/bw 1].sendKey(me.getButton());
draw(chx/bw 1,chy/bw 1);
}
else if(me.getButton() == 1)
{
if(openNoMin(chx/bw 1,chy/bw 1) == -1)
{
f = 1;
repaint();
}
else if ( ksummsum == bx*by )
{
f = 2;
repaint();
}
}
}
}
);
}
// 重画所有的图形,包括一些修饰的图形
public void paint(Graphics g)
{
int x,y,w;
int s;
int cx = bw/2 - 4;
int cy = bw/24;
g.clearRect(0,0,600,600);
for(i=1;i=bx;i)
{
for(j=1;j=by;j)
{
draw(g,i,j);
}
}
if(f == 1)
{
Font f = new Font("11",1,70);
Font fo = g.getFont();
g.setColor(Color.white);
g.setFont(f);
//g.setSize();
g.drawString("Game Over",0,200);
g.setFont(fo);
}
if( f == 2 )
{
Font f = new Font("11",1,70);
Font fo = g.getFont();
g.setColor(Color.white);
g.setFont(f);
//g.setSize();
g.drawString("You win!",0,200);
g.setFont(fo);
}
}
};
// 主类和程序的入口
public class Mine extends JFrame implements ActionListener
{
Containercp = getContentPane();
JButtonbt = new JButton("开局");
Label l1 = new Label("列:");
Label l2 = new Label("行:");
Label l3 = new Label("宽度:");
Label l4 = new Label("雷的个数:");
TextField tf1 = new TextField("10",2); //列
TextField tf2 = new TextField("10",2); //行
TextField tf3 = new TextField("40",2); //宽度
TextField tf4 = new TextField("15",2); //雷的个数
int x=10,y=10,w=40,sum=15;
DrawPaneldp = new DrawPanel(x,y,w,sum);
public Mine()
{
setBackground(Color.white);
cp.setLayout(null);
cp.add(dp);
cp.add(bt);
cp.add(tf1);
cp.add(tf2);
cp.add(tf3);
cp.add(tf4);
cp.add(l1);
cp.add(l2);
cp.add(l3);
cp.add(l4);
l1.setBounds(20 ,10,20,20);
tf1.setBounds(40,10,20,20);
l2.setBounds(70,10,20,20);
tf2.setBounds(90,10,20,20);
l3.setBounds(120,10,40,20);
tf3.setBounds(160,10,20,20);
l4.setBounds(190,10,60,20);
tf4.setBounds(250,10,20,20);
bt.setBounds(300,10,80,20);
dp.setBounds(20,40,x*w,y*w);
setResizable(false);
setSize(x*w 40,y*w 80);
setTitle(" 扫雷");
show();
bt.addActionListener(this);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == bt)
{
//x = Integer.parseInt(tf1.getText());
//y = Integer.parseInt(tf2.getText());
//w = Integer.parseInt(tf3.getText());
sum = Integer.parseInt(tf4.getText());
setSize(x*w 40,y*w 80);
dp.setBounds(20,40,x*w,y*w);
show();
dp.initMin(x,y,w,sum);
}
}
public static void main(String args[])
{
new Mine();
}
};
求一个java扫雷游戏的程序源代码,尽量多点注释,要确实可用的!急急急急急急急急急急急急?。。。。?/h2>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;
}
}
}
【java游戏之扫雷代码 java 扫雷代码】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()!="

    推荐阅读