|
@ -7,54 +7,14 @@ import javax.swing.JPanel; |
|
|
public class MinesweeperGame extends JPanel { |
|
|
public class MinesweeperGame extends JPanel { |
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L; |
|
|
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 static final int WIDTH = 600, HEIGTH = 600; |
|
|
|
|
|
|
|
|
public Cell[][] playfield; |
|
|
|
|
|
|
|
|
public Playfield playfield; |
|
|
|
|
|
|
|
|
public MinesweeperGame(int _playfieldSize, int _bombAmount) { |
|
|
public MinesweeperGame(int _playfieldSize, int _bombAmount) { |
|
|
this.setSize(WIDTH, HEIGTH); |
|
|
this.setSize(WIDTH, HEIGTH); |
|
|
playfieldSize = _playfieldSize; |
|
|
|
|
|
bombAmount = _bombAmount; |
|
|
|
|
|
setLayout(null); |
|
|
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; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
playfield = new Playfield(this, _playfieldSize, _bombAmount ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
public static void main(String[] args) { |
|
|