From d589f40feb444393b8fdc589fcae2058ecad3185 Mon Sep 17 00:00:00 2001 From: "tyler :3" Date: Thu, 16 May 2024 14:26:16 +0100 Subject: [PATCH] trying and failing to implement recursion in jfx --- .../com/shr4pnel/minesweeper/Controller.java | 25 ++++-- .../java/com/shr4pnel/minesweeper/Grid.java | 3 +- .../com/shr4pnel/minesweeper/GridWrapper.java | 80 +++++++++++++------ 3 files changed, 75 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/shr4pnel/minesweeper/Controller.java b/src/main/java/com/shr4pnel/minesweeper/Controller.java index feadd92..e98405d 100644 --- a/src/main/java/com/shr4pnel/minesweeper/Controller.java +++ b/src/main/java/com/shr4pnel/minesweeper/Controller.java @@ -1,6 +1,7 @@ package com.shr4pnel.minesweeper; import java.net.URL; +import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; import javafx.fxml.FXML; @@ -42,11 +43,17 @@ public class Controller { int time = 0; long startTime; int bombCount = 99; + private Node[][] gridPaneArray; + @FXML private void initialize() { gridHandler = new Grid(); wrapper = gridHandler.grid; + gridPaneArray = new Node[30][16]; + for (Node node : grid.getChildren()) { + gridPaneArray[GridPane.getColumnIndex(node)][GridPane.getRowIndex(node)] = node; + } } @@ -69,6 +76,7 @@ public class Controller { wrapper = gridHandler.grid; } + @FXML private void gridClicked(MouseEvent event) { if (gameOver) { @@ -89,8 +97,11 @@ public class Controller { gameOver(tileClicked); return; } + int adjacentBombs = getAdjacentCount(tileClicked); setAdjacentCount(tileClicked); - expandGrid(column, row); + if (adjacentBombs == 0) { + System.out.println("todo. implement this stupid!"); + } } void gameOver(Node tileClicked) { @@ -181,8 +192,9 @@ public class Controller { for (Node node : grid.getChildren()) { int column = GridPane.getColumnIndex(node); int row = GridPane.getRowIndex(node); - if (column == clickedColumn && row == clickedRow) + if (column == clickedColumn && row == clickedRow) { continue; + } if (wrapper.atColumn(column).atRow(row).isBomb()) { ImageView imageView = (ImageView) node; imageView.setImage(new Image(String.valueOf(bombRevealedURL))); @@ -205,15 +217,16 @@ public class Controller { } void setAdjacentCount(Node tileClicked) { - int column = GridPane.getColumnIndex(tileClicked); - int row = GridPane.getRowIndex(tileClicked); + int adjacentBombs = getAdjacentCount(tileClicked); ImageView image = (ImageView) tileClicked; - int adjacentBombs = wrapper.atColumn(column).atRow(row).adjacentBombCount(); URL imageURL = getClass().getResource("img/num_" + adjacentBombs + ".png"); image.setImage(new Image(String.valueOf(imageURL))); } - void expandGrid(int baseColumn, int baseRow) { + int getAdjacentCount(Node tileClicked) { + int column = GridPane.getColumnIndex(tileClicked); + int row = GridPane.getRowIndex(tileClicked); + return wrapper.atColumn(column).atRow(row).adjacentBombCount(); } } \ No newline at end of file diff --git a/src/main/java/com/shr4pnel/minesweeper/Grid.java b/src/main/java/com/shr4pnel/minesweeper/Grid.java index 9642a03..7eeae09 100644 --- a/src/main/java/com/shr4pnel/minesweeper/Grid.java +++ b/src/main/java/com/shr4pnel/minesweeper/Grid.java @@ -15,9 +15,8 @@ public class Grid { private void generateBombs(int bombMax) { int i; - boolean success = false; + boolean success; for (i = 0; i < bombMax; ++i) { - System.out.println("Pass " + i); success = false; while (!success) { int column = ThreadLocalRandom.current().nextInt(30); diff --git a/src/main/java/com/shr4pnel/minesweeper/GridWrapper.java b/src/main/java/com/shr4pnel/minesweeper/GridWrapper.java index 70d7603..beff16a 100644 --- a/src/main/java/com/shr4pnel/minesweeper/GridWrapper.java +++ b/src/main/java/com/shr4pnel/minesweeper/GridWrapper.java @@ -1,11 +1,23 @@ package com.shr4pnel.minesweeper; +//class ValidTileBean { +// boolean top; +// boolean topRight; +// boolean right; +// boolean bottomRight; +// boolean bottom; +// boolean bottomLeft; +// boolean left; +// boolean topLeft; +//} + public class GridWrapper { final boolean[][] grid = new boolean[30][16]; private int currentColumn; private int currentRow; + public GridWrapper() { this.currentColumn = 0; this.currentRow = 0; @@ -66,42 +78,60 @@ public class GridWrapper { return count; } - private int topRight() { - return grid[currentColumn + 1][currentRow - 1] ? 1 : 0; - } - private int top() { - return grid[currentColumn][currentRow - 1] ? 1 : 0; + return isValid() && currentRow > 0 ? (grid[currentColumn][currentRow - 1] ? 1 : 0) : 0; } - private int topLeft() { - return grid[currentColumn - 1][currentRow - 1] ? 1 : 0; - } - - private int left() { - return grid[currentColumn - 1][currentRow] ? 1 : 0; - } - - private int bottomLeft() { - return grid[currentColumn - 1][currentRow + 1] ? 1 : 0; - } - - private int bottom() { - return grid[currentColumn][currentRow + 1] ? 1 : 0; - } - - private int bottomRight() { - return grid[currentColumn + 1][currentRow + 1] ? 1 : 0; + private int topRight() { + return isValid() && currentColumn < 29 && currentRow > 0 ? + (grid[currentColumn + 1][currentRow - 1] ? 1 : 0) : 0; } private int right() { - return grid[currentColumn + 1][currentRow] ? 1 : 0; + return isValid() && currentColumn < 29 ? (grid[currentColumn + 1][currentRow] ? 1 : 0) : 0; + } + + private int bottomRight() { + return isValid() && currentColumn < 29 && currentRow < 15 ? + (grid[currentColumn + 1][currentRow + 1] ? 1 : 0) : 0; + } + + private int bottom() { + return isValid() && currentRow < 15 ? (grid[currentColumn][currentRow + 1] ? 1 : 0) : 0; + } + + private int bottomLeft() { + return isValid() && currentColumn > 0 && currentRow < 15 ? + (grid[currentColumn - 1][currentRow + 1] ? 1 : 0) : 0; + } + + private int left() { + return isValid() && currentColumn > 0 ? (grid[currentColumn - 1][currentRow] ? 1 : 0) : 0; + } + + private int topLeft() { + return isValid() && currentColumn > 0 && currentRow > 0 ? + (grid[currentColumn - 1][currentRow - 1] ? 1 : 0) : 0; } boolean isValid() { - return this.currentColumn >= 0 && this.currentColumn < 30 && this.currentRow >= 0 && this.currentRow < 16; + return this.currentColumn >= 0 && this.currentColumn < 30 && this.currentRow >= 0 && + this.currentRow < 16; } +// ValidTileBean getBounds() { +// ValidTileBean validTiles = new ValidTileBean(); +// validTiles.top = top() != 0; +// validTiles.topRight = topRight() != 0; +// validTiles.right = right() != 0; +// validTiles.bottomRight = bottomRight() != 0; +// validTiles.bottom = bottom() != 0; +// validTiles.bottomLeft = bottomLeft() != 0; +// validTiles.left = left() != 0; +// validTiles.topLeft = topLeft() != 0; +// return validTiles; +// } + void printGrid() { int i, j; for (i = 0; i < 30; ++i) {