From 88de22364d8695e95d25d628e30184fd3c4a3dac Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 16:49:01 +0100 Subject: [PATCH 01/36] Add class MinesweeperGame with basic playfield gen. --- .gitignore | 21 +++++++++++ .../java/Minesweeper/MinesweeperGame.java | 36 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/test/java/Minesweeper/MinesweeperGame.java diff --git a/.gitignore b/.gitignore index 84adb3f..cfcc9bc 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,24 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Eclipse m2e generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# End of https://www.toptal.com/developers/gitignore/api/maven \ No newline at end of file diff --git a/src/test/java/Minesweeper/MinesweeperGame.java b/src/test/java/Minesweeper/MinesweeperGame.java new file mode 100644 index 0000000..8285a98 --- /dev/null +++ b/src/test/java/Minesweeper/MinesweeperGame.java @@ -0,0 +1,36 @@ +package Minesweeper; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +public class MinesweeperGame extends JPanel { + + public MinesweeperGame(int _playfieldSize) { + this.setSize(600, 600); + setLayout(null); + initPlayfield(_playfieldSize); + } + + private void initPlayfield(int _playfieldSize) { + for (int i = 0; i < _playfieldSize; i++) { + for (int j = 0; j < _playfieldSize; j++) { + JButton b = new JButton(); + b.setBounds(j * 40, i * 40, 40, 40); + add(b); + } + } + } + + public static void main(String[] args) { + JFrame f = new JFrame(); + MinesweeperGame ttt = new MinesweeperGame(8); + + f.add(ttt); + f.setSize(600, 600); + f.setLayout(null); + f.setVisible(true); + + } + +} From 00b6ab308c03c679c1154db02099f91fe58ddc6e Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 16:56:45 +0100 Subject: [PATCH 02/36] Refactore MinesweeperGame --- .../java/Minesweeper/MinesweeperGame.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/test/java/Minesweeper/MinesweeperGame.java b/src/test/java/Minesweeper/MinesweeperGame.java index 8285a98..a7764e3 100644 --- a/src/test/java/Minesweeper/MinesweeperGame.java +++ b/src/test/java/Minesweeper/MinesweeperGame.java @@ -6,17 +6,24 @@ 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; + public MinesweeperGame(int _playfieldSize) { - this.setSize(600, 600); + this.setSize(WIDTH, HEIGTH); + playfieldSize = _playfieldSize; setLayout(null); - initPlayfield(_playfieldSize); + + initPlayfield(); } - private void initPlayfield(int _playfieldSize) { - for (int i = 0; i < _playfieldSize; i++) { - for (int j = 0; j < _playfieldSize; j++) { + private void initPlayfield() { + for (int i = 0; i < playfieldSize; i++) { + for (int j = 0; j < playfieldSize; j++) { JButton b = new JButton(); - b.setBounds(j * 40, i * 40, 40, 40); + b.setBounds(j * CELLSIZE, i * CELLSIZE, CELLSIZE, CELLSIZE); add(b); } } @@ -27,7 +34,7 @@ public class MinesweeperGame extends JPanel { MinesweeperGame ttt = new MinesweeperGame(8); f.add(ttt); - f.setSize(600, 600); + f.setSize(WIDTH, HEIGTH); f.setLayout(null); f.setVisible(true); From 44a39db750e8dc5499aa13f884c0476594daef15 Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 18:17:21 +0100 Subject: [PATCH 03/36] Add class Cell --- src/main/java/Minesweeper/Cell.java | 15 +++++++++++++++ .../java/Minesweeper/MinesweeperGame.java | 0 2 files changed, 15 insertions(+) create mode 100644 src/main/java/Minesweeper/Cell.java rename src/{test => main}/java/Minesweeper/MinesweeperGame.java (100%) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java new file mode 100644 index 0000000..594c53d --- /dev/null +++ b/src/main/java/Minesweeper/Cell.java @@ -0,0 +1,15 @@ +package Minesweeper; + +enum CellType{Number, Bomb} +public class Cell { + public CellType type; + public boolean flagged = false; + public int value = -1; + + public Cell(CellType _type) { + type = _type; + } + + + +} diff --git a/src/test/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java similarity index 100% rename from src/test/java/Minesweeper/MinesweeperGame.java rename to src/main/java/Minesweeper/MinesweeperGame.java From 665d7c123240023c3e7117b7b5034c3620477464 Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 18:27:51 +0100 Subject: [PATCH 04/36] Add Cell to Minesweeper, replacing JButtons / Move playfield to center --- src/main/java/Minesweeper/Cell.java | 9 +++++---- src/main/java/Minesweeper/MinesweeperGame.java | 14 ++++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 594c53d..12a1935 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -1,7 +1,11 @@ package Minesweeper; +import javax.swing.JButton; + enum CellType{Number, Bomb} -public class Cell { +public class Cell extends JButton { + + private static final long serialVersionUID = 1L; public CellType type; public boolean flagged = false; public int value = -1; @@ -9,7 +13,4 @@ public class Cell { public Cell(CellType _type) { type = _type; } - - - } diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index a7764e3..8be5353 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -11,20 +11,26 @@ public class MinesweeperGame extends JPanel { private static final int CELLSIZE = 40; private int playfieldSize; + public Cell[][] playfield; + public MinesweeperGame(int _playfieldSize) { this.setSize(WIDTH, HEIGTH); playfieldSize = _playfieldSize; setLayout(null); - + initPlayfield(); } private void initPlayfield() { + + playfield = new Cell[playfieldSize][playfieldSize]; + for (int i = 0; i < playfieldSize; i++) { for (int j = 0; j < playfieldSize; j++) { - JButton b = new JButton(); - b.setBounds(j * CELLSIZE, i * CELLSIZE, CELLSIZE, CELLSIZE); - add(b); + Cell c = new Cell(CellType.Number); + c.setBounds(j * CELLSIZE + (WIDTH / 2 - playfieldSize * CELLSIZE / 2), i * CELLSIZE + (HEIGTH / 2 - playfieldSize * CELLSIZE / 2), CELLSIZE, + CELLSIZE); + add(c); } } } From 1dc0576f7b3eb855928953e168697f36f4d0905e Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 11:13:53 +0100 Subject: [PATCH 05/36] Add bomb placement --- .../java/Minesweeper/MinesweeperGame.java | 34 ++++++++++++--- .../java/Minesweeper/MinesweeperGameTest.java | 41 +++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 src/test/java/Minesweeper/MinesweeperGameTest.java diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index 8be5353..ffb4a10 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -10,12 +10,14 @@ public class MinesweeperGame extends JPanel { 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) { + public MinesweeperGame(int _playfieldSize, int _bombAmount) { this.setSize(WIDTH, HEIGTH); playfieldSize = _playfieldSize; + bombAmount = _bombAmount; setLayout(null); initPlayfield(); @@ -25,19 +27,39 @@ public class MinesweeperGame extends JPanel { 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++) { - Cell c = new Cell(CellType.Number); - c.setBounds(j * CELLSIZE + (WIDTH / 2 - playfieldSize * CELLSIZE / 2), i * CELLSIZE + (HEIGTH / 2 - playfieldSize * CELLSIZE / 2), CELLSIZE, - CELLSIZE); - add(c); + 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); + MinesweeperGame ttt = new MinesweeperGame(8, 10); f.add(ttt); f.setSize(WIDTH, HEIGTH); diff --git a/src/test/java/Minesweeper/MinesweeperGameTest.java b/src/test/java/Minesweeper/MinesweeperGameTest.java new file mode 100644 index 0000000..fe10af4 --- /dev/null +++ b/src/test/java/Minesweeper/MinesweeperGameTest.java @@ -0,0 +1,41 @@ +package Minesweeper; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class MinesweeperGameTest { + + @ParameterizedTest + @MethodSource("testBombs") + void testBombPlacement(int _playfieldSize, int _bombAmount) { + MinesweeperGame m = new MinesweeperGame(_playfieldSize, _bombAmount); + + int bombCounter = 0; + + for(Cell[] row : m.playfield){ + for (Cell c : row) { + if(c.type == CellType.Bomb) { + bombCounter++; + } + } + } + assertEquals(_bombAmount, bombCounter); + } + + + private static Stream testBombs(){ + return Stream.of( + Arguments.of(8, 10), + Arguments.of(8, 0), + Arguments.of(4, 12), + Arguments.of(10, 100), + Arguments.of(5, 1) + ); + } + +} From aa04d679bd0d4f92df6725c2883db57372601dd8 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 11:36:45 +0100 Subject: [PATCH 06/36] 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++; From 380e848e38450c1af397f44392242ba7db982dd8 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 11:40:31 +0100 Subject: [PATCH 07/36] Refactore Playfield --- src/main/java/Minesweeper/Playfield.java | 28 +++++++++---------- .../java/Minesweeper/MinesweeperGameTest.java | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 013a0bb..aa843a6 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -3,19 +3,19 @@ package Minesweeper; public class Playfield { private static final int CELLSIZE = 40; - private int playfieldSize; + private int Size; private MinesweeperGame MsG; - public Cell[][] playfield; + public Cell[][] cells; - public Playfield(MinesweeperGame _MsG, int _playfieldSize, int _bombAmount) { + public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) { MsG = _MsG; - playfieldSize = _playfieldSize; + Size = _Size; - playfield = new Cell[playfieldSize][playfieldSize]; + cells = new Cell[Size][Size]; int[] bPlacement = new int[_bombAmount]; for (int i = 0; i < bPlacement.length; i++) { - bPlacement[i] = (int) (Math.random() * playfieldSize * playfieldSize); + bPlacement[i] = (int) (Math.random() * Size * Size); for (int j = 0; j < i; j++) { if (bPlacement[i] == bPlacement[j]) { @@ -25,17 +25,17 @@ public class Playfield { } } - for (int i = 0; i < playfieldSize; i++) { - for (int j = 0; j < playfieldSize; j++) { - playfield[i][j] = new Cell(CellType.Number); + for (int i = 0; i < Size; i++) { + for (int j = 0; j < Size; j++) { + cells[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]); + cells[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - Size * CELLSIZE / 2), + i * CELLSIZE + (MsG.HEIGTH / 2 - Size * CELLSIZE / 2), CELLSIZE, CELLSIZE); + MsG.add(cells[i][j]); for (int k = 0; k < bPlacement.length; k++) { - if (bPlacement[k] == i * playfieldSize + j) { - playfield[i][j].type = CellType.Bomb; + if (bPlacement[k] == i * Size + j) { + cells[i][j].type = CellType.Bomb; break; } } diff --git a/src/test/java/Minesweeper/MinesweeperGameTest.java b/src/test/java/Minesweeper/MinesweeperGameTest.java index 2564d00..2dce81d 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.playfield){ + for(Cell[] row : m.playfield.cells){ for (Cell c : row) { if(c.type == CellType.Bomb) { bombCounter++; From 0e2ea58e5721989641caa2f74419a3a8390c86c2 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 12:56:43 +0100 Subject: [PATCH 08/36] Add bomb proximity with test --- src/main/java/Minesweeper/Cell.java | 2 +- .../java/Minesweeper/MinesweeperGame.java | 3 +- src/main/java/Minesweeper/Playfield.java | 62 ++++++++++++++++++- .../java/Minesweeper/MinesweeperGameTest.java | 12 ++++ 4 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 12a1935..36946f8 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -8,7 +8,7 @@ public class Cell extends JButton { private static final long serialVersionUID = 1L; public CellType type; public boolean flagged = false; - public int value = -1; + public int value = 0; public Cell(CellType _type) { type = _type; diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index df5d33c..fde06cd 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -8,7 +8,6 @@ public class MinesweeperGame extends JPanel { private static final long serialVersionUID = 1L; public static final int WIDTH = 600, HEIGTH = 600; - public Playfield playfield; public MinesweeperGame(int _playfieldSize, int _bombAmount) { @@ -19,7 +18,7 @@ public class MinesweeperGame extends JPanel { public static void main(String[] args) { JFrame f = new JFrame(); - MinesweeperGame ttt = new MinesweeperGame(8, 10); + MinesweeperGame ttt = new MinesweeperGame(3, 0); f.add(ttt); f.setSize(WIDTH, HEIGTH); diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index aa843a6..a56dbc6 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -2,7 +2,7 @@ package Minesweeper; public class Playfield { - private static final int CELLSIZE = 40; + private static final int CELLSIZE = 50; private int Size; private MinesweeperGame MsG; public Cell[][] cells; @@ -10,7 +10,7 @@ public class Playfield { public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) { MsG = _MsG; Size = _Size; - + cells = new Cell[Size][Size]; int[] bPlacement = new int[_bombAmount]; @@ -41,6 +41,64 @@ public class Playfield { } } } + + for (int i = 0; i < Size; i++) { + for (int j = 0; j < Size; j++) { + if (cells[i][j].type == CellType.Number) { + calculateBombProximity(i, j); + } + } + } + } + + public void calculateBombProximity(int row, int column) { + + if (row > 0) { + if (column > 0) { + if (cells[row - 1][column - 1].type == CellType.Bomb) { + cells[row][column].value++; + } + } + + if (cells[row - 1][column].type == CellType.Bomb) { + cells[row][column].value++; + } + + if (column < cells.length - 1) { + if (cells[row - 1][column + 1].type == CellType.Bomb) { + cells[row][column].value++; + } + } + } + + if (row < cells.length - 1) { + if (column > 0) { + if (cells[row + 1][column - 1].type == CellType.Bomb) { + cells[row][column].value++; + } + } + + if (cells[row + 1][column].type == CellType.Bomb) { + cells[row][column].value++; + } + + if (column < cells.length - 1) { + if (cells[row + 1][column + 1].type == CellType.Bomb) { + cells[row][column].value++; + } + } + } + + if (column > 0) { + if (cells[row][column - 1].type == CellType.Bomb) { + cells[row][column].value++; + } + } + if (column < cells.length - 1) { + if (cells[row][column + 1].type == CellType.Bomb) { + cells[row][column].value++; + } + } } } diff --git a/src/test/java/Minesweeper/MinesweeperGameTest.java b/src/test/java/Minesweeper/MinesweeperGameTest.java index 2dce81d..4b61967 100644 --- a/src/test/java/Minesweeper/MinesweeperGameTest.java +++ b/src/test/java/Minesweeper/MinesweeperGameTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.*; import java.util.stream.Stream; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -27,6 +28,15 @@ class MinesweeperGameTest { assertEquals(_bombAmount, bombCounter); } + @Test + void testProximityPoints() { + MinesweeperGame m = new MinesweeperGame(3, 0); + m.playfield.cells[0][0].type = CellType.Bomb; + m.playfield.calculateBombProximity(1, 1); + + assertEquals(1, m.playfield.cells[1][1].value); + } + private static Stream testBombs(){ return Stream.of( @@ -38,4 +48,6 @@ class MinesweeperGameTest { ); } + + } From cc10c4fdde4576d56b5ae1957c7619fe8813fb36 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 13:47:23 +0100 Subject: [PATCH 09/36] Show proximity values on cells --- src/main/java/Minesweeper/Cell.java | 8 ++++++++ src/main/java/Minesweeper/MinesweeperGame.java | 2 +- src/main/java/Minesweeper/Playfield.java | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 36946f8..a455e88 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -13,4 +13,12 @@ public class Cell extends JButton { public Cell(CellType _type) { type = _type; } + + public void update() { + if(type == CellType.Number) { + setText(String.valueOf(value)); + } else { + + } + } } diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index fde06cd..231c674 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -18,7 +18,7 @@ public class MinesweeperGame extends JPanel { public static void main(String[] args) { JFrame f = new JFrame(); - MinesweeperGame ttt = new MinesweeperGame(3, 0); + MinesweeperGame ttt = new MinesweeperGame(8, 10); f.add(ttt); f.setSize(WIDTH, HEIGTH); diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index a56dbc6..9f52d84 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -99,6 +99,8 @@ public class Playfield { cells[row][column].value++; } } + + cells[row][column].update(); } } From 4d97b75dbae0cc782d06b6061d65c46d4db80ce8 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 14:40:07 +0100 Subject: [PATCH 10/36] Add floodfill on click --- src/main/java/Minesweeper/Cell.java | 81 +++++++++++++++++++++--- src/main/java/Minesweeper/Playfield.java | 8 ++- 2 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index a455e88..c2a3f33 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -1,24 +1,89 @@ package Minesweeper; +import java.awt.Color; +import java.awt.Point; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + import javax.swing.JButton; -enum CellType{Number, Bomb} +enum CellType { + Number, Bomb +} + public class Cell extends JButton { - + private static final long serialVersionUID = 1L; + private Playfield playfield; + public CellType type; + public Point cord; public boolean flagged = false; public int value = 0; - - public Cell(CellType _type) { + + public Cell(CellType _type, Playfield _playfield, Point _cord) { type = _type; + cord = _cord; + playfield = _playfield; + + addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + OnMouseClick(); + } + }); } - + + protected void OnMouseClick() { + if (type != CellType.Bomb) { + flood(); + } + } + public void update() { - if(type == CellType.Number) { + if (type == CellType.Number) { setText(String.valueOf(value)); - } else { - } } + + public void flood() { + if (type == CellType.Bomb) { + return; + } + setBackground(Color.LIGHT_GRAY); + setEnabled(false); + + if (value == 0) { + if (cord.y > 0) { + if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number + && playfield.cells[cord.y - 1][cord.x].isEnabled()) { + playfield.cells[cord.y - 1][cord.x].flood(); + } + } + + if (cord.x < playfield.Size - 1) { + if (playfield.cells[cord.y][cord.x + 1].type == CellType.Number + && playfield.cells[cord.y][cord.x + 1].isEnabled()) { + playfield.cells[cord.y][cord.x + 1].flood(); + } + } + + if (cord.y < playfield.Size - 1) { + if (playfield.cells[cord.y + 1][cord.x].type == CellType.Number + && playfield.cells[cord.y + 1][cord.x].isEnabled()) { + playfield.cells[cord.y + 1][cord.x].flood(); + } + } + + if (cord.x > 0) { + if (playfield.cells[cord.y][cord.x - 1].type == CellType.Number + && playfield.cells[cord.y][cord.x - 1].isEnabled()) { + playfield.cells[cord.y][cord.x - 1].flood(); + } + } + + } + + } + } diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 9f52d84..1cefcbf 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -1,9 +1,11 @@ package Minesweeper; +import java.awt.Point; + public class Playfield { private static final int CELLSIZE = 50; - private int Size; + public int Size; private MinesweeperGame MsG; public Cell[][] cells; @@ -27,7 +29,7 @@ public class Playfield { for (int i = 0; i < Size; i++) { for (int j = 0; j < Size; j++) { - cells[i][j] = new Cell(CellType.Number); + cells[i][j] = new Cell(CellType.Number, this, new Point(j, i)); cells[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - Size * CELLSIZE / 2), i * CELLSIZE + (MsG.HEIGTH / 2 - Size * CELLSIZE / 2), CELLSIZE, CELLSIZE); @@ -99,7 +101,7 @@ public class Playfield { cells[row][column].value++; } } - + cells[row][column].update(); } From b51cfb67323aa4f1441ec2f4b4a255a72a340753 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 14:48:48 +0100 Subject: [PATCH 11/36] Change update for celltype bomb --- src/main/java/Minesweeper/Cell.java | 2 ++ src/main/java/Minesweeper/Playfield.java | 1 + 2 files changed, 3 insertions(+) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index c2a3f33..7b5e0de 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -43,6 +43,8 @@ public class Cell extends JButton { public void update() { if (type == CellType.Number) { setText(String.valueOf(value)); + } else { + setBackground(Color.RED); } } diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 1cefcbf..14f0a81 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -38,6 +38,7 @@ public class Playfield { for (int k = 0; k < bPlacement.length; k++) { if (bPlacement[k] == i * Size + j) { cells[i][j].type = CellType.Bomb; + cells[i][j].update(); break; } } From 30903220e9ecc89c297f1add1b0fe707e0af7f9c Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 14:53:51 +0100 Subject: [PATCH 12/36] Refactore Playfield --- src/main/java/Minesweeper/MinesweeperGame.java | 3 ++- src/main/java/Minesweeper/Playfield.java | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index 231c674..c3b9af4 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -24,7 +24,8 @@ public class MinesweeperGame extends JPanel { f.setSize(WIDTH, HEIGTH); f.setLayout(null); f.setVisible(true); - } + + } diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 14f0a81..3fc9006 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -12,6 +12,10 @@ public class Playfield { public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) { MsG = _MsG; Size = _Size; + generatePlayfield(_bombAmount); + } + + public void generatePlayfield(int _bombAmount) { cells = new Cell[Size][Size]; @@ -53,7 +57,7 @@ public class Playfield { } } } - + public void calculateBombProximity(int row, int column) { if (row > 0) { From d718f86c52e7de7c05d24980d9c10bfeb16c7229 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 14:58:58 +0100 Subject: [PATCH 13/36] Made playfield resetable --- src/main/java/Minesweeper/Playfield.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 3fc9006..eed046b 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -8,18 +8,20 @@ public class Playfield { public int Size; private MinesweeperGame MsG; public Cell[][] cells; + private int bombAmount; public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) { MsG = _MsG; Size = _Size; - generatePlayfield(_bombAmount); + bombAmount = _bombAmount; + generatePlayfield(); } - public void generatePlayfield(int _bombAmount) { + public void generatePlayfield() { cells = new Cell[Size][Size]; - int[] bPlacement = new int[_bombAmount]; + int[] bPlacement = new int[bombAmount]; for (int i = 0; i < bPlacement.length; i++) { bPlacement[i] = (int) (Math.random() * Size * Size); @@ -57,7 +59,16 @@ public class Playfield { } } } - + + public void reset() { + for (int i = 0; i < Size; i++) { + for (int j = 0; j < Size; j++) { + MsG.remove(cells[i][j]); + } + } + generatePlayfield(); + } + public void calculateBombProximity(int row, int column) { if (row > 0) { From 4cbfc45a3de3a47c7f78663b11aa993c009e353c Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 15:02:28 +0100 Subject: [PATCH 14/36] Game End: Clicked on Mine --- src/main/java/Minesweeper/Cell.java | 2 ++ src/main/java/Minesweeper/Playfield.java | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 7b5e0de..93f8799 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -37,6 +37,8 @@ public class Cell extends JButton { protected void OnMouseClick() { if (type != CellType.Bomb) { flood(); + } else { + playfield.reset(); } } diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index eed046b..38f61cc 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -2,6 +2,8 @@ package Minesweeper; import java.awt.Point; +import javax.swing.JOptionPane; + public class Playfield { private static final int CELLSIZE = 50; @@ -61,6 +63,7 @@ public class Playfield { } public void reset() { + JOptionPane.showMessageDialog(MsG,"KABOOM! Try again!"); for (int i = 0; i < Size; i++) { for (int j = 0; j < Size; j++) { MsG.remove(cells[i][j]); From b635c64d4f88c7e25074ab31e0272da5e341ccc8 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 15:12:36 +0100 Subject: [PATCH 15/36] Game End: Victory --- src/main/java/Minesweeper/Cell.java | 2 +- src/main/java/Minesweeper/Playfield.java | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 93f8799..fc7d393 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -56,7 +56,7 @@ public class Cell extends JButton { } setBackground(Color.LIGHT_GRAY); setEnabled(false); - + playfield.cellFlooded(); if (value == 0) { if (cord.y > 0) { if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 38f61cc..c67c570 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -11,7 +11,9 @@ public class Playfield { private MinesweeperGame MsG; public Cell[][] cells; private int bombAmount; - + private int cellsFlooded = 0; + + public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) { MsG = _MsG; Size = _Size; @@ -123,5 +125,12 @@ public class Playfield { cells[row][column].update(); } - + + public void cellFlooded() { + cellsFlooded++; + if(cellsFlooded >= Size * Size - bombAmount) { + JOptionPane.showMessageDialog(MsG, "You won, congratulations!"); + System.exit(0); + } + } } From d0594cba7ed93ca3c1805643c5e53b752cfbf5bd Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 15:55:18 +0100 Subject: [PATCH 16/36] Flagging on right click --- src/main/java/Minesweeper/Cell.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index fc7d393..80e9aac 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -4,6 +4,8 @@ import java.awt.Color; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import javax.swing.JButton; @@ -32,6 +34,17 @@ public class Cell extends JButton { OnMouseClick(); } }); + + addMouseListener(new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + // TODO Auto-generated method stub + super.mousePressed(e); + if (e.getButton() == 3) { + OnMouseRightClick(); + } + } + }); } protected void OnMouseClick() { @@ -42,6 +55,14 @@ public class Cell extends JButton { } } + protected void OnMouseRightClick() { + if (flagged) { + flagged = false; + } else { + flagged = true; + } + } + public void update() { if (type == CellType.Number) { setText(String.valueOf(value)); From 3c6f092049c6cb45420b50c9b660e5731234c1e4 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 16:03:42 +0100 Subject: [PATCH 17/36] Add flagging logic to cell --- src/main/java/Minesweeper/Cell.java | 33 +++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 80e9aac..9f6cc0c 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -27,7 +27,8 @@ public class Cell extends JButton { type = _type; cord = _cord; playfield = _playfield; - + + setBackground(Color.white); addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -48,18 +49,30 @@ public class Cell extends JButton { } protected void OnMouseClick() { - if (type != CellType.Bomb) { - flood(); - } else { - playfield.reset(); + if (!flagged) { + if (type != CellType.Bomb) { + flood(); + } else { + playfield.reset(); + } } } protected void OnMouseRightClick() { - if (flagged) { - flagged = false; - } else { - flagged = true; + if (isEnabled()) { + if (flagged) { + flagged = false; + + if (type == CellType.Number) { + setBackground(Color.gray); + } else { + setBackground(Color.red); + } + + } else { + flagged = true; + setBackground(Color.cyan); + } } } @@ -75,9 +88,11 @@ public class Cell extends JButton { if (type == CellType.Bomb) { return; } + setBackground(Color.LIGHT_GRAY); setEnabled(false); playfield.cellFlooded(); + if (value == 0) { if (cord.y > 0) { if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number From d6178355ded68961cde36c2aa2244483d1b61fa1 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 16:06:34 +0100 Subject: [PATCH 18/36] Bugfix early victory --- src/main/java/Minesweeper/Playfield.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index c67c570..2a1806e 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -65,7 +65,8 @@ public class Playfield { } public void reset() { - JOptionPane.showMessageDialog(MsG,"KABOOM! Try again!"); + JOptionPane.showMessageDialog(MsG,"KABOOM! Try again!"); + cellsFlooded = 0; for (int i = 0; i < Size; i++) { for (int j = 0; j < Size; j++) { MsG.remove(cells[i][j]); From 7de023fa3e0e4bcca4258ca62b056841e34de105 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 16:30:00 +0100 Subject: [PATCH 19/36] Flagged cells will not be flooded --- src/main/java/Minesweeper/Cell.java | 2 +- src/main/java/Minesweeper/MinesweeperGame.java | 5 +---- src/main/java/Minesweeper/Playfield.java | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 9f6cc0c..d67ba5b 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -85,7 +85,7 @@ public class Cell extends JButton { } public void flood() { - if (type == CellType.Bomb) { + if (type == CellType.Bomb || flagged) { return; } diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index c3b9af4..9570e80 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -25,7 +25,4 @@ public class MinesweeperGame extends JPanel { f.setLayout(null); f.setVisible(true); } - - - -} +} \ No newline at end of file diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 2a1806e..754dd52 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -134,4 +134,4 @@ public class Playfield { System.exit(0); } } -} +} \ No newline at end of file From 5d1aba44082f7911b0dd17ab58ac4669e9721551 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 16:38:16 +0100 Subject: [PATCH 20/36] Flagged number cells count as flooded --- src/main/java/Minesweeper/Cell.java | 8 ++++++-- src/main/java/Minesweeper/Playfield.java | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index d67ba5b..8b05e78 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -62,16 +62,20 @@ public class Cell extends JButton { if (isEnabled()) { if (flagged) { flagged = false; - + if (type == CellType.Number) { setBackground(Color.gray); + playfield.cellDried(); } else { setBackground(Color.red); } - + } else { flagged = true; setBackground(Color.cyan); + if (type == CellType.Number) { + playfield.cellFlooded(); + } } } } diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 754dd52..86d14fb 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -134,4 +134,8 @@ public class Playfield { System.exit(0); } } + + public void cellDried() { + cellsFlooded--; + } } \ No newline at end of file From c6c2328e4ac5f8a1082b6d3f735be23d6e309772 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 18:32:48 +0100 Subject: [PATCH 21/36] Changed victory end to reset --- src/main/java/Minesweeper/Cell.java | 2 ++ src/main/java/Minesweeper/Playfield.java | 12 +++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 8b05e78..8311365 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -8,6 +8,7 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; +import javax.swing.JOptionPane; enum CellType { Number, Bomb @@ -53,6 +54,7 @@ public class Cell extends JButton { if (type != CellType.Bomb) { flood(); } else { + JOptionPane.showMessageDialog(getParent(),"KABOOM! Try again!"); playfield.reset(); } } diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 86d14fb..f757df8 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -12,8 +12,7 @@ public class Playfield { public Cell[][] cells; private int bombAmount; private int cellsFlooded = 0; - - + public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) { MsG = _MsG; Size = _Size; @@ -65,7 +64,6 @@ public class Playfield { } public void reset() { - JOptionPane.showMessageDialog(MsG,"KABOOM! Try again!"); cellsFlooded = 0; for (int i = 0; i < Size; i++) { for (int j = 0; j < Size; j++) { @@ -126,15 +124,15 @@ public class Playfield { cells[row][column].update(); } - + public void cellFlooded() { cellsFlooded++; - if(cellsFlooded >= Size * Size - bombAmount) { + if (cellsFlooded >= Size * Size - bombAmount) { JOptionPane.showMessageDialog(MsG, "You won, congratulations!"); - System.exit(0); + reset(); } } - + public void cellDried() { cellsFlooded--; } From 430ff0922847aeba9019d8da62d9315a70a6cad3 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 18:49:51 +0100 Subject: [PATCH 22/36] Add reveal cell function --- src/main/java/Minesweeper/Cell.java | 4 +++- src/main/java/Minesweeper/Playfield.java | 5 ++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 8311365..31c0e7b 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -51,12 +51,14 @@ public class Cell extends JButton { protected void OnMouseClick() { if (!flagged) { + reveal(); if (type != CellType.Bomb) { flood(); } else { JOptionPane.showMessageDialog(getParent(),"KABOOM! Try again!"); playfield.reset(); } + } } @@ -82,7 +84,7 @@ public class Cell extends JButton { } } - public void update() { + public void reveal() { if (type == CellType.Number) { setText(String.valueOf(value)); } else { diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index f757df8..f2036df 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -1,5 +1,6 @@ package Minesweeper; +import java.awt.Color; import java.awt.Point; import javax.swing.JOptionPane; @@ -43,11 +44,9 @@ public class Playfield { cells[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - Size * CELLSIZE / 2), i * CELLSIZE + (MsG.HEIGTH / 2 - Size * CELLSIZE / 2), CELLSIZE, CELLSIZE); MsG.add(cells[i][j]); - for (int k = 0; k < bPlacement.length; k++) { if (bPlacement[k] == i * Size + j) { cells[i][j].type = CellType.Bomb; - cells[i][j].update(); break; } } @@ -61,6 +60,7 @@ public class Playfield { } } } + MsG.repaint(); } public void reset() { @@ -122,7 +122,6 @@ public class Playfield { } } - cells[row][column].update(); } public void cellFlooded() { From 7c6706bc2021cf331232402e90e61a829cad8477 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 18:59:03 +0100 Subject: [PATCH 23/36] Changed Colors --- src/main/java/Minesweeper/Cell.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 31c0e7b..2252b7d 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -16,6 +16,10 @@ enum CellType { public class Cell extends JButton { + private static final Color FLAGCOLOR = Color.RED; + private static final Color FLOODEDCOLOR = Color.LIGHT_GRAY; + private static final Color HIDDENCOLOR = Color.GRAY; + private static final Color MINECOLOR = Color.BLACK; private static final long serialVersionUID = 1L; private Playfield playfield; @@ -68,15 +72,15 @@ public class Cell extends JButton { flagged = false; if (type == CellType.Number) { - setBackground(Color.gray); + setBackground(HIDDENCOLOR); playfield.cellDried(); } else { - setBackground(Color.red); + setBackground(MINECOLOR); } } else { flagged = true; - setBackground(Color.cyan); + setBackground(FLAGCOLOR); if (type == CellType.Number) { playfield.cellFlooded(); } @@ -88,7 +92,7 @@ public class Cell extends JButton { if (type == CellType.Number) { setText(String.valueOf(value)); } else { - setBackground(Color.RED); + setBackground(MINECOLOR); } } @@ -97,7 +101,7 @@ public class Cell extends JButton { return; } - setBackground(Color.LIGHT_GRAY); + setBackground(FLOODEDCOLOR); setEnabled(false); playfield.cellFlooded(); From 02c2b01d95728a768c63d6a87f08795f76009ddf Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 19:01:56 +0100 Subject: [PATCH 24/36] Reveal all bombs on game end --- src/main/java/Minesweeper/Cell.java | 1 + src/main/java/Minesweeper/Playfield.java | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 2252b7d..4991e9e 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -59,6 +59,7 @@ public class Cell extends JButton { if (type != CellType.Bomb) { flood(); } else { + playfield.revealAllBombs(); JOptionPane.showMessageDialog(getParent(),"KABOOM! Try again!"); playfield.reset(); } diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index f2036df..372b715 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -127,11 +127,23 @@ public class Playfield { public void cellFlooded() { cellsFlooded++; if (cellsFlooded >= Size * Size - bombAmount) { + revealAllBombs(); JOptionPane.showMessageDialog(MsG, "You won, congratulations!"); reset(); } } + public void revealAllBombs() { + for (int i = 0; i < Size; i++) { + for (int j = 0; j < Size; j++) { + if(cells[i][j].type == CellType.Bomb) { + cells[i][j].reveal(); + } + } + } + MsG.repaint(); + } + public void cellDried() { cellsFlooded--; } From 968c93804d6a04b6ccafdecac4122c21c468bc11 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 19:05:16 +0100 Subject: [PATCH 25/36] Refactore Minesweeper, Playfield --- src/main/java/Minesweeper/MinesweeperGame.java | 5 ++--- src/main/java/Minesweeper/Playfield.java | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index 9570e80..8293a18 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -1,6 +1,5 @@ package Minesweeper; -import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; @@ -18,9 +17,9 @@ public class MinesweeperGame extends JPanel { public static void main(String[] args) { JFrame f = new JFrame(); - MinesweeperGame ttt = new MinesweeperGame(8, 10); + MinesweeperGame MsG = new MinesweeperGame(8, 10); - f.add(ttt); + f.add(MsG); f.setSize(WIDTH, HEIGTH); f.setLayout(null); f.setVisible(true); diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 372b715..9e73cb8 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -1,6 +1,5 @@ package Minesweeper; -import java.awt.Color; import java.awt.Point; import javax.swing.JOptionPane; From 5c365de0d2eba3af035a38bfa2ca7210cb4b64c5 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 19:17:40 +0100 Subject: [PATCH 26/36] .gitignore fix --- .gitignore | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/.gitignore b/.gitignore index 84adb3f..cfcc9bc 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,24 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Eclipse m2e generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# End of https://www.toptal.com/developers/gitignore/api/maven \ No newline at end of file From 9c2de1f2f7ef7c6630a2bf9cfe10fde1dfdeb24b Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 19:44:48 +0100 Subject: [PATCH 27/36] Bugfix numbers not showen --- src/main/java/Minesweeper/Cell.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 4991e9e..f8535eb 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -91,7 +91,9 @@ public class Cell extends JButton { public void reveal() { if (type == CellType.Number) { - setText(String.valueOf(value)); + if(value > 0) { + setText(String.valueOf(value)); + } } else { setBackground(MINECOLOR); } @@ -104,6 +106,7 @@ public class Cell extends JButton { setBackground(FLOODEDCOLOR); setEnabled(false); + reveal(); playfield.cellFlooded(); if (value == 0) { From 896413211c40fcb697d435dca1435d5e0709e27f Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 19:47:31 +0100 Subject: [PATCH 28/36] Bugfix worng colors on unflag --- src/main/java/Minesweeper/Cell.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index f8535eb..aa1a65f 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -18,7 +18,7 @@ public class Cell extends JButton { private static final Color FLAGCOLOR = Color.RED; private static final Color FLOODEDCOLOR = Color.LIGHT_GRAY; - private static final Color HIDDENCOLOR = Color.GRAY; + private static final Color HIDDENCOLOR = Color.WHITE; private static final Color MINECOLOR = Color.BLACK; private static final long serialVersionUID = 1L; private Playfield playfield; @@ -33,7 +33,7 @@ public class Cell extends JButton { cord = _cord; playfield = _playfield; - setBackground(Color.white); + setBackground(HIDDENCOLOR); addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -72,11 +72,9 @@ public class Cell extends JButton { if (flagged) { flagged = false; + setBackground(HIDDENCOLOR); if (type == CellType.Number) { - setBackground(HIDDENCOLOR); playfield.cellDried(); - } else { - setBackground(MINECOLOR); } } else { From f902c3550b2ab4c37492b10a89dc97a195d98b7a Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:02:43 +0100 Subject: [PATCH 29/36] Add TimerLable class with basic logic --- .../java/Minesweeper/MinesweeperGame.java | 2 +- src/main/java/Minesweeper/TimerLable.java | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Minesweeper/TimerLable.java diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index 8293a18..f31d319 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -12,7 +12,7 @@ public class MinesweeperGame extends JPanel { public MinesweeperGame(int _playfieldSize, int _bombAmount) { this.setSize(WIDTH, HEIGTH); setLayout(null); - playfield = new Playfield(this, _playfieldSize, _bombAmount ); + playfield = new Playfield(this, _playfieldSize, _bombAmount); } public static void main(String[] args) { diff --git a/src/main/java/Minesweeper/TimerLable.java b/src/main/java/Minesweeper/TimerLable.java new file mode 100644 index 0000000..a194bbf --- /dev/null +++ b/src/main/java/Minesweeper/TimerLable.java @@ -0,0 +1,38 @@ +package Minesweeper; + +import java.util.Timer; +import java.util.TimerTask; + +import javax.swing.JLabel; + +public class TimerLable extends JLabel { + + private static final long serialVersionUID = 1L; + private int counter = 0; + + public void start() { + Timer timer = new Timer(); + TimerTask task = new Helper(this); + + timer.schedule(task, 0, 1000); + } + + public void update() { + setText(String.valueOf(++counter)); + } +} + + +class Helper extends TimerTask +{ + public static int i = 0; + private TimerLable timerLable; + + public Helper(TimerLable _timerLable) { + timerLable = _timerLable; + } + public void run() + { + timerLable.update(); + } +} \ No newline at end of file From 1a8df9a1a9c3b145d5b618d763a50afa49b3f011 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:03:24 +0100 Subject: [PATCH 30/36] Display TimerLable --- src/main/java/Minesweeper/MinesweeperGame.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index f31d319..1741988 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -13,6 +13,10 @@ public class MinesweeperGame extends JPanel { this.setSize(WIDTH, HEIGTH); setLayout(null); playfield = new Playfield(this, _playfieldSize, _bombAmount); + TimerLable tl = new TimerLable(); + tl.setBounds((WIDTH / 2 - 5), HEIGTH / 2 - 240, 20, 20); + add(tl); + tl.start(); } public static void main(String[] args) { From 8128061c09986773d9a3c684ef4e9c77dca2f236 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:07:22 +0100 Subject: [PATCH 31/36] Add reset to TimerLable --- src/main/java/Minesweeper/TimerLable.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/Minesweeper/TimerLable.java b/src/main/java/Minesweeper/TimerLable.java index a194bbf..ae08055 100644 --- a/src/main/java/Minesweeper/TimerLable.java +++ b/src/main/java/Minesweeper/TimerLable.java @@ -8,11 +8,12 @@ import javax.swing.JLabel; public class TimerLable extends JLabel { private static final long serialVersionUID = 1L; - private int counter = 0; + protected int counter = 0; + private Helper task; public void start() { Timer timer = new Timer(); - TimerTask task = new Helper(this); + task = new Helper(this); timer.schedule(task, 0, 1000); } @@ -20,12 +21,18 @@ public class TimerLable extends JLabel { public void update() { setText(String.valueOf(++counter)); } + + public void reset() { + task.reset = true; + } + } class Helper extends TimerTask { - public static int i = 0; + public boolean reset; + public static int i = 0; private TimerLable timerLable; public Helper(TimerLable _timerLable) { @@ -33,6 +40,10 @@ class Helper extends TimerTask } public void run() { + if(reset) { + reset = false; + timerLable.counter = 0; + } timerLable.update(); } } \ No newline at end of file From 1a7908fa2a0b1e43882cdbb9f0eeee0530a5328e Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:09:28 +0100 Subject: [PATCH 32/36] Reset timer on game end --- src/main/java/Minesweeper/MinesweeperGame.java | 4 +++- src/main/java/Minesweeper/Playfield.java | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index 1741988..cf07347 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -8,12 +8,14 @@ public class MinesweeperGame extends JPanel { private static final long serialVersionUID = 1L; public static final int WIDTH = 600, HEIGTH = 600; public Playfield playfield; + public TimerLable tl; public MinesweeperGame(int _playfieldSize, int _bombAmount) { this.setSize(WIDTH, HEIGTH); setLayout(null); playfield = new Playfield(this, _playfieldSize, _bombAmount); - TimerLable tl = new TimerLable(); + + tl = new TimerLable(); tl.setBounds((WIDTH / 2 - 5), HEIGTH / 2 - 240, 20, 20); add(tl); tl.start(); diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 9e73cb8..425f4f2 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -69,6 +69,7 @@ public class Playfield { MsG.remove(cells[i][j]); } } + MsG.tl.reset(); generatePlayfield(); } From d6f1baffe9cc878be102568f9f0f9e038cafc0b1 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:18:46 +0100 Subject: [PATCH 33/36] Timer stops while game end dialog is open --- src/main/java/Minesweeper/Playfield.java | 1 + src/main/java/Minesweeper/TimerLable.java | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 425f4f2..29dde34 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -141,6 +141,7 @@ public class Playfield { } } } + MsG.tl.stop(); MsG.repaint(); } diff --git a/src/main/java/Minesweeper/TimerLable.java b/src/main/java/Minesweeper/TimerLable.java index ae08055..750e417 100644 --- a/src/main/java/Minesweeper/TimerLable.java +++ b/src/main/java/Minesweeper/TimerLable.java @@ -24,14 +24,19 @@ public class TimerLable extends JLabel { public void reset() { task.reset = true; + task.stop = false; } + public void stop() { + task.stop = true; + } } class Helper extends TimerTask { public boolean reset; + public boolean stop; public static int i = 0; private TimerLable timerLable; @@ -40,6 +45,9 @@ class Helper extends TimerTask } public void run() { + if(stop) { + return; + } if(reset) { reset = false; timerLable.counter = 0; From 520c5dc3a0a5320891b6b01374ee9101ca02c4bd Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:19:19 +0100 Subject: [PATCH 34/36] Bugfix timer reset --- src/main/java/Minesweeper/TimerLable.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/Minesweeper/TimerLable.java b/src/main/java/Minesweeper/TimerLable.java index 750e417..68a1ea9 100644 --- a/src/main/java/Minesweeper/TimerLable.java +++ b/src/main/java/Minesweeper/TimerLable.java @@ -24,6 +24,9 @@ public class TimerLable extends JLabel { public void reset() { task.reset = true; + counter = 0; + setText(String.valueOf(counter)); + repaint(); task.stop = false; } From 7738993b29e092bd7af2db10075d7407c4869e64 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:54:55 +0100 Subject: [PATCH 35/36] fix .gitignore, again --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index cfcc9bc..c52aba5 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,4 @@ buildNumber.properties # JDT-specific (Eclipse Java Development Tools) .classpath -# End of https://www.toptal.com/developers/gitignore/api/maven \ No newline at end of file +# End of https://www.toptal.com/developers/gitignore/api/maven From b1e7a31280623d87f9c6012c5ddf5cad6fdf0fa1 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 21:16:31 +0100 Subject: [PATCH 36/36] fix .gitignore, again...again --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c52aba5..c837958 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,4 @@ buildNumber.properties .classpath # End of https://www.toptal.com/developers/gitignore/api/maven +