多平台统一管理软件接口,如何实现多平台统一管理软件接口
224
2022-12-08
Java实现简单的扫雷小程序
前两天看了个扫雷的视频,于是自己跟着做了下,感觉还不是很难。
初学java的同学可以尝试自己操作下Java小程序
这样子才能提高自己的理解能力和编程水平
不用多说了,直接上代码吧!
具体代码操作如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
public class saolei implements ActionListener {
JFrame frame=new JFrame("扫雷游戏");
JButton reset=new JButton("重来");
Container container=new Container();
//游戏数据结构
final int row=20;
final int col=20;
final int leiCount=30;
JButton [][] buttons=new JButton[row][col];
http://int [][] counts=new int[row][col];
final int LEICODE=10;
// 构造函数
public saolei(){
//1、设置窗口
frame.setSize(900, 800);
frame.setResizable(true);//是否可改变窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//2、添加重来按钮
addResetButton();
//添加按钮
addButtons();
//埋雷
addLei();
//添加雷的计算
calcNeiboLei();
frame.setVisible(true);
}
public void addResetButton(){
reset.setBackground(Color.green);
reset.setOpaque(true);
reset.addActionListener(this);
frame.add(reset,BorderLayout.NORTH);
}
public void addLei(){
Random rand=new Random();
int randRow,randCol;
for(int i=0;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 for(int j=0;j JButton button=new JButton(); button.setBackground(Color.yellow); button.setOpaque(true); button.addActionListener(this); buttons[i][j]=button; container.add(button); } } } public void calcNeiboLei(){ int count; for(int i=0;i for(int j=0;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 for(int j=0;j buttons[i][j].setText(""); buttons[i][j].setEnabled(true); buttons[i][j].setBackground(Color.yellow); counts[i][j]=0; } } addLei(); calcNeiboLei(); }else{ int count=0; for(int i=0;i for(int j=0;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 for(int j=0;j if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return; } } JOptionPane.showMessageDialog(frame, "Yeah,你赢了!"); } 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 for(int j=0;j int count=counts[i][j]; if(count==LEICODE){ buttons[i][j].setText("X"); buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled(false); }else{ buttons[i][j].setText(count+""); buttons[i][j].setEnabled(false); } } } } public static void main(String[] args) { saolei lei=new saolei(); } } 大致具体的代码和结果如下 更多精彩游戏,请参考专题《java经典小游戏》
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 for(int j=0;j JButton button=new JButton(); button.setBackground(Color.yellow); button.setOpaque(true); button.addActionListener(this); buttons[i][j]=button; container.add(button); } } } public void calcNeiboLei(){ int count; for(int i=0;i for(int j=0;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 for(int j=0;j buttons[i][j].setText(""); buttons[i][j].setEnabled(true); buttons[i][j].setBackground(Color.yellow); counts[i][j]=0; } } addLei(); calcNeiboLei(); }else{ int count=0; for(int i=0;i for(int j=0;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 for(int j=0;j if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return; } } JOptionPane.showMessageDialog(frame, "Yeah,你赢了!"); } 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 for(int j=0;j int count=counts[i][j]; if(count==LEICODE){ buttons[i][j].setText("X"); buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled(false); }else{ buttons[i][j].setText(count+""); buttons[i][j].setEnabled(false); } } } } public static void main(String[] args) { saolei lei=new saolei(); } } 大致具体的代码和结果如下 更多精彩游戏,请参考专题《java经典小游戏》
for(int j=0;j
JButton button=new JButton();
button.setBackground(Color.yellow);
button.setOpaque(true);
button.addActionListener(this);
buttons[i][j]=button;
container.add(button);
}
}
}
public void calcNeiboLei(){
int count;
for(int i=0;i for(int j=0;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 for(int j=0;j buttons[i][j].setText(""); buttons[i][j].setEnabled(true); buttons[i][j].setBackground(Color.yellow); counts[i][j]=0; } } addLei(); calcNeiboLei(); }else{ int count=0; for(int i=0;i for(int j=0;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 for(int j=0;j if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return; } } JOptionPane.showMessageDialog(frame, "Yeah,你赢了!"); } 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 for(int j=0;j int count=counts[i][j]; if(count==LEICODE){ buttons[i][j].setText("X"); buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled(false); }else{ buttons[i][j].setText(count+""); buttons[i][j].setEnabled(false); } } } } public static void main(String[] args) { saolei lei=new saolei(); } } 大致具体的代码和结果如下 更多精彩游戏,请参考专题《java经典小游戏》
for(int j=0;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 for(int j=0;j buttons[i][j].setText(""); buttons[i][j].setEnabled(true); buttons[i][j].setBackground(Color.yellow); counts[i][j]=0; } } addLei(); calcNeiboLei(); }else{ int count=0; for(int i=0;i for(int j=0;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 for(int j=0;j if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return; } } JOptionPane.showMessageDialog(frame, "Yeah,你赢了!"); } 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 for(int j=0;j int count=counts[i][j]; if(count==LEICODE){ buttons[i][j].setText("X"); buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled(false); }else{ buttons[i][j].setText(count+""); buttons[i][j].setEnabled(false); } } } } public static void main(String[] args) { saolei lei=new saolei(); } } 大致具体的代码和结果如下 更多精彩游戏,请参考专题《java经典小游戏》
for(int j=0;j
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
buttons[i][j].setBackground(Color.yellow);
counts[i][j]=0;
}
}
addLei();
calcNeiboLei();
}else{
int count=0;
for(int i=0;i for(int j=0;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 for(int j=0;j if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return; } } JOptionPane.showMessageDialog(frame, "Yeah,你赢了!"); } 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 for(int j=0;j int count=counts[i][j]; if(count==LEICODE){ buttons[i][j].setText("X"); buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled(false); }else{ buttons[i][j].setText(count+""); buttons[i][j].setEnabled(false); } } } } public static void main(String[] args) { saolei lei=new saolei(); } } 大致具体的代码和结果如下 更多精彩游戏,请参考专题《java经典小游戏》
for(int j=0;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 for(int j=0;j if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return; } } JOptionPane.showMessageDialog(frame, "Yeah,你赢了!"); } 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 for(int j=0;j int count=counts[i][j]; if(count==LEICODE){ buttons[i][j].setText("X"); buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled(false); }else{ buttons[i][j].setText(count+""); buttons[i][j].setEnabled(false); } } } } public static void main(String[] args) { saolei lei=new saolei(); } } 大致具体的代码和结果如下 更多精彩游戏,请参考专题《java经典小游戏》
for(int j=0;j
if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return;
}
}
JOptionPane.showMessageDialog(frame, "Yeah,你赢了!");
}
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 for(int j=0;j int count=counts[i][j]; if(count==LEICODE){ buttons[i][j].setText("X"); buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled(false); }else{ buttons[i][j].setText(count+""); buttons[i][j].setEnabled(false); } } } } public static void main(String[] args) { saolei lei=new saolei(); } } 大致具体的代码和结果如下 更多精彩游戏,请参考专题《java经典小游戏》
for(int j=0;j
int count=counts[i][j];
if(count==LEICODE){
buttons[i][j].setText("X");
buttons[i][j].setBackground(Color.red);
buttons[i][j].setEnabled(false);
}else{
buttons[i][j].setText(count+"");
buttons[i][j].setEnabled(false);
}
}
}
}
public static void main(String[] args) {
saolei lei=new saolei();
}
}
大致具体的代码和结果如下
更多精彩游戏,请参考专题《java经典小游戏》
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~