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

来自 , 2020-11-25, 写在 Java, 查看 123 次.
URL http://www.code666.cn/view/1373b284
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class Frame
  5. extends JFrame {
  6. JLabel nowBomb, setBomb;
  7. int BombNum, BlockNum; // 当前雷数,当前方块数
  8. int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数
  9. JButton start = new JButton(" 开始 ");
  10. JPanel MenuPamel = new JPanel();
  11. JPanel bombPanel = new JPanel();
  12. Bomb[][] bombButton;
  13. BorderLayout borderLayout1 = new BorderLayout();
  14. GridLayout gridLayout1 = new GridLayout();
  15. public Frame() {
  16. try {
  17. setDefaultCloseOperation(EXIT_ON_CLOSE);
  18. jbInit();
  19. }
  20. catch (Exception exception) {
  21. exception.printStackTrace();
  22. }
  23. }
  24. private void jbInit() throws Exception {
  25. c = (JPanel) getContentPane();
  26. setTitle("扫雷");
  27. c.setBackground(Color.WHITE);
  28. MenuPamel.setBackground(Color.GRAY);
  29. c.setLayout(borderLayout1);
  30. setSize(new Dimension(600, 600));
  31. setResizable(false);
  32. BlockNum = 144;
  33. BombNum = 10;
  34. text = new JTextField("10 ", 3);
  35. nowBomb = new JLabel("当前雷数" + ":" + BombNum);
  36. setBomb = new JLabel("设置地雷数");
  37. start.addActionListener(new Frame1_start_actionAdapter(this));
  38. MenuPamel.add(setBomb);
  39. MenuPamel.add(text);
  40. MenuPamel.add(start);
  41. MenuPamel.add(nowBomb);
  42. c.add(MenuPamel, java.awt.BorderLayout.SOUTH);
  43. bombPanel.setLayout(gridLayout1);
  44. gridLayout1.setColumns( (int) Math.sqrt(BlockNum));
  45. gridLayout1.setRows( (int) Math.sqrt(BlockNum));
  46. bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];
  47. for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  48. for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  49. bombButton[i][j] = new Bomb(i, j);
  50. //bombButton[i][j].setSize(10, 10);
  51. bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小
  52. bombButton[i][j].setForeground(Color.white);
  53. bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
  54. bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
  55. bombPanel.add(bombButton[i][j]);
  56. }
  57. }
  58. c.add(bombPanel, java.awt.BorderLayout.CENTER);
  59. startBomb();
  60. }
  61. /* 开始按钮 */
  62. public void start_actionPerformed(ActionEvent e) {
  63. int num=Integer.parseInt(text.getText().trim());
  64. if (num >= 5 && num < 50) {
  65. BombNum = num;
  66. startBomb();
  67. }
  68. else if (num < 5) {
  69. JOptionPane.showMessageDialog(null, "您设置的地雷数太少了,请重设!", "错误",
  70. JOptionPane.ERROR_MESSAGE);
  71. num=10;
  72. BombNum = num;
  73. }
  74. else {
  75. JOptionPane.showMessageDialog(null, "您设置的地雷数太多了,请重设!", "错误",
  76. JOptionPane.ERROR_MESSAGE);
  77. num=10;
  78. BombNum = num;
  79. }
  80. }
  81. /* 开始,布雷 */
  82. public void startBomb() {
  83. nowBomb.setText("当前雷数" + ":" + BombNum);
  84. for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  85. for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  86. bombButton[i][j].isBomb = false;
  87. bombButton[i][j].isClicked = false;
  88. bombButton[i][j].isRight = false;
  89. bombButton[i][j].BombFlag = 0;
  90. bombButton[i][j].BombRoundCount = 9;
  91. bombButton[i][j].setEnabled(true);
  92. bombButton[i][j].setText("");
  93. bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小
  94. bombButton[i][j].setForeground(Color.BLUE);
  95. rightBomb = 0;
  96. restBomb = BombNum;
  97. restBlock = BlockNum - BombNum;
  98. }
  99. }
  100. for (int i = 0; i < BombNum; ) {
  101. int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
  102. int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
  103. if (bombButton[x][y].isBomb != true) {
  104. bombButton[x][y].isBomb = true;
  105. i++;
  106. }
  107. }
  108. CountRoundBomb();
  109. }
  110. /* 计算方块周围雷数 */
  111. public void CountRoundBomb() {
  112. for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  113. for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  114. int count = 0;
  115. // 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
  116. if (bombButton[i][j].isBomb != true) {
  117. for (int x = i - 1; x < i + 2; x++) {
  118. for (int y = j - 1; y < j + 2; y++) {
  119. if ( (x >= 0) && (y >= 0)
  120. && (x < ( (int) Math.sqrt(BlockNum)))
  121. && (y < ( (int) Math.sqrt(BlockNum)))) {
  122. if (bombButton[x][y].isBomb == true) {
  123. count++;
  124. }
  125. }
  126. }
  127. }
  128. bombButton[i][j].BombRoundCount = count;
  129. }
  130. }
  131. }
  132. }
  133. /* 是否挖完了所有的雷 */
  134. public void isWin() {
  135. restBlock = BlockNum - BombNum;
  136. for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  137. for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  138. if (bombButton[i][j].isClicked == true) {
  139. restBlock--;
  140. }
  141. }
  142. }
  143. if (rightBomb == BombNum || restBlock == 0) {
  144. JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",
  145. JOptionPane.INFORMATION_MESSAGE);
  146. startBomb();
  147. }
  148. }
  149. /** 当选中的位置为空,则翻开周围的地图* */
  150. public void isNull(Bomb ClickedButton) {
  151. int i, j;
  152. i = ClickedButton.num_x;
  153. j = ClickedButton.num_y;
  154. for (int x = i - 1; x < i + 2; x++) {
  155. for (int y = j - 1; y < j + 2; y++) {
  156. if ( ( (x != i) || (y != j)) && (x >= 0) && (y >= 0)
  157. && (x < ( (int) Math.sqrt(BlockNum)))
  158. && (y < ( (int) Math.sqrt(BlockNum)))) {
  159. if (bombButton[x][y].isBomb == false
  160. && bombButton[x][y].isClicked == false
  161. && bombButton[x][y].isRight == false) {
  162. turn(bombButton[x][y]);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. /* 翻开 */
  169. public void turn(Bomb ClickedButton) {
  170. ClickedButton.setEnabled(false);
  171. ClickedButton.isClicked = true;
  172. if (ClickedButton.BombRoundCount > 0) {
  173. ClickedButton.setText(ClickedButton.BombRoundCount + "");
  174. }
  175. else {
  176. isNull(ClickedButton);
  177. }
  178. }
  179. /* 左键点击 */
  180. public void actionPerformed(ActionEvent e) {
  181. if ( ( (Bomb) e.getSource()).isClicked == false
  182. && ( (Bomb) e.getSource()).isRight == false) {
  183. if ( ( (Bomb) e.getSource()).isBomb == false) {
  184. turn( ( (Bomb) e.getSource()));
  185. isWin();
  186. }
  187. else {
  188. for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
  189. for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
  190. if (bombButton[i][j].isBomb == true) {
  191. bombButton[i][j].setText("b");
  192. }
  193. }
  194. }
  195. ( (Bomb) e.getSource()).setForeground(Color.RED);
  196. ( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));
  197. ( (Bomb) e.getSource()).setText("X");
  198. JOptionPane.showMessageDialog(this, "你踩到地雷了,按确定重来", "踩到地雷", 2);
  199. startBomb();
  200. }
  201. }
  202. }
  203. /* 右键点击 */
  204. public void mouseClicked(MouseEvent e) {
  205. Bomb bombSource = (Bomb) e.getSource();
  206. boolean right = SwingUtilities.isRightMouseButton(e);
  207. if ( (right == true) && (bombSource.isClicked == false)) {
  208. bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
  209. if (bombSource.BombFlag == 1) {
  210. if (restBomb > 0) {
  211. bombSource.setForeground(Color.RED);
  212. bombSource.setText("F");
  213. bombSource.isRight = true;
  214. restBomb--;
  215. }
  216. else {
  217. bombSource.BombFlag = 0;
  218. }
  219. }
  220. else if (bombSource.BombFlag == 2) {
  221. restBomb++;
  222. bombSource.setText("Q");
  223. bombSource.isRight = false;
  224. }
  225. else {
  226. bombSource.setText("");
  227. }
  228. if (bombSource.isBomb == true) {
  229. if (bombSource.BombFlag == 1) {
  230. rightBomb++;
  231. }
  232. else if (bombSource.BombFlag == 2) {
  233. rightBomb--;
  234. }
  235. }
  236. nowBomb.setText("当前雷数" + ":" + restBomb);
  237. isWin();
  238. }
  239. }
  240. public static void main(String[] args) {
  241. Frame frame = new Frame();
  242. frame.setVisible(true);
  243. }
  244. }
  245. class Frame1_start_actionAdapter
  246. implements ActionListener {
  247. private Frame adaptee;
  248. Frame1_start_actionAdapter(Frame adaptee) {
  249. this.adaptee = adaptee;
  250. }
  251. public void actionPerformed(ActionEvent e) {
  252. adaptee.start_actionPerformed(e);
  253. }
  254. }
  255. ////////////////////////////
  256. class Bomb
  257. extends JButton {
  258. int num_x, num_y; // 第几号方块
  259. int BombRoundCount; // 周围雷数
  260. boolean isBomb; // 是否为雷
  261. boolean isClicked; // 是否被点击
  262. int BombFlag; // 探雷标记
  263. boolean isRight; // 是否点击右键
  264. public Bomb(int x, int y) {
  265. num_x = x;
  266. num_y = y;
  267. BombFlag = 0;
  268. BombRoundCount = 9;
  269. isBomb = false;
  270. isClicked = false;
  271. isRight = false;
  272. }
  273. }
  274. class Bomb_actionAdapter
  275. implements ActionListener {
  276. private Frame adaptee;
  277. Bomb_actionAdapter(Frame adaptee) {
  278. this.adaptee = adaptee;
  279. }
  280. public void actionPerformed(ActionEvent e) {
  281. adaptee.actionPerformed(e);
  282. }
  283. }
  284. class Bomb_mouseAdapter
  285. extends MouseAdapter {
  286. private Frame adaptee;
  287. Bomb_mouseAdapter(Frame adaptee) {
  288. this.adaptee = adaptee;
  289. }
  290. public void mouseClicked(MouseEvent e) {
  291. adaptee.mouseClicked(e);
  292. }
  293. }

回复 "简单扫雷"

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

captcha