You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.6 KiB
71 lines
1.6 KiB
package Minesweeper;
|
|
|
|
import javax.swing.JButton;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JPanel;
|
|
|
|
public class MinesweeperGame extends JPanel {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
private static final int WIDTH = 600, HEIGTH = 600;
|
|
private static final int CELLSIZE = 40;
|
|
private int playfieldSize;
|
|
private int bombAmount;
|
|
|
|
public Cell[][] playfield;
|
|
|
|
public MinesweeperGame(int _playfieldSize, int _bombAmount) {
|
|
this.setSize(WIDTH, HEIGTH);
|
|
playfieldSize = _playfieldSize;
|
|
bombAmount = _bombAmount;
|
|
setLayout(null);
|
|
|
|
initPlayfield();
|
|
}
|
|
|
|
private void initPlayfield() {
|
|
|
|
playfield = new Cell[playfieldSize][playfieldSize];
|
|
|
|
int[] bPlacment = new int[bombAmount];
|
|
for (int i = 0; i < bPlacment.length; i++) {
|
|
bPlacment[i] = (int) (Math.random() * playfieldSize * playfieldSize);
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
if (bPlacment[i] == bPlacment[j]) {
|
|
i--;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < playfieldSize; i++) {
|
|
for (int j = 0; j < playfieldSize; j++) {
|
|
playfield[i][j] = new Cell(CellType.Number);
|
|
|
|
playfield[i][j].setBounds(j * CELLSIZE + (WIDTH / 2 - playfieldSize * CELLSIZE / 2),
|
|
i * CELLSIZE + (HEIGTH / 2 - playfieldSize * CELLSIZE / 2), CELLSIZE, CELLSIZE);
|
|
add(playfield[i][j]);
|
|
|
|
for (int k = 0; k < bPlacment.length; k++) {
|
|
if (bPlacment[k] == i * playfieldSize + j) {
|
|
playfield[i][j].type = CellType.Bomb;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
JFrame f = new JFrame();
|
|
MinesweeperGame ttt = new MinesweeperGame(8, 10);
|
|
|
|
f.add(ttt);
|
|
f.setSize(WIDTH, HEIGTH);
|
|
f.setLayout(null);
|
|
f.setVisible(true);
|
|
|
|
}
|
|
|
|
}
|