trying and failing to implement recursion in jfx

This commit is contained in:
tyler :3 2024-05-16 14:26:16 +01:00
parent 749fd17a90
commit d589f40feb
3 changed files with 75 additions and 33 deletions

View File

@ -1,6 +1,7 @@
package com.shr4pnel.minesweeper; package com.shr4pnel.minesweeper;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import javafx.fxml.FXML; import javafx.fxml.FXML;
@ -42,11 +43,17 @@ public class Controller {
int time = 0; int time = 0;
long startTime; long startTime;
int bombCount = 99; int bombCount = 99;
private Node[][] gridPaneArray;
@FXML @FXML
private void initialize() { private void initialize() {
gridHandler = new Grid(); gridHandler = new Grid();
wrapper = gridHandler.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; wrapper = gridHandler.grid;
} }
@FXML @FXML
private void gridClicked(MouseEvent event) { private void gridClicked(MouseEvent event) {
if (gameOver) { if (gameOver) {
@ -89,8 +97,11 @@ public class Controller {
gameOver(tileClicked); gameOver(tileClicked);
return; return;
} }
int adjacentBombs = getAdjacentCount(tileClicked);
setAdjacentCount(tileClicked); setAdjacentCount(tileClicked);
expandGrid(column, row); if (adjacentBombs == 0) {
System.out.println("todo. implement this stupid!");
}
} }
void gameOver(Node tileClicked) { void gameOver(Node tileClicked) {
@ -181,8 +192,9 @@ public class Controller {
for (Node node : grid.getChildren()) { for (Node node : grid.getChildren()) {
int column = GridPane.getColumnIndex(node); int column = GridPane.getColumnIndex(node);
int row = GridPane.getRowIndex(node); int row = GridPane.getRowIndex(node);
if (column == clickedColumn && row == clickedRow) if (column == clickedColumn && row == clickedRow) {
continue; continue;
}
if (wrapper.atColumn(column).atRow(row).isBomb()) { if (wrapper.atColumn(column).atRow(row).isBomb()) {
ImageView imageView = (ImageView) node; ImageView imageView = (ImageView) node;
imageView.setImage(new Image(String.valueOf(bombRevealedURL))); imageView.setImage(new Image(String.valueOf(bombRevealedURL)));
@ -205,15 +217,16 @@ public class Controller {
} }
void setAdjacentCount(Node tileClicked) { void setAdjacentCount(Node tileClicked) {
int column = GridPane.getColumnIndex(tileClicked); int adjacentBombs = getAdjacentCount(tileClicked);
int row = GridPane.getRowIndex(tileClicked);
ImageView image = (ImageView) tileClicked; ImageView image = (ImageView) tileClicked;
int adjacentBombs = wrapper.atColumn(column).atRow(row).adjacentBombCount();
URL imageURL = getClass().getResource("img/num_" + adjacentBombs + ".png"); URL imageURL = getClass().getResource("img/num_" + adjacentBombs + ".png");
image.setImage(new Image(String.valueOf(imageURL))); 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();
} }
} }

View File

@ -15,9 +15,8 @@ public class Grid {
private void generateBombs(int bombMax) { private void generateBombs(int bombMax) {
int i; int i;
boolean success = false; boolean success;
for (i = 0; i < bombMax; ++i) { for (i = 0; i < bombMax; ++i) {
System.out.println("Pass " + i);
success = false; success = false;
while (!success) { while (!success) {
int column = ThreadLocalRandom.current().nextInt(30); int column = ThreadLocalRandom.current().nextInt(30);

View File

@ -1,11 +1,23 @@
package com.shr4pnel.minesweeper; 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 { public class GridWrapper {
final boolean[][] grid = new boolean[30][16]; final boolean[][] grid = new boolean[30][16];
private int currentColumn; private int currentColumn;
private int currentRow; private int currentRow;
public GridWrapper() { public GridWrapper() {
this.currentColumn = 0; this.currentColumn = 0;
this.currentRow = 0; this.currentRow = 0;
@ -66,42 +78,60 @@ public class GridWrapper {
return count; return count;
} }
private int topRight() {
return grid[currentColumn + 1][currentRow - 1] ? 1 : 0;
}
private int top() { private int top() {
return grid[currentColumn][currentRow - 1] ? 1 : 0; return isValid() && currentRow > 0 ? (grid[currentColumn][currentRow - 1] ? 1 : 0) : 0;
} }
private int topLeft() { private int topRight() {
return grid[currentColumn - 1][currentRow - 1] ? 1 : 0; return isValid() && currentColumn < 29 && currentRow > 0 ?
} (grid[currentColumn + 1][currentRow - 1] ? 1 : 0) : 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 right() { 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() { 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() { void printGrid() {
int i, j; int i, j;
for (i = 0; i < 30; ++i) { for (i = 0; i < 30; ++i) {