A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lxdamya 初级黑马   /  2015-5-14 12:42  /  430 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class Frame
  5.     extends JFrame {
  6.   JTextField text;
  7.   JLabel nowBomb, setBomb;
  8.   int BombNum, BlockNum; // 当前雷数,当前方块数
  9.   int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数
  10.   JButton start = new JButton(" 开始 ");
  11.   JPanel MenuPamel = new JPanel();
  12.   JPanel bombPanel = new JPanel();
  13.   Bomb[][] bombButton;
  14.   JPanel c;
  15.   BorderLayout borderLayout1 = new BorderLayout();
  16.   GridLayout gridLayout1 = new GridLayout();
  17.   public Frame() {
  18.     try {
  19.       setDefaultCloseOperation(EXIT_ON_CLOSE);
  20.       jbInit();
  21.     }
  22.     catch (Exception exception) {
  23.       exception.printStackTrace();
  24.     }
  25.   }
  26. private void jbInit() throws Exception {
  27.     c = (JPanel) getContentPane();
  28.     setTitle("扫雷");
  29.     c.setBackground(Color.WHITE);
  30.     MenuPamel.setBackground(Color.GRAY);
  31.     c.setLayout(borderLayout1);
  32.     setSize(new Dimension(600, 600));
  33.     setResizable(false);
  34.     BlockNum = 144;
  35.     BombNum = 10;
  36.     text = new JTextField("10 ", 3);
  37.     nowBomb = new JLabel("当前雷数" + ":" + BombNum);
  38.     setBomb = new JLabel("设置地雷数");
  39.     start.addActionListener(new Frame1_start_actionAdapter(this));
  40.     MenuPamel.add(setBomb);
  41.     MenuPamel.add(text);
  42.     MenuPamel.add(start);
  43.     MenuPamel.add(nowBomb);
  44.     c.add(MenuPamel, java.awt.BorderLayout.SOUTH);
  45.     bombPanel.setLayout(gridLayout1);
  46.     gridLayout1.setColumns( (int) Math.sqrt(BlockNum));
  47.     gridLayout1.setRows( (int) Math.sqrt(BlockNum));
  48.     bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];
  49.     for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  50.       for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  51.         bombButton[i][j] = new Bomb(i, j);
  52.         //bombButton[i][j].setSize(10, 10);
  53.         bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小
  54.       
  55.         bombButton[i][j].setForeground(Color.white);
  56.         bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
  57.         bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
  58.         bombPanel.add(bombButton[i][j]);
  59.       }
  60.     }
  61.     c.add(bombPanel, java.awt.BorderLayout.CENTER);
  62.     startBomb();
  63.   }

  64.   /* 开始按钮 */

  65.   public void start_actionPerformed(ActionEvent e) {
  66.    int num=Integer.parseInt(text.getText().trim());
  67.     if (num >= 5 && num < 50) {
  68.       BombNum = num;
  69.       startBomb();
  70.     }
  71.     else if (num < 5) {
  72.       JOptionPane.showMessageDialog(null, "您设置的地雷数太少了,请重设!", "错误",
  73.                                     JOptionPane.ERROR_MESSAGE);
  74.                                     num=10;
  75.                                     BombNum = num;
  76.     }
  77.     else {
  78.       JOptionPane.showMessageDialog(null, "您设置的地雷数太多了,请重设!", "错误",
  79.                                     JOptionPane.ERROR_MESSAGE);
  80.                                      num=10;
  81.                                     BombNum = num;
  82.     }
  83.   }

  84.   /* 开始,布雷 */

  85.   public void startBomb() {
  86.     nowBomb.setText("当前雷数" + ":" + BombNum);
  87.     for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  88.       for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  89.         bombButton[i][j].isBomb = false;
  90.         bombButton[i][j].isClicked = false;
  91.         bombButton[i][j].isRight = false;
  92.         bombButton[i][j].BombFlag = 0;
  93.         bombButton[i][j].BombRoundCount = 9;
  94.         bombButton[i][j].setEnabled(true);
  95.         bombButton[i][j].setText("");
  96.         bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小
  97.         bombButton[i][j].setForeground(Color.BLUE);
  98.         rightBomb = 0;
  99.         restBomb = BombNum;
  100.         restBlock = BlockNum - BombNum;
  101.       }
  102.     }

  103.     for (int i = 0; i < BombNum; ) {
  104.       int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
  105.       int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));

  106.       if (bombButton[x][y].isBomb != true) {
  107.         bombButton[x][y].isBomb = true;
  108.         i++;
  109.       }
  110.     }
  111.     CountRoundBomb();
  112.   }

  113.   /* 计算方块周围雷数 */

  114.   public void CountRoundBomb() {
  115.     for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  116.       for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  117.         int count = 0;
  118.         // 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
  119.         if (bombButton[i][j].isBomb != true) {
  120.           for (int x = i - 1; x < i + 2; x++) {
  121.             for (int y = j - 1; y < j + 2; y++) {
  122.               if ( (x >= 0) && (y >= 0)
  123.                   && (x < ( (int) Math.sqrt(BlockNum)))
  124.                   && (y < ( (int) Math.sqrt(BlockNum)))) {
  125.                 if (bombButton[x][y].isBomb == true) {
  126.                   count++;
  127.                 }
  128.               }
  129.             }
  130.           }
  131.           bombButton[i][j].BombRoundCount = count;
  132.         }
  133.       }
  134.     }
  135.   }

  136.   /* 是否挖完了所有的雷 */

  137.   public void isWin() {
  138.     restBlock = BlockNum - BombNum;
  139.     for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  140.       for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  141.         if (bombButton[i][j].isClicked == true) {
  142.           restBlock--;
  143.         }
  144.       }
  145.     }

  146.     if (rightBomb == BombNum || restBlock == 0) {
  147.       JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",
  148.                                     JOptionPane.INFORMATION_MESSAGE);
  149.       startBomb();
  150.     }
  151.   }

  152.   /** 当选中的位置为空,则翻开周围的地图* */

  153.   public void isNull(Bomb ClickedButton) {
  154.     int i, j;
  155.     i = ClickedButton.num_x;
  156.     j = ClickedButton.num_y;

  157.     for (int x = i - 1; x < i + 2; x++) {
  158.       for (int y = j - 1; y < j + 2; y++) {
  159.         if ( ( (x != i) || (y != j)) && (x >= 0) && (y >= 0)
  160.             && (x < ( (int) Math.sqrt(BlockNum)))
  161.             && (y < ( (int) Math.sqrt(BlockNum)))) {
  162.           if (bombButton[x][y].isBomb == false
  163.               && bombButton[x][y].isClicked == false
  164.               && bombButton[x][y].isRight == false) {
  165.             turn(bombButton[x][y]);
  166.           }
  167.         }
  168.       }
  169.     }
  170.   }

  171.   /* 翻开 */

  172.   public void turn(Bomb ClickedButton) {
  173.     ClickedButton.setEnabled(false);
  174.     ClickedButton.isClicked = true;
  175.     if (ClickedButton.BombRoundCount > 0) {
  176.       ClickedButton.setText(ClickedButton.BombRoundCount + "");
  177.     }
  178.     else {
  179.       isNull(ClickedButton);
  180.     }
  181.   }

  182.   /* 左键点击 */

  183.   public void actionPerformed(ActionEvent e) {
  184.     if ( ( (Bomb) e.getSource()).isClicked == false
  185.         && ( (Bomb) e.getSource()).isRight == false) {
  186.       if ( ( (Bomb) e.getSource()).isBomb == false) {
  187.         turn( ( (Bomb) e.getSource()));
  188.         isWin();
  189.       }

  190.       else {
  191.         for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  192.           for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  193.             if (bombButton[i][j].isBomb == true) {
  194.               bombButton[i][j].setText("b");
  195.             }
  196.           }
  197.         }
  198.         ( (Bomb) e.getSource()).setForeground(Color.RED);
  199.         ( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));
  200.         ( (Bomb) e.getSource()).setText("X");
  201.         JOptionPane.showMessageDialog(this, "你踩到地雷了,按确定重来", "踩到地雷", 2);
  202.         startBomb();
  203.       }
  204.     }
  205.   }

  206.   /* 右键点击 */

  207.   public void mouseClicked(MouseEvent e) {
  208.     Bomb bombSource = (Bomb) e.getSource();
  209.     boolean right = SwingUtilities.isRightMouseButton(e);

  210.     if ( (right == true) && (bombSource.isClicked == false)) {
  211.       bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
  212.       if (bombSource.BombFlag == 1) {
  213.         if (restBomb > 0) {
  214.           bombSource.setForeground(Color.RED);
  215.           bombSource.setText("F");
  216.           bombSource.isRight = true;
  217.           restBomb--;
  218.         }
  219.         else {
  220.           bombSource.BombFlag = 0;
  221.         }
  222.       }
  223.       else if (bombSource.BombFlag == 2) {
  224.         restBomb++;
  225.         bombSource.setText("Q");
  226.         bombSource.isRight = false;
  227.       }
  228.       else {
  229.         bombSource.setText("");
  230.       }

  231.       if (bombSource.isBomb == true) {
  232.         if (bombSource.BombFlag == 1) {
  233.           rightBomb++;
  234.         }
  235.         else if (bombSource.BombFlag == 2) {
  236.           rightBomb--;
  237.         }
  238.       }
  239.       nowBomb.setText("当前雷数" + ":" + restBomb);
  240.       isWin();
  241.     }
  242.   }

  243.   public static void main(String[] args) {
  244.     Frame frame = new Frame();
  245.     frame.setVisible(true);
  246.   }
  247. }

  248. class Frame1_start_actionAdapter
  249.     implements ActionListener {
  250.   private Frame adaptee;
  251.   Frame1_start_actionAdapter(Frame adaptee) {
  252.     this.adaptee = adaptee;
  253.   }

  254.   public void actionPerformed(ActionEvent e) {
  255.     adaptee.start_actionPerformed(e);
  256.   }
  257. }

  258. ////////////////////////////
  259. class Bomb
  260.     extends JButton {
  261.   int num_x, num_y; // 第几号方块
  262.   int BombRoundCount; // 周围雷数
  263.   boolean isBomb; // 是否为雷
  264.   boolean isClicked; // 是否被点击
  265.   int BombFlag; // 探雷标记
  266.   boolean isRight; // 是否点击右键

  267.   public Bomb(int x, int y) {
  268.     num_x = x;
  269.     num_y = y;
  270.     BombFlag = 0;
  271.     BombRoundCount = 9;
  272.     isBomb = false;
  273.     isClicked = false;
  274.     isRight = false;
  275.   }
  276. }

  277. class Bomb_actionAdapter
  278.     implements ActionListener {
  279.   private Frame adaptee;
  280.   Bomb_actionAdapter(Frame adaptee) {
  281.     this.adaptee = adaptee;
  282.   }

  283.   public void actionPerformed(ActionEvent e) {
  284.     adaptee.actionPerformed(e);
  285.   }
  286. }

  287. class Bomb_mouseAdapter
  288.     extends MouseAdapter {
  289.   private Frame adaptee;
  290.   Bomb_mouseAdapter(Frame adaptee) {
  291.     this.adaptee = adaptee;
  292.   }

  293.   public void mouseClicked(MouseEvent e) {
  294.     adaptee.mouseClicked(e);
  295.   }
  296. }
复制代码

5 个回复

倒序浏览
谢谢分享!!
回复 使用道具 举报
好流弊的样子
回复 使用道具 举报
支持一下!
回复 使用道具 举报
还厉害的样子  
回复 使用道具 举报
很好很强大啊,学习了………………
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马