[Java] 简单JavaSE游戏----扫雷 →→→→→进入此内容的聊天室

来自 , 2020-08-24, 写在 Java, 查看 167 次.
URL http://www.code666.cn/view/c1e39d91
  1. //根据中软国际所给教材编写
  2. //时间限制未实现
  3. package fengke.game.test;
  4.  
  5. import java.awt.BorderLayout;
  6. import java.awt.Color;
  7. import java.awt.Container;
  8. import java.awt.Font;
  9. import java.awt.GridLayout;
  10. import java.awt.Insets;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.MouseAdapter;
  14. import java.awt.event.MouseEvent;
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JMenu;
  19. import javax.swing.JMenuBar;
  20. import javax.swing.JMenuItem;
  21. import javax.swing.JPanel;
  22. import javax.swing.Timer;
  23.  
  24.  
  25. public class Mine implements ActionListener {
  26.        
  27.         // 声明所需组件
  28.         private JFrame frame;
  29.         private Container contentPane;//视图
  30.         private JPanel menuPanel, timePanel, gamePanel;//菜单,时间,游戏三个部分
  31.         private JLabel timeLabel, resutLabel, mineCountLabel;//时间标签,状态标签,地雷数量
  32.         private JMenuItem menuItem1, menuItem2, menuItem3;//游戏难度选择
  33.  
  34.         private JButton[][] buttons;
  35.         private int[][] buttonsValue;
  36.         private boolean[][] buttonsFlag;
  37.  
  38.         private int timeLength = 0;
  39.         private int row, col;
  40.         private int mineCount = 10;
  41.        
  42.         private int mineRealCount=10;
  43.         private boolean winGame=false;
  44.         //计时器
  45.         private Timer timer;
  46.         private int gameStatus=0;
  47.        
  48.         // 构造方法
  49.                 public Mine() {
  50.  
  51.                         frame = new JFrame("锋客扫雷游戏");
  52.                         contentPane = frame.getContentPane();
  53.  
  54.                         menuPanel = new JPanel();
  55.                         timePanel = new JPanel();
  56.                         gamePanel = new JPanel();
  57.  
  58.                         timeLabel = new JLabel("游戏时间:" + new Integer(timeLength).toString()
  59.                                         + "秒");
  60.                         resutLabel = new JLabel("   状态:准备游戏");
  61.                         mineCountLabel = new JLabel("地雷个数:" + mineCount);
  62.                        
  63.                         timer=new Timer(1000,new TimerActionListener());
  64.                 }
  65.         //初始化游戏界面
  66.                 public void initGame() {
  67.                         JMenuBar menuBar = new JMenuBar();
  68.                         JMenu menu = new JMenu("游戏设置");
  69.                         menuItem1 = new JMenuItem("初级");
  70.                         menuItem2 = new JMenuItem("中级");
  71.                         menuItem3 = new JMenuItem("高级");
  72.  
  73.                         menuBar.add(menu);
  74.                         menu.add(menuItem1);
  75.                         menu.add(menuItem2);
  76.                         menu.add(menuItem3);
  77.                         menuPanel.add(menuBar);
  78.  
  79.                         timePanel.add(timeLabel);
  80.                         timePanel.add(mineCountLabel);
  81.                         timePanel.add(resutLabel);
  82.  
  83.  
  84.  
  85.                         JPanel panel = new JPanel();
  86.                         panel.setLayout(new BorderLayout());
  87.                         panel.add(menuPanel, BorderLayout.NORTH);
  88.                         panel.add(timePanel, BorderLayout.SOUTH);
  89.                         contentPane.add(panel, BorderLayout.NORTH);
  90.  
  91.                         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  92.                         frame.pack();
  93.                         frame.setSize(297, 377);
  94.                         frame.setBounds(400, 100, 400, 500);
  95.                         frame.setVisible(true);
  96.                         //选择难度
  97.                         menuItem1.addActionListener(this);
  98.                         menuItem2.addActionListener(this);
  99.                         menuItem3.addActionListener(this);
  100.                
  101.                 }
  102.                     //监听
  103.                         public void actionPerformed(ActionEvent e) {
  104.                                 if(e.getSource()==this.menuItem1){
  105.                                         row = 9;
  106.                                         col = 9;
  107.                                         mineRealCount=10;
  108.                                         mineCountLabel.setText("地雷个数:" + mineRealCount);
  109.                                         resutLabel.setText("   状态:准备游戏");
  110.                                         //设置埋雷区域
  111.                                         gamePanel.removeAll();
  112.                                         initButtonsAllValues();
  113.                                         gamePanel.setLayout(new GridLayout(row, col, 0, 0));
  114.                                         for (int i = 1; i <= row; i++) {
  115.                                                 for (int j = 1; j <= col; j++) {
  116.                                                         gamePanel.add(buttons[i][j]);
  117.                                                 }
  118.                                         }
  119.                                         contentPane.add(gamePanel, BorderLayout.CENTER);
  120.                                         // 设置地雷
  121.                                         timeLength=0;
  122.                                         setMines(mineRealCount);
  123.                                         setButtonValue();
  124.                                         addListener();
  125.                                 }
  126.                                 if(e.getSource()==this.menuItem2){
  127.                                         row = 9;
  128.                                         col = 9;
  129.                                         mineRealCount=30;
  130.                                         mineCountLabel.setText("地雷个数:" + mineRealCount);
  131.                                         resutLabel.setText("   状态:准备游戏");
  132.                                         //设置埋雷区域
  133.                                         gamePanel.removeAll();
  134.                                         initButtonsAllValues();
  135.                                         gamePanel.setLayout(new GridLayout(row, col, 0, 0));
  136.                                         for (int i = 1; i <= row; i++) {
  137.                                                 for (int j = 1; j <= col; j++) {
  138.                                                         gamePanel.add(buttons[i][j]);
  139.                                                 }
  140.                                         }
  141.                                         contentPane.add(gamePanel, BorderLayout.CENTER);
  142.                                         // 设置地雷
  143.                                         timeLength=0;
  144.                                         setMines(mineRealCount);
  145.                                         setButtonValue();
  146.                                         addListener();
  147.                                        
  148.                                 }
  149.                                 if(e.getSource()==this.menuItem3){
  150.                                         row = 15;
  151.                                         col = 15;
  152.                                         mineRealCount=15;
  153.                                         mineCountLabel.setText("地雷个数:" + mineRealCount);
  154.                                         resutLabel.setText("   状态:准备游戏");
  155.                                         //设置埋雷区域
  156.                                         gamePanel.removeAll();
  157.                                         initButtonsAllValues();
  158.                                         gamePanel.setLayout(new GridLayout(row, col, 0, 0));
  159.                                         for (int i = 1; i <= row; i++) {
  160.                                                 for (int j = 1; j <= col; j++) {
  161.                                                         gamePanel.add(buttons[i][j]);
  162.                                                 }
  163.                                         }
  164.                                        
  165.                                         contentPane.add(gamePanel, BorderLayout.CENTER);
  166.                                        
  167.                                         // 设置地雷
  168.                                         timeLength=0;
  169.                                         setMines(mineRealCount);
  170.                                         setButtonValue();
  171.                                         addListener(); 
  172.                                 }
  173.                                
  174.                         }
  175.                        
  176.                
  177.                 //定义按钮数组
  178.                 public void initButtonsAllValues() {
  179.                         buttons = new JButton[row + 2][col + 2];
  180.                         buttonsValue = new int[row + 2][col + 2];
  181.                         buttonsFlag = new boolean[row + 2][col + 2];
  182.                         for (int i = 0; i < row + 2; i++) {
  183.                                 for (int j = 0; j < col + 2; j++) {
  184.                                         buttons[i][j] = new JButton();
  185.                                         buttons[i][j].setMargin(new Insets(0, 0, 0, 0));
  186.                                         buttons[i][j].setFont(new Font(null, Font.BOLD, 25));
  187.                                         buttons[i][j].setText("");
  188.                                         buttonsValue[i][j] = 0;
  189.  
  190.                                 }
  191.  
  192.                         }
  193.  
  194.                 }
  195.                
  196.                 // 设置地雷
  197.                 public void setMines(int mineCount) {
  198.                         this.mineCount = mineCount;
  199.                         int[] randomValue = new int[mineCount];
  200.                         // mineCount是地雷的个数,先获得mineCount个不重复的随机数,然后通过随机数计算出地雷的位置
  201.                         for (int i = 0; i < mineCount; i++) {
  202.  
  203.                                 int temp = (int) (Math.random() * row * col);
  204.                                 for (int j = 0; j < randomValue.length; j++) {
  205.                                         if (randomValue[j] == temp) {
  206.                                                 temp = (int) (Math.random() * row * col);
  207.                                                 j = 0;
  208.                                         }
  209.  
  210.                                 }
  211.                                 randomValue[i] = temp;
  212.                                 // 把随机数转换成坐标
  213.                                 int x = randomValue[i] / col + 1;
  214.                                 int y = randomValue[i] % col + 1;
  215.                                 // 对应坐标的位置设置为地雷
  216.                                 buttonsValue[x][y] = 10;
  217.                                 // 临时显示地雷位置,作为测试使用,随机产生
  218.                                 // buttons[x][y].setText("Q");
  219.  
  220.                         }
  221.                 }
  222.                
  223.                 // 对非地雷的按钮进行计算,周围没有地雷的,默认值为0,有雷的,显示地雷的个数。
  224.                 public void setButtonValue() {
  225.                         for (int i = 1; i <= row; i++) {
  226.                                 for (int j = 1; j <= col; j++) {
  227.                                         if (buttonsValue[i][j] != 10) {
  228.                                                 for (int x = j - 1; x <=j + 1; x++) {
  229.                                                         if (buttonsValue[i - 1][x] == 10) {
  230.                                                                 buttonsValue[i][j]++;
  231.                                                         }
  232.                                                         if (buttonsValue[i + 1][x] == 10) {
  233.                                                                 buttonsValue[i][j]++;
  234.                                                         }
  235.  
  236.                                                 }
  237.                                                 if (buttonsValue[i][j - 1] == 10) {
  238.                                                         buttonsValue[i][j]++;
  239.                                                 }
  240.                                                 if (buttonsValue[i][j + 1] == 10) {
  241.                                                         buttonsValue[i][j]++;
  242.                                                 }
  243.  
  244.                                                 // 测试
  245.                                                  buttons[i][j].setText(new Integer(buttonsValue[i][j]).toString());
  246.  
  247.                                         }
  248.  
  249.                                 }
  250.  
  251.                         }
  252.                 }
  253.                
  254.                
  255.                  //点击事件
  256.                 class ButtonActionListener implements ActionListener {
  257.  
  258.                         @Override
  259.                         public void actionPerformed(ActionEvent e) {
  260.                                 if (gameStatus==0) {
  261.                                         timer.start();
  262.                                 }
  263.                                 gameStatus=1;
  264.                                 for (int i = 1; i <= row; i++) {
  265.                                         for (int j = 1; j <= col; j++) {
  266.                                                 if (e.getSource() == buttons[i][j]) {
  267.                                                         if(!buttons[i][j].getText().isEmpty()&&buttonsFlag[i][j]==false){
  268.                                                                 buttons[i][j].setText("");
  269.                                                         }else{
  270.                                                         if (buttonsValue[i][j] == 0) {
  271.                                                                 markZero(i, j);
  272.                                                         } else if (buttonsValue[i][j] == 10) {
  273.                                                                 markMine(i, j);
  274.                                                                 stopGame();
  275.                                                         } else {
  276.                                                                 markNumber(i, j);
  277.                                                         }
  278.                                                         }
  279.                                                 }
  280.  
  281.                                         }
  282.  
  283.                                 }
  284.  
  285.                         }
  286.  
  287.                 }
  288.                
  289.                 //设置地雷属性
  290.                 class FindMineMouseListener extends MouseAdapter{
  291.                         public void mouseClicked(MouseEvent e) {
  292.                                 if (gameStatus==0) {
  293.                                         timer.start();
  294.                                 }
  295.                                 gameStatus=1;
  296.                                 for (int i = 1; i <=row; i++) {
  297.                                         for (int j = 1; j <=col; j++) {
  298.                                                 if(e.getSource()==buttons[i][j]&&e.getButton()==MouseEvent.BUTTON3){
  299.                                                         if (buttonsFlag[i][j]==false) {
  300.                                                                 findMine(i,j);
  301.                                                         }
  302.                                                        
  303.                                                 }
  304.                                        
  305.                                         }
  306.                                        
  307.                                 }
  308.                         }
  309.                 }
  310.                
  311.                 //添加监听
  312.                 public void addListener() {
  313.                         for (int i = 1; i <= row; i++) {
  314.                                 for (int j = 1; j <= col; j++) {
  315.                                         buttons[i][j].addActionListener(new ButtonActionListener());
  316.                                         buttons[i][j].addMouseListener(new FindMineMouseListener());
  317.                                        
  318.                                 }
  319.  
  320.                         }
  321.                 }
  322.                
  323.                 //计时器
  324.                 class TimerActionListener implements ActionListener{
  325.  
  326.                         @Override
  327.                         public void actionPerformed(ActionEvent e) {
  328.                                 timeLength++;
  329.                                 timeLabel.setText("游戏时间:"+new Integer(timeLength).toString()+"秒");
  330.                         }
  331.                        
  332.                 }
  333.  
  334.  
  335.                 public void markNumber(int i, int j) {
  336.                         buttons[i][j].setText(new Integer(buttonsValue[i][j]).toString());
  337.                         buttons[i][j].setEnabled(false);
  338.                         buttonsFlag[i][j] = true;
  339.                 }
  340.  
  341.                 public void markMine(int i, int j) {
  342.                         buttons[i][j].setForeground(Color.red);
  343.                         buttons[i][j].setText("Q");
  344.                         buttons[i][j].setEnabled(false);
  345.                         buttonsFlag[i][j] = true;
  346.  
  347.                 }
  348.  
  349.                 public void markZero(int i, int j) {
  350.                         //注意问题:当选择的是地雷时,不进行任何操作
  351.                         if(buttonsValue[i][j] == 10){
  352.                                 return ;
  353.                         }else{
  354.                         buttons[i][j].setEnabled(false);
  355.                         if (buttonsFlag[i][j] == true) {
  356.                                 return;
  357.                         } else {
  358.                                 buttonsFlag[i][j] = true;
  359.                                 if (buttonsValue[i][j] != 10 && buttonsValue[i][j] != 0) {
  360.                                         markNumber(i, j);
  361.                                 }
  362.                                 if (buttonsValue[i][j] == 0) {
  363.                                         buttons[i][j].setText("");
  364.                                         for (int s = i - 1; s >= 0 && s <= row && s <= i + 1; s++) //注意括号的问题
  365.                                                 for (int t = j - 1; t >= 0 && t <= col && t <= j + 1; t++) {
  366.                                                         markZero(s, t);
  367.                                                 }
  368.                                 }
  369.                                
  370.                         }
  371.                         }
  372.  
  373.                 }
  374.                
  375.                 //地雷处理
  376.                 public void findMine(int i,int j){
  377.                         buttons[i][j].setForeground(Color.red);
  378.                         buttons[i][j].setText("Q");
  379.                         mineCount--;   
  380.                         if (buttonsValue[i][j]==10) {
  381.                                 mineRealCount--;
  382.                         }
  383.                        
  384.                         isWinner();
  385.                        
  386.                 }
  387.                
  388.                 //判断游戏是否结束
  389.                 public  void stopGame() {
  390.                         for (int i = 1; i <= row; i++) {
  391.                                 for (int j = 1; j <=col; j++) {
  392.                                     if (buttonsFlag[i][j]==false) {
  393.                                         if (buttonsValue[i][j]==0) {
  394.                                                 buttons[i][j].setText("");
  395.                                                 }else if(buttonsValue[i][j]==10){
  396.                                                         buttons[i][j].setText("Q");
  397.                                                 }else{
  398.                                                         buttons[i][j].setText(new Integer(buttonsValue[i][j]).toString());
  399.                                                 }
  400.                                         }
  401.                                     buttons[i][j].setEnabled(false);
  402.                                    
  403.                                     gameStatus=0;
  404.                                     if (winGame) {
  405.                                                 resutLabel.setText("恭喜你,你赢了!");
  406.                                                
  407.                                         }else{
  408.                                                 resutLabel.setText("你踩到地雷了,很可惜!");
  409.                                                
  410.                                         }
  411.                                 }
  412.                                
  413.                         }
  414.                 }
  415.                
  416.                 //判断是否游戏胜利
  417.                 public void isWinner() {
  418.                         if (mineRealCount==0) {
  419.                                 winGame=true;
  420.                                 stopGame();
  421.                        
  422.                         }
  423.                 }
  424.                
  425. }
  426. ========================================================================
  427.  
  428. package fengke.game.test;
  429.  
  430. public class Test {
  431.        
  432.         public static void main(String[] args) {
  433.                 new Mine().initGame();
  434.         }
  435.  
  436. }
  437.  

回复 "简单JavaSE游戏----扫雷"

这儿你可以回复上面这条便签

captcha