refactor, split methods, implement recursive tile open
This commit is contained in:
parent
97a1a2b296
commit
76d8ce23d9
@ -14,90 +14,140 @@ import javafx.scene.layout.GridPane;
|
|||||||
|
|
||||||
public class Controller {
|
public class Controller {
|
||||||
@FXML
|
@FXML
|
||||||
GridPane grid;
|
private GridPane grid;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
ImageView smiley;
|
private ImageView smiley, time_1, time_2, time_3, bomb_2, bomb_3;
|
||||||
|
|
||||||
@FXML
|
private Grid gridHandler;
|
||||||
ImageView time_1;
|
private GridWrapper wrapper;
|
||||||
|
private boolean gameOver = false;
|
||||||
@FXML
|
private boolean isFirstLoad = true;
|
||||||
ImageView time_2;
|
|
||||||
|
|
||||||
@FXML
|
|
||||||
ImageView time_3;
|
|
||||||
|
|
||||||
@FXML
|
|
||||||
ImageView bomb_2;
|
|
||||||
|
|
||||||
@FXML
|
|
||||||
ImageView bomb_3;
|
|
||||||
|
|
||||||
|
|
||||||
Grid gridHandler;
|
|
||||||
GridWrapper wrapper;
|
|
||||||
boolean gameOver = false;
|
|
||||||
boolean isFirstLoad = true;
|
|
||||||
static Timer timer = new Timer();
|
static Timer timer = new Timer();
|
||||||
int time = 0;
|
private int time = 0;
|
||||||
long startTime;
|
private long startTime;
|
||||||
int bombCount = 99;
|
private int bombCount = 99;
|
||||||
private Node[][] gridPaneArray;
|
|
||||||
|
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void initialize() {
|
private void initialize() {
|
||||||
|
setupGrid();
|
||||||
|
gridHandler = new Grid();
|
||||||
|
wrapper = gridHandler.grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupGrid() {
|
||||||
for (int column = 0; column < 30; ++column) {
|
for (int column = 0; column < 30; ++column) {
|
||||||
for (int row = 0; row < 16; ++row) {
|
for (int row = 0; row < 16; ++row) {
|
||||||
Image blank =
|
Button blankButton = createBlankButton();
|
||||||
new Image(String.valueOf(getClass().getResource("img/blank.png")), 16, 16, true,
|
|
||||||
true);
|
|
||||||
ImageView blankImage = new ImageView(blank);
|
|
||||||
Button blankButton = new Button();
|
|
||||||
blankButton.setGraphic(blankImage);
|
|
||||||
blankButton.setMinSize(16, 16);
|
|
||||||
blankButton.setStyle(
|
|
||||||
"-fx-background-color: transparent; -fx-border-color: transparent; -fx-padding: 0;");
|
|
||||||
blankButton.setOnMouseClicked(this::buttonClicked);
|
|
||||||
grid.add(blankButton, column, row);
|
grid.add(blankButton, column, row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gridHandler = new Grid();
|
|
||||||
wrapper = gridHandler.grid;
|
|
||||||
gridPaneArray = new Node[30][16];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void buttonClicked(MouseEvent e) {
|
private Button createBlankButton() {
|
||||||
Button clicked = (Button) e.getSource();
|
Image blank =
|
||||||
int column = GridPane.getColumnIndex(clicked);
|
new Image(String.valueOf(getClass().getResource("img/blank.png")), 16, 16, true, true);
|
||||||
int row = GridPane.getRowIndex(clicked);
|
ImageView blankImage = new ImageView(blank);
|
||||||
|
Button blankButton = new Button();
|
||||||
|
blankButton.setGraphic(blankImage);
|
||||||
|
blankButton.setMinSize(16, 16);
|
||||||
|
blankButton.setOnMouseClicked(this::buttonClicked);
|
||||||
|
return blankButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buttonClicked(MouseEvent e) {
|
||||||
if (gameOver) {
|
if (gameOver) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Button clicked = (Button) e.getSource();
|
||||||
|
int column = GridPane.getColumnIndex(clicked);
|
||||||
|
int row = GridPane.getRowIndex(clicked);
|
||||||
|
|
||||||
if (isFirstLoad) {
|
if (isFirstLoad) {
|
||||||
scheduleTimer();
|
scheduleTimer();
|
||||||
|
isFirstLoad = false;
|
||||||
}
|
}
|
||||||
isFirstLoad = false;
|
|
||||||
if (e.getButton() == MouseButton.SECONDARY) {
|
if (e.getButton() == MouseButton.SECONDARY) {
|
||||||
flag(clicked);
|
flag(clicked);
|
||||||
return;
|
} else {
|
||||||
}
|
handlePrimaryClick(clicked, column, row);
|
||||||
if (wrapper.atColumn(column).atRow(row).isBomb()) {
|
|
||||||
gameOver(clicked);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int adjacentBombs = getAdjacentCount(clicked);
|
|
||||||
setAdjacentCount(clicked);
|
|
||||||
if (adjacentBombs == 0) {
|
|
||||||
System.out.println("todo. implement this stupid!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handlePrimaryClick(Button clicked, int column, int row) {
|
||||||
|
if (wrapper.atColumn(column).atRow(row).isBomb()) {
|
||||||
|
gameOver(clicked);
|
||||||
|
} else {
|
||||||
|
int adjacentBombs = wrapper.adjacentBombCount();
|
||||||
|
setAdjacentCount(clicked, adjacentBombs);
|
||||||
|
if (adjacentBombs == 0) {
|
||||||
|
recursiveExpandTiles(column, row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recursiveExpandTiles(int column, int row) {
|
||||||
|
if (row > 0) {
|
||||||
|
expandTile(column, row - 1);
|
||||||
|
}
|
||||||
|
if (row > 0 && column < 29) {
|
||||||
|
expandTile(column + 1, row - 1);
|
||||||
|
}
|
||||||
|
if (column < 29) {
|
||||||
|
expandTile(column + 1, row);
|
||||||
|
}
|
||||||
|
if (row < 15 && column < 29) {
|
||||||
|
expandTile(column + 1, row + 1);
|
||||||
|
}
|
||||||
|
if (row < 15) {
|
||||||
|
expandTile(column, row + 1);
|
||||||
|
}
|
||||||
|
if (row < 15 && column > 0) {
|
||||||
|
expandTile(column - 1, row + 1);
|
||||||
|
}
|
||||||
|
if (column > 0) {
|
||||||
|
expandTile(column - 1, row);
|
||||||
|
}
|
||||||
|
if (row > 0 && column > 0) {
|
||||||
|
expandTile(column - 1, row - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void expandTile(int column, int row) {
|
||||||
|
Node tile = getNodeByRowColumnIndex(row, column);
|
||||||
|
if (tile != null) {
|
||||||
|
Button button = (Button) tile;
|
||||||
|
if (button.isVisible()) {
|
||||||
|
int adjacentBombs = wrapper.atColumn(column).atRow(row).adjacentBombCount();
|
||||||
|
setAdjacentCount(button, adjacentBombs);
|
||||||
|
if (adjacentBombs == 0) {
|
||||||
|
recursiveExpandTiles(column, row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Node getNodeByRowColumnIndex(int row, int column) {
|
||||||
|
for (Node node : grid.getChildren()) {
|
||||||
|
if (GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column) {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
private void reinitialize() {
|
private void reinitialize() {
|
||||||
bombCount = 99;
|
bombCount = 99;
|
||||||
updateBombCounter();
|
updateBombCounter();
|
||||||
|
resetTimer();
|
||||||
|
resetGrid();
|
||||||
|
gridHandler = new Grid();
|
||||||
|
wrapper = gridHandler.grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resetTimer() {
|
||||||
URL zeroSecondURL = getClass().getResource("img/0_seconds.png");
|
URL zeroSecondURL = getClass().getResource("img/0_seconds.png");
|
||||||
time_1.setImage(new Image(String.valueOf(zeroSecondURL)));
|
time_1.setImage(new Image(String.valueOf(zeroSecondURL)));
|
||||||
time_2.setImage(new Image(String.valueOf(zeroSecondURL)));
|
time_2.setImage(new Image(String.valueOf(zeroSecondURL)));
|
||||||
@ -105,151 +155,112 @@ public class Controller {
|
|||||||
timer.cancel();
|
timer.cancel();
|
||||||
isFirstLoad = true;
|
isFirstLoad = true;
|
||||||
timer = new Timer();
|
timer = new Timer();
|
||||||
URL blank = getClass().getResource("img/blank.png");
|
|
||||||
for (Node n : grid.getChildren()) {
|
|
||||||
Button nodeAsButton = (Button) n;
|
|
||||||
nodeAsButton.setGraphic(new ImageView(new Image(String.valueOf(blank), 16, 16, true, false)));
|
|
||||||
}
|
|
||||||
gridHandler = new Grid();
|
|
||||||
wrapper = gridHandler.grid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @FXML
|
private void resetGrid() {
|
||||||
// private void gridClicked(MouseEvent event) {
|
URL blank = getClass().getResource("img/blank.png");
|
||||||
|
for (Node node : grid.getChildren()) {
|
||||||
|
Button button = (Button) node;
|
||||||
|
button.setGraphic(new ImageView(new Image(String.valueOf(blank), 16, 16, true, false)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void gameOver(Node tileClicked) {
|
private void gameOver(Node tileClicked) {
|
||||||
gameOver = true;
|
gameOver = true;
|
||||||
timer.cancel();
|
timer.cancel();
|
||||||
System.out.println("DEAD!!!");
|
setImage(smiley, "img/face_dead.png");
|
||||||
URL smileyURL = getClass().getResource("img/face_dead.png");
|
setImage((Button) tileClicked, "img/bomb_death.png");
|
||||||
smiley.setImage(new Image(String.valueOf(smileyURL)));
|
|
||||||
Button tileClickedButton = (Button) tileClicked;
|
|
||||||
URL deathBombURL = getClass().getResource("img/bomb_death.png");
|
|
||||||
ImageView deathBombImage =
|
|
||||||
new ImageView(new Image(String.valueOf(deathBombURL), 16, 16, true, false));
|
|
||||||
tileClickedButton.setGraphic(deathBombImage);
|
|
||||||
tileClickedButton.setMinSize(16, 16);
|
|
||||||
tileClickedButton.setPrefSize(16, 16);
|
|
||||||
showAllBombs(GridPane.getColumnIndex(tileClicked), GridPane.getRowIndex(tileClicked));
|
showAllBombs(GridPane.getColumnIndex(tileClicked), GridPane.getRowIndex(tileClicked));
|
||||||
}
|
}
|
||||||
|
|
||||||
void flag(Node tileClicked) {
|
private void flag(Node tileClicked) {
|
||||||
bombCount--;
|
bombCount--;
|
||||||
URL flagURL = getClass().getResource("img/bomb_flagged.png");
|
|
||||||
Image flagImage = new Image(String.valueOf(flagURL), 16, 16, true, false);
|
|
||||||
Button tile = (Button) tileClicked;
|
|
||||||
// if (tile.getGraphic().) {
|
|
||||||
// bombCount++;
|
|
||||||
// }
|
|
||||||
updateBombCounter();
|
updateBombCounter();
|
||||||
tile.setGraphic(new ImageView(new Image(String.valueOf(flagURL), 16, 16, true, false)));
|
setImage((Button) tileClicked, "img/bomb_flagged.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateBombCounter() {
|
private void updateBombCounter() {
|
||||||
String bombCountString = String.valueOf(bombCount);
|
String bombCountString = String.format("%03d", bombCount);
|
||||||
char[] bombCountCharArray = bombCountString.toCharArray();
|
setBombCounterImage(bomb_2, bombCountString.charAt(1));
|
||||||
if (bombCountString.length() == 2) {
|
setBombCounterImage(bomb_3, bombCountString.charAt(2));
|
||||||
URL bombCountTensURL =
|
|
||||||
getClass().getResource("img/" + bombCountCharArray[0] + "_seconds.png");
|
|
||||||
URL bombCountUnitsURL =
|
|
||||||
getClass().getResource("img/" + bombCountCharArray[1] + "_seconds.png");
|
|
||||||
bomb_2.setImage(new Image(String.valueOf(bombCountTensURL)));
|
|
||||||
bomb_3.setImage(new Image(String.valueOf(bombCountUnitsURL)));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
URL bombCountUnitsURL =
|
|
||||||
getClass().getResource("img/" + bombCountCharArray[1] + "_seconds.png");
|
|
||||||
bomb_3.setImage(new Image(String.valueOf(bombCountUnitsURL)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void scheduleTimer() {
|
private void setBombCounterImage(ImageView imageView, char digit) {
|
||||||
|
URL imageURL = getClass().getResource("img/" + digit + "_seconds.png");
|
||||||
|
imageView.setImage(new Image(String.valueOf(imageURL)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scheduleTimer() {
|
||||||
startTime = System.currentTimeMillis();
|
startTime = System.currentTimeMillis();
|
||||||
TimerTask task = new TimerTask() {
|
TimerTask task = new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (time >= 999) {
|
if (time >= 999) {
|
||||||
timer.cancel();
|
timer.cancel();
|
||||||
return;
|
} else {
|
||||||
}
|
updateTimer();
|
||||||
long currentTimeMillis = System.currentTimeMillis();
|
|
||||||
long elapsedTimeMillis = currentTimeMillis - startTime;
|
|
||||||
time = (int) (elapsedTimeMillis / 1000);
|
|
||||||
String timeString = String.valueOf(time);
|
|
||||||
char[] timeStringArray = timeString.toCharArray();
|
|
||||||
int length = timeString.length();
|
|
||||||
if (length == 3) {
|
|
||||||
char hundred = timeStringArray[0];
|
|
||||||
char tens = timeStringArray[1];
|
|
||||||
char unit = timeStringArray[2];
|
|
||||||
URL hundredURL = getClass().getResource("img/" + hundred + "_seconds.png");
|
|
||||||
URL tensURL = getClass().getResource("img/" + tens + "_seconds.png");
|
|
||||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
|
||||||
time_1.setImage(new Image(String.valueOf(hundredURL)));
|
|
||||||
time_2.setImage(new Image(String.valueOf(tensURL)));
|
|
||||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
|
||||||
} else if (length == 2) {
|
|
||||||
char tens = timeStringArray[0];
|
|
||||||
char unit = timeStringArray[1];
|
|
||||||
URL tensURL = getClass().getResource("img/" + tens + "_seconds.png");
|
|
||||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
|
||||||
time_2.setImage(new Image(String.valueOf(tensURL)));
|
|
||||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
|
||||||
} else if (length == 1) {
|
|
||||||
char unit = timeStringArray[0];
|
|
||||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
|
||||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
timer.schedule(task, 0, 1000);
|
timer.schedule(task, 0, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void showAllBombs(int clickedColumn, int clickedRow) {
|
private void updateTimer() {
|
||||||
URL bombRevealedURL = getClass().getResource("img/bomb_revealed.png");
|
long elapsedTimeMillis = System.currentTimeMillis() - startTime;
|
||||||
|
time = (int) (elapsedTimeMillis / 1000);
|
||||||
|
String timeString = String.format("%03d", time);
|
||||||
|
setTimerImage(time_1, timeString.charAt(0));
|
||||||
|
setTimerImage(time_2, timeString.charAt(1));
|
||||||
|
setTimerImage(time_3, timeString.charAt(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setTimerImage(ImageView imageView, char digit) {
|
||||||
|
URL imageURL = getClass().getResource("img/" + digit + "_seconds.png");
|
||||||
|
imageView.setImage(new Image(String.valueOf(imageURL)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showAllBombs(int clickedColumn, int clickedRow) {
|
||||||
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;
|
wrapper.atColumn(column).atRow(row).isBomb()) {
|
||||||
}
|
setImage((Button) node, "img/bomb_revealed.png");
|
||||||
if (wrapper.atColumn(column).atRow(row).isBomb()) {
|
|
||||||
Button button = (Button) node;
|
|
||||||
ImageView buttonGraphic =
|
|
||||||
new ImageView(new Image(String.valueOf(bombRevealedURL), 16, 16, true, false));
|
|
||||||
button.setGraphic(buttonGraphic);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
private void setImage(Button button, String imagePath) {
|
||||||
void smileyPressed() {
|
URL imageURL = getClass().getResource(imagePath);
|
||||||
URL smileyURL = getClass().getResource("img/face_smile_pressed.png");
|
button.setGraphic(new ImageView(new Image(String.valueOf(imageURL), 16, 16, true, false)));
|
||||||
smiley.setImage(new Image(String.valueOf(smileyURL)));
|
}
|
||||||
|
|
||||||
|
private void setImage(ImageView imageView, String imagePath) {
|
||||||
|
URL imageURL = getClass().getResource(imagePath);
|
||||||
|
imageView.setImage(new Image(String.valueOf(imageURL)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
void smileyReleased() {
|
private void smileyPressed() {
|
||||||
|
setImage(smiley, "img/face_smile_pressed.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void smileyReleased() {
|
||||||
gameOver = false;
|
gameOver = false;
|
||||||
URL smileyURL = getClass().getResource("img/face_smile.png");
|
setImage(smiley, "img/face_smile.png");
|
||||||
smiley.setImage(new Image(String.valueOf(smileyURL)));
|
|
||||||
reinitialize();
|
reinitialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setAdjacentCount(Node tileClicked) {
|
private void setAdjacentCount(Node tileClicked, int adjacentBombs) {
|
||||||
int adjacentBombs = getAdjacentCount(tileClicked);
|
|
||||||
Button button = (Button) tileClicked;
|
Button button = (Button) tileClicked;
|
||||||
URL imageURL = getClass().getResource("img/num_" + adjacentBombs + ".png");
|
URL imageURL = getClass().getResource("img/num_" + adjacentBombs + ".png");
|
||||||
Button adjacentButton = new Button();
|
button.setGraphic(new ImageView(new Image(String.valueOf(imageURL), 16, 16, true, false)));
|
||||||
adjacentButton.setMinSize(16, 16);
|
|
||||||
ImageView buttonImage =
|
|
||||||
new ImageView(new Image(String.valueOf(imageURL), 16, 16, true, false));
|
|
||||||
button.setGraphic(buttonImage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int getAdjacentCount(Node tileClicked) {
|
private int getAdjacentCount(Node tileClicked) {
|
||||||
int column = GridPane.getColumnIndex(tileClicked);
|
int column = GridPane.getColumnIndex(tileClicked);
|
||||||
int row = GridPane.getRowIndex(tileClicked);
|
int row = GridPane.getRowIndex(tileClicked);
|
||||||
return wrapper.atColumn(column).atRow(row).adjacentBombCount();
|
return wrapper.atColumn(column).atRow(row).adjacentBombCount();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user