From aa04d679bd0d4f92df6725c2883db57372601dd8 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 11:36:45 +0100 Subject: [PATCH] Moved playfield logic to new class --- .../java/Minesweeper/MinesweeperGame.java | 46 ++----------------- src/main/java/Minesweeper/Playfield.java | 46 +++++++++++++++++++ .../java/Minesweeper/MinesweeperGameTest.java | 2 +- 3 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 src/main/java/Minesweeper/Playfield.java diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index ffb4a10..df5d33c 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -7,54 +7,14 @@ 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 static final int WIDTH = 600, HEIGTH = 600; - public Cell[][] playfield; + public Playfield 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; - } - } - } - } + playfield = new Playfield(this, _playfieldSize, _bombAmount ); } public static void main(String[] args) { diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java new file mode 100644 index 0000000..013a0bb --- /dev/null +++ b/src/main/java/Minesweeper/Playfield.java @@ -0,0 +1,46 @@ +package Minesweeper; + +public class Playfield { + + private static final int CELLSIZE = 40; + private int playfieldSize; + private MinesweeperGame MsG; + public Cell[][] playfield; + + public Playfield(MinesweeperGame _MsG, int _playfieldSize, int _bombAmount) { + MsG = _MsG; + playfieldSize = _playfieldSize; + + playfield = new Cell[playfieldSize][playfieldSize]; + + int[] bPlacement = new int[_bombAmount]; + for (int i = 0; i < bPlacement.length; i++) { + bPlacement[i] = (int) (Math.random() * playfieldSize * playfieldSize); + + for (int j = 0; j < i; j++) { + if (bPlacement[i] == bPlacement[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 + (MsG.WIDTH / 2 - playfieldSize * CELLSIZE / 2), + i * CELLSIZE + (MsG.HEIGTH / 2 - playfieldSize * CELLSIZE / 2), CELLSIZE, CELLSIZE); + MsG.add(playfield[i][j]); + + for (int k = 0; k < bPlacement.length; k++) { + if (bPlacement[k] == i * playfieldSize + j) { + playfield[i][j].type = CellType.Bomb; + break; + } + } + } + } + } + +} diff --git a/src/test/java/Minesweeper/MinesweeperGameTest.java b/src/test/java/Minesweeper/MinesweeperGameTest.java index fe10af4..2564d00 100644 --- a/src/test/java/Minesweeper/MinesweeperGameTest.java +++ b/src/test/java/Minesweeper/MinesweeperGameTest.java @@ -17,7 +17,7 @@ class MinesweeperGameTest { int bombCounter = 0; - for(Cell[] row : m.playfield){ + for(Cell[] row : m.playfield.playfield){ for (Cell c : row) { if(c.type == CellType.Bomb) { bombCounter++;