|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.ActionEvent; |
| 3 | +import java.awt.event.ActionListener; |
| 4 | +import java.util.Random; |
| 5 | +import javax.swing.*; |
| 6 | + |
| 7 | +public class Minesweeper extends JFrame { |
| 8 | + private JButton[][] buttons; |
| 9 | + private boolean[][] mines; |
| 10 | + private int[][] surroundingMines; |
| 11 | + private int uncoveredCells; |
| 12 | + |
| 13 | + public Minesweeper() { |
| 14 | + setTitle("Minesweeper"); |
| 15 | + setDefaultCloseOperation(EXIT_ON_CLOSE); |
| 16 | + setLayout(new GridLayout(10, 10)); |
| 17 | + |
| 18 | + buttons = new JButton[10][10]; |
| 19 | + mines = new boolean[10][10]; |
| 20 | + surroundingMines = new int[10][10]; |
| 21 | + uncoveredCells = 0; |
| 22 | + |
| 23 | + for (int i = 0; i < 10; i++) { |
| 24 | + for (int j = 0; j < 10; j++) { |
| 25 | + buttons[i][j] = new JButton(); |
| 26 | + buttons[i][j].addActionListener(new CellClickListener(i, j)); |
| 27 | + add(buttons[i][j]); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + placeMines(); |
| 32 | + countSurroundingMines(); |
| 33 | + |
| 34 | + pack(); |
| 35 | + setVisible(true); |
| 36 | + } |
| 37 | + |
| 38 | + private void placeMines() { |
| 39 | + Random random = new Random(); |
| 40 | + int placedMines = 0; |
| 41 | + while (placedMines < 10) { |
| 42 | + int i = random.nextInt(10); |
| 43 | + int j = random.nextInt(10); |
| 44 | + if (!mines[i][j]) { |
| 45 | + mines[i][j] = true; |
| 46 | + placedMines++; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private void countSurroundingMines() { |
| 52 | + for (int i = 0; i < 10; i++) { |
| 53 | + for (int j = 0; j < 10; j++) { |
| 54 | + if (!mines[i][j]) { |
| 55 | + int count = 0; |
| 56 | + if (i > 0 && mines[i - 1][j]) count++; |
| 57 | + if (i < 9 && mines[i + 1][j]) count++; |
| 58 | + if (j > 0 && mines[i][j - 1]) count++; |
| 59 | + if (j < 9 && mines[i][j + 1]) count++; |
| 60 | + if (i > 0 && j > 0 && mines[i - 1][j - 1]) count++; |
| 61 | + if (i < 9 && j < 9 && mines[i + 1][j + 1]) count++; |
| 62 | + if (i > 0 && j < 9 && mines[i - 1][j + 1]) count++; |
| 63 | + if (i < 9 && j > 0 && mines[i + 1][j - 1]) count++; |
| 64 | + surroundingMines[i][j] = count; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private void uncoverCell(int i, int j) { |
| 71 | + if (mines[i][j]) { |
| 72 | + loseGame(); |
| 73 | + } else { |
| 74 | + buttons[i][j].setText(Integer.toString(surroundingMines[i][j])); |
| 75 | + buttons[i][j].setEnabled(false); |
| 76 | + uncoveredCells++; |
| 77 | + if (uncoveredCells == 90) { |
| 78 | + winGame(); |
| 79 | + } |
| 80 | + if (surroundingMines[i][j] == 0) { |
| 81 | + uncoverSurroundingCells(i, j); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void uncoverSurroundingCells(int i, int j) { |
| 87 | + if (i > 0 && buttons[i - 1][j].isEnabled()) uncoverCell(i - 1, j); |
| 88 | + if (i < 9 && buttons[i + 1][j].isEnabled()) uncoverCell(i + 1, j); |
| 89 | + if (j > 0 && buttons[i][j - 1].isEnabled()) uncoverCell(i, j - 1); |
| 90 | + if (j < 9 && buttons[i][j + 1].isEnabled()) uncoverCell(i, j + 1); |
| 91 | + if (i > 0 && j > 0 && buttons[i - 1][j - 1].isEnabled()) uncoverCell( |
| 92 | + i - 1, |
| 93 | + j - 1 |
| 94 | + ); |
| 95 | + if (i < 9 && j < 9 && buttons[i + 1][j + 1].isEnabled()) uncoverCell( |
| 96 | + i + 1, |
| 97 | + j + 1 |
| 98 | + ); |
| 99 | + if (i > 0 && j < 9 && buttons[i - 1][j + 1].isEnabled()) uncoverCell( |
| 100 | + i - 1, |
| 101 | + j + 1 |
| 102 | + ); |
| 103 | + if (i < 9 && j > 0 && buttons[i + 1][j - 1].isEnabled()) uncoverCell( |
| 104 | + i + 1, |
| 105 | + j - 1 |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + private void winGame() { |
| 110 | + JOptionPane.showMessageDialog(this, "You won!"); |
| 111 | + System.exit(0); |
| 112 | + } |
| 113 | + |
| 114 | + private void loseGame() { |
| 115 | + for (int i = 0; i < 10; i++) { |
| 116 | + for (int j = 0; j < 10; j++) { |
| 117 | + if (mines[i][j]) { |
| 118 | + buttons[i][j].setText("*"); |
| 119 | + } |
| 120 | + buttons[i][j].setEnabled(false); |
| 121 | + } |
| 122 | + } |
| 123 | + JOptionPane.showMessageDialog(this, "You lost."); |
| 124 | + System.exit(0); |
| 125 | + } |
| 126 | + |
| 127 | + private class CellClickListener implements ActionListener { |
| 128 | + private int i; |
| 129 | + private int j; |
| 130 | + |
| 131 | + public CellClickListener(int i, int j) { |
| 132 | + this.i = i; |
| 133 | + this.j = j; |
| 134 | + } |
| 135 | + |
| 136 | + public void actionPerformed(ActionEvent e) { |
| 137 | + uncoverCell(i, j); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments