java实现简单窗口小游戏“扫雷,记得把每一次面试当做经验积累

reset.addActionListener(this);
frame.add(reset,BorderLayout.NORTH);

}
public void addLei(){
Random rand=new Random();
int randRow,randCol;
for(int i=0;i<leiCount;i++){
randRow=rand.nextInt(row);
randCol=rand.nextInt(col);
if(counts[randRow][randCol]== LEICODE){
i–;
}else{
counts[randRow][randCol]=LEICODE;
//buttons[randRow][randCol].setText("*");

	}
}

}
public void addButtons(){
frame.add(container,BorderLayout.CENTER);
container.setLayout(new GridLayout(row,col));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
JButton button=new JButton();
button.setBackground(Color.orange);
button.setOpaque(true);
button.addActionListener(this);
buttons[i][j]=button;
container.add(button);
}
}
}
public void calcNeiboLei(){
int count;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
count=0;
if(counts[i][j]==LEICODE) continue;

		if(i>0 && j>0 && counts[i-1][j-1]==LEICODE) count++;
		if(i>0&&counts[i-1][j]==LEICODE) count++;
		if(i>0 && j<19 && counts[i-1][j+1]==LEICODE) count++;
		if(j>0 && counts[i][j-1]==LEICODE) count++;
		if(j<19 && counts[i][j+1]==LEICODE) count++;
		if(i<19&&j>0&&counts[i+1][j-1]==LEICODE) count++;
		if(i<19&&counts[i+1][j]==LEICODE) count++;
		if(i<19&&j<19&&counts[i+1][j+1]==LEICODE) count++;
		
		counts[i][j]=count;
		//buttons[i][j].setText(counts[i][j]+"");
	}
}

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton button=(JButton)e.getSource();
if(button.equals(reset)){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
buttons[i][j].setBackground(Color.orange);
counts[i][j]=0;
}
}
addLei();
calcNeiboLei();
}else{
int count=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(button.equals(buttons[i][j])){
count=counts[i][j];
if(count==LEICODE){
LoseGame();
}else{
openCell(i,j);
checkWin();
} return;
}
}
}

}

}
void checkWin(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return;
}
}
JOptionPane.showMessageDialog(frame, “握草牛啤,你竟然赢了!”);
}
void openCell(int i,int j){
if(buttons[i][j].isEnabled()==false) return;

buttons[i][j].setEnabled(false); 
if(counts[i][j]==0){
	if(i>0 && j>0 && counts[i-1][j-1]!=LEICODE) openCell(i-1, j-1);
	if(i>0&&counts[i-1][j]!=LEICODE) openCell(i-1, j);
	if(i>0 && j<19 && counts[i-1][j+1]!=LEICODE) openCell(i-1, j+1);
	if(j>0 && counts[i][j-1]!=LEICODE) openCell(i, j-1);
	if(j<19 && counts[i][j+1]!=LEICODE) openCell(i, j+1);
	if(i<19&&j>0&&counts[i+1][j-1]!=LEICODE) openCell(i+1, j-1);
	if(i<19&&counts[i+1][j]!=LEICODE) openCell(i+1, j);
	if(i<19&&j<19&&counts[i+1][j+1]!=LEICODE) openCell(i+1, j+1);
	
	buttons[i][j].setText(counts[i][j]+"");
}else{
	buttons[i][j].setText(counts[i][j]+"");
}

}
void LoseGame(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
int count=counts[i][j];
if(count==LEICODE){
buttons[i][j].setText(“×”);
buttons[i][j].setBackground(Color.red);
buttons[i][j].setEnabled(false);
}else{
buttons[i][j].setText(count+"");

最后

码字不易,觉得有帮助的可以帮忙点个赞,让更多有需要的人看到

又是一年求职季,在这里,我为各位准备了一套Java程序员精选高频面试笔试真题,来帮助大家攻下BAT的offer,题目范围从初级的Java基础到高级的分布式架构等等一系列的面试题和答案,用于给大家作为参考,需要的可以戳这里免费领取,以下是部分内容截图
试真题,来帮助大家攻下BAT的offer,题目范围从初级的Java基础到高级的分布式架构等等一系列的面试题和答案,用于给大家作为参考,需要的可以戳这里免费领取,以下是部分内容截图
架构面试专题及架构学习笔记导图.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值