zoukankan      html  css  js  c++  java
  • 简单JavaSE游戏----扫雷

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

    package chinasoft.game.fengke;

    import java.awt.BorderLayout;
    import java.awt.Color;
    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;

    /**
     * 扫雷游戏
     *
     * @author 锋客 功能: 时间限制;难度选择。
     *
     */
    public class MineSweepGame {

     // 声明所需组件
     private JFrame frame;
     private Container contentPane;// 视图
     private JPanel menuPanel, timePanel, gamePanel;// 菜单,时间,游戏三个部分
     private JLabel timeLabel, resutLabel, mineCountLabel;// 时间标签,状态标签,地雷数量
     private JMenuItem menuItem1, menuItem2, menuItem3;// 游戏难度选择

     private JButton[][] buttons;
     private int[][] buttonsValue;
     private boolean[][] buttonsFlag;

     private int timeLength = 0;
     private int row, col;

     private int mineRealCount = 10;
     private boolean winGame = false;
     // 计时器
     private Timer timer;
     private int gameStatus = 0;
     private TimerActionListener temp = new TimerActionListener();
     // 构造方法
     public MineSweepGame() {
      row = 9;
      col = 9;

      frame = new JFrame("锋客扫雷游戏");
      contentPane = frame.getContentPane();

      menuPanel = new JPanel();
      timePanel = new JPanel();
      gamePanel = new JPanel();

      timeLabel = new JLabel("游戏时间:" + new Integer(timeLength).toString()
        + "秒");
      resutLabel = new JLabel("   状态:准备游戏");
      mineCountLabel = new JLabel("地雷个数:" + mineRealCount);

      timer = new Timer(1000,temp);
     }

     public void initButtonsAllValues() {
      buttons = new JButton[row + 2][col + 2];
      buttonsValue = new int[row + 2][col + 2];
      buttonsFlag = new boolean[row + 2][col + 2];
      for (int i = 0; i < row + 2; i++) {
       for (int j = 0; j < col + 2; j++) {
        buttons[i][j] = new JButton();
        buttons[i][j].setMargin(new Insets(0, 0, 0, 0));
        buttons[i][j].setFont(new Font(null, Font.BOLD, 25));
        buttons[i][j].setText("");
        buttonsValue[i][j] = 0;

       }

      }

     }

     // 初始化游戏界面
     public void initGame() {
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("游戏设置");
      menuItem1 = new JMenuItem("初级");
      menuItem2 = new JMenuItem("中级");
      menuItem3 = new JMenuItem("高级");

      menuBar.add(menu);
      menu.add(menuItem1);
      menu.add(menuItem2);
      menu.add(menuItem3);
      menuPanel.add(menuBar);

      timePanel.add(timeLabel);
      timePanel.add(mineCountLabel);
      timePanel.add(resutLabel);

      JPanel panel = new JPanel();
      panel.setLayout(new BorderLayout());
      panel.add(menuPanel, BorderLayout.NORTH);
      panel.add(timePanel, BorderLayout.SOUTH);
      contentPane.add(panel, BorderLayout.NORTH);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setSize(297, 377);
      frame.setBounds(400, 100, 400, 500);
      frame.setVisible(true);
      // 选择难度(匿名内部类)
      menuItem1.addActionListener(new ActionListener() {

       @Override
       public void actionPerformed(ActionEvent e) {
        if (gameStatus == 0) {
         timer.start();
        }
        gameStatus = 1;
        row = 9;
        col = 9;
        mineRealCount = 10;
        mineCountLabel.setText("地雷个数:" + mineRealCount);
        resutLabel.setText("   状态:准备游戏");
        // 设置埋雷区域
        gamePanel.removeAll();
        initButtonsAllValues();
        gamePanel.setLayout(new GridLayout(row, col, 0, 0));
        for (int i = 1; i <= row; i++) {
         for (int j = 1; j <= col; j++) {
          gamePanel.add(buttons[i][j]);
         }
        }
        contentPane.add(gamePanel, BorderLayout.CENTER);
        // 设置地雷
        timeLength = 0;
        setMines(mineRealCount);
        setButtonValue();
        addListener();

       }
      });
      menuItem2.addActionListener(new ActionListener() {

       @Override
       public void actionPerformed(ActionEvent e) {
        if (gameStatus == 0) {
         timer.start();
        }
        gameStatus = 1;
        row = 9;
        col = 9;
        mineRealCount = 30;
        mineCountLabel.setText("地雷个数:" + mineRealCount);
        resutLabel.setText("   状态:准备游戏");
        // 设置埋雷区域
        gamePanel.removeAll();
        initButtonsAllValues();
        gamePanel.setLayout(new GridLayout(row, col, 0, 0));
        for (int i = 1; i <= row; i++) {
         for (int j = 1; j <= col; j++) {
          gamePanel.add(buttons[i][j]);
         }
        }
        contentPane.add(gamePanel, BorderLayout.CENTER);
        // 设置地雷
        timeLength = 0;
        setMines(mineRealCount);
        setButtonValue();
        addListener();

       }
      });
      menuItem3.addActionListener(new ActionListener() {

       @Override
       public void actionPerformed(ActionEvent e) {
        if (gameStatus == 0) {
         timer.start();
        }
        gameStatus = 1;
        row = 15;
        col = 15;
        mineRealCount = 15;
        mineCountLabel.setText("地雷个数:" + mineRealCount);
        resutLabel.setText("   状态:准备游戏");
        // 设置埋雷区域
        gamePanel.removeAll();
        initButtonsAllValues();
        gamePanel.setLayout(new GridLayout(row, col, 0, 0));
        for (int i = 1; i <= row; i++) {
         for (int j = 1; j <= col; j++) {
          gamePanel.add(buttons[i][j]);
         }
        }

        contentPane.add(gamePanel, BorderLayout.CENTER);

        // 设置地雷
        timeLength = 0;
        setMines(mineRealCount);
        setButtonValue();
        addListener();

       }
      });

     }

     // 设置地雷
     public void setMines(int mineCount) {
      this.mineRealCount = mineCount;
      int[] randomValue = new int[mineCount];
      // mineCount是地雷的个数,先获得mineCount个不重复的随机数,然后通过随机数计算出地雷的位置
      for (int i = 0; i < mineCount; i++) {

       int temp = (int) (Math.random() * row * col);
       for (int j = 0; j < randomValue.length; j++) {
        if (randomValue[j] == temp) {
         temp = (int) (Math.random() * row * col);
         j = 0;
        }

       }
       randomValue[i] = temp;
       // 把随机数转换成坐标
       int x = randomValue[i] / col + 1;
       int y = randomValue[i] % col + 1;
       // 对应坐标的位置设置为地雷
       buttonsValue[x][y] = 10;
       // 临时显示地雷位置,作为测试使用,随机产生
       // buttons[x][y].setText("Q");

      }
     }

     // 对非地雷的按钮进行计算,周围没有地雷的,默认值为0,有雷的,显示地雷的个数。
     public void setButtonValue() {
      for (int i = 1; i <= row; i++) {
       for (int j = 1; j <= col; j++) {
        if (buttonsValue[i][j] != 10) {
         for (int x = j - 1; x <= j + 1; x++) {
          if (buttonsValue[i - 1][x] == 10) {
           buttonsValue[i][j]++;
          }
          if (buttonsValue[i + 1][x] == 10) {
           buttonsValue[i][j]++;
          }

         }
         if (buttonsValue[i][j - 1] == 10) {
          buttonsValue[i][j]++;
         }
         if (buttonsValue[i][j + 1] == 10) {
          buttonsValue[i][j]++;
         }

         // 测试
         //buttons[i][j].setText(new Integer(buttonsValue[i][j])
         //  .toString());

        }

       }

      }
     }

     // 点击事件
     class ButtonActionListener implements ActionListener {

      @Override
      public void actionPerformed(ActionEvent e) {

       for (int i = 1; i <= row; i++) {
        for (int j = 1; j <= col; j++) {
         if (e.getSource() == buttons[i][j]) {
          if (!buttons[i][j].getText().isEmpty()
            && buttonsFlag[i][j] == false) {
           buttons[i][j].setText("");
          } else {
           if (buttonsValue[i][j] == 0) {
            markZero(i, j);
           } else if (buttonsValue[i][j] == 10) {
            System.out.println("停止时间");
            stopGame();
           } else {
            markNumber(i, j);
           }
          }
         }

        }

       }

      }

     }

     // 设置地雷属性
     class FindMineMouseListener extends MouseAdapter {
      public void mouseClicked(MouseEvent e) {
       for (int i = 1; i <= row; i++) {
        for (int j = 1; j <= col; j++) {
         if (e.getSource() == buttons[i][j]
           && e.getButton() == MouseEvent.BUTTON3) {
          if (buttonsFlag[i][j] == false) {
           findMine(i, j);
          }

         }

        }

       }
      }
     }

     // 计时器
     class TimerActionListener implements ActionListener {

      @Override
      public void actionPerformed(ActionEvent e) {

       timeLength++;
       timeLabel.setText("游戏时间:" + new Integer(timeLength).toString()
         + "秒");

       // if (timeLength>=20) {
       // for (int i = 1; i <= row; i++) {
       // for (int j = 1; j <=col; j++) {
       // if (buttonsFlag[i][j]==false) {
       // if (buttonsValue[i][j]==0) {
       // buttons[i][j].setText("");
       // }else if(buttonsValue[i][j]==10){
       // buttons[i][j].setText("Q");
       // }else{
       // buttons[i][j].setText(new
       // Integer(buttonsValue[i][j]).toString());
       // }
       // }
       // buttons[i][j].setEnabled(false);
       // timer.stop();
       // gameStatus=0;
       // if (winGame) {
       // resutLabel.setText("恭喜你,你赢了!");
       //
       // }else{
       // resutLabel.setText("很可惜,时间到!");
       //
       // }
       // }
       //
       // }
       // }
      }

     }
        //添加监听器
     public void addListener() {
      for (int i = 1; i <= row; i++) {
       for (int j = 1; j <= col; j++) {
        buttons[i][j].addActionListener(new ButtonActionListener());
        buttons[i][j].addMouseListener(new FindMineMouseListener());

       }

      }
     }
       
     public void markNumber(int i, int j) {
      buttons[i][j].setText(new Integer(buttonsValue[i][j]).toString());
      buttons[i][j].setEnabled(false);
      buttonsFlag[i][j] = true;
     }
        //标记地雷
     public void markMine(int i, int j) {
      buttons[i][j].setForeground(Color.red);
      buttons[i][j].setText("Q");
      buttons[i][j].setEnabled(false);
      buttonsFlag[i][j] = true;

     }
        //标记空白区域
     public void markZero(int i, int j) {
      // 注意问题:当选择的是地雷时,不进行任何操作
      if (buttonsValue[i][j] == 10) {
       return;
      } else {
       buttons[i][j].setEnabled(false);
       if (buttonsFlag[i][j] == true) {
        return;
       } else {
        buttonsFlag[i][j] = true;
        if (buttonsValue[i][j] != 10 && buttonsValue[i][j] != 0) {
         markNumber(i, j);
        }
        if (buttonsValue[i][j] == 0) {
         buttons[i][j].setText("");
         for (int s = i - 1; s >= 0 && s <= row && s <= i + 1; s++)
          // 注意括号的问题
          for (int t = j - 1; t >= 0 && t <= col && t <= j + 1; t++) {
           markZero(s, t);
          }
        }

       }
      }

     }

     // 发现地雷处理
     public void findMine(int i, int j) {
      if(buttons[i][j].getText()=="Q"){
       buttons[i][j].setForeground(Color.yellow);
       buttons[i][j].setText("?");
      
     }else{
      buttons[i][j].setForeground(Color.red);
      buttons[i][j].setText("Q");
      if (buttonsValue[i][j] == 10) {
       mineRealCount--;
      }
      isWinner();
     }
    }
     

     // 判断游戏是否结束
     public void stopGame() {
      for (int i = 1; i <= row; i++) {
       for (int j = 1; j <= col; j++) {

        if (buttonsFlag[i][j] == false) {
         if (buttonsValue[i][j] == 0) {
          buttons[i][j].setText("");
         } else if (buttonsValue[i][j] == 10) {
          buttons[i][j].setText("Q");
         } else {
          buttons[i][j].setText(new Integer(buttonsValue[i][j])
            .toString());
         }
        }

        buttons[i][j].setEnabled(false);

       }

      }
      gameStatus = 0;
      timer.stop();
      if (winGame) {
       resutLabel.setText("恭喜你,你赢了!");

      } else {
       resutLabel.setText("你踩到地雷了,很可惜!");
      }
     }

     // 判断是否游戏胜利
     public void isWinner() {
      if (mineRealCount == 0) {
       winGame = true;
       stopGame();
      }
     }

    }

  • 相关阅读:
    个人学习随笔(psi-blast随笔)
    psp进度(11月25号-31号)
    本周psp(11月17-23)
    规格说明书练习-吉林市1日游
    补PSP进度(10.28-11.3)
    第九周PSP&进度条
    (第九周)读构建之法有感1
    (第九周)读构建之法有感2
    词频统计的效能测试。
    (第二周)读《构建之法》有感
  • 原文地址:https://www.cnblogs.com/fengke/p/4800232.html
Copyright © 2011-2022 走看看