{
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 ( ksum + msum == bx*by )
{
f = 2;
repaint();
}
}
}
}
);
}
// 重画所有的图形 , 包括一些修饰的图形
public void paint(Graphics g)
{
int x,y,w;
int s;
int cx = bw/2 - 4;
int cy = bw/2 + 4;
推荐阅读
- java代码实现卷积计算,java 卷积
- 外资招聘gis,外资招聘要求
- 你用什么显卡用英语怎么说,你用的是什么配置
- 霞湖世家直播卖货最高记录,霞湖世家给谁代工
- php添加数组数据 php数组加法
- 临床医疗的区块链技术,医疗区块链应用
- 门店导航小程序源码码,店铺导航怎么写
- vb.net自动填写表单 vba 自动填写数据
- flutter国内,flutter国内案例