tidy practices
This commit is contained in:
1 file changed
+47
-23
@@ -16,6 +16,7 @@ import javafx.stage.Stage;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Mines extends Application
|
||||
{
|
||||
@@ -26,6 +27,8 @@ public class Mines extends Application
|
||||
private static final int WINDOW_HEIGHT = 1080;
|
||||
private static final int BUTTON_WIDTH = 60;
|
||||
private static final int BUTTON_HEIGHT = 60;
|
||||
private static final int MENU_BUTTON_WIDTH = 180;
|
||||
private static final int MENU_BUTTON_HEIGHT = 80;
|
||||
private static final int EASY_WIDTH = 8;
|
||||
private static final int EASY_HEIGHT = 8;
|
||||
private static final int HARD_WIDTH = 36;
|
||||
@@ -78,39 +81,45 @@ public class Mines extends Application
|
||||
public void start(final Stage stage)
|
||||
{
|
||||
final Label title;
|
||||
title = new Label("Select Game Size");
|
||||
title.setStyle("-fx-font-size: 20px; -fx-font-weight: bold;");
|
||||
|
||||
final Button smallBtn;
|
||||
final Button largeBtn;
|
||||
|
||||
title = new Label("Select Game Size");
|
||||
title.setStyle("-fx-font-size: 20px; -fx-font-weight: bold;");
|
||||
|
||||
smallBtn = new Button("8 × 8 (Beginner)");
|
||||
largeBtn = new Button("36 × 16 (Expert)");
|
||||
|
||||
smallBtn.setPrefWidth(BUTTON_WIDTH);
|
||||
largeBtn.setPrefWidth(BUTTON_WIDTH);
|
||||
|
||||
smallBtn.setPrefWidth(MENU_BUTTON_WIDTH);
|
||||
largeBtn.setPrefWidth(MENU_BUTTON_WIDTH);
|
||||
smallBtn.setOnAction(e -> startGame(EASY_WIDTH, EASY_HEIGHT, EASY_MINES));
|
||||
largeBtn.setOnAction(e -> startGame(HARD_WIDTH, HARD_HEIGHT, HARD_MINES));
|
||||
|
||||
final VBox root;
|
||||
final Scene scene;
|
||||
|
||||
root = new VBox(VERTICAL_MARGIN);
|
||||
scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
|
||||
root.setPadding(new Insets(PADDING));
|
||||
root.setAlignment(Pos.CENTER);
|
||||
root.getChildren().addAll(title, smallBtn, largeBtn);
|
||||
|
||||
final Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
stage.setTitle("Random Mines - A Minesweeper Game");
|
||||
stage.setResizable(false);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
private void forEachNeighbor(final int index, final java.util.function.IntConsumer action)
|
||||
{
|
||||
final int row = index / width;
|
||||
final int col = index % width;
|
||||
private void forEachNeighbor(
|
||||
final int index,
|
||||
final Consumer<Integer> action
|
||||
) {
|
||||
final int row;
|
||||
final int col;
|
||||
|
||||
row = index / width;
|
||||
col = index % width;
|
||||
|
||||
for (int dr = MIN_OFFSET; dr <= MAX_OFFSET; dr++)
|
||||
{
|
||||
@@ -121,8 +130,11 @@ public class Mines extends Application
|
||||
continue;
|
||||
}
|
||||
|
||||
final int nr = row + dr;
|
||||
final int nc = col + dc;
|
||||
final int nr;
|
||||
final int nc;
|
||||
|
||||
nr = row + dr;
|
||||
nc = col + dc;
|
||||
|
||||
if (nr < FIRST_ROW || nr >= height ||
|
||||
nc < FIRST_COL || nc >= width)
|
||||
@@ -130,7 +142,8 @@ public class Mines extends Application
|
||||
continue;
|
||||
}
|
||||
|
||||
final int neighborIndex = nr * width + nc;
|
||||
final int neighborIndex;
|
||||
neighborIndex = nr * width + nc;
|
||||
action.accept(neighborIndex);
|
||||
}
|
||||
}
|
||||
@@ -160,14 +173,14 @@ public class Mines extends Application
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < field.length; i++)
|
||||
for (int i = 0; i < this.field.length; i++)
|
||||
{
|
||||
if (this.field[i] == MINE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
int[] count = { NO_MINE };
|
||||
final int[] count = { NO_MINE };
|
||||
|
||||
forEachNeighbor(i, neighborIndex -> {
|
||||
if (this.field[neighborIndex] == MINE)
|
||||
@@ -184,11 +197,11 @@ public class Mines extends Application
|
||||
{
|
||||
forEachNeighbor(index, neighborIndex -> {
|
||||
|
||||
if (!revealed[neighborIndex])
|
||||
if (!this.revealed[neighborIndex])
|
||||
{
|
||||
reveal(neighborIndex);
|
||||
|
||||
if (field[neighborIndex] == NO_MINE)
|
||||
if (this.field[neighborIndex] == NO_MINE)
|
||||
{
|
||||
popFieldVoid(neighborIndex);
|
||||
}
|
||||
@@ -238,11 +251,15 @@ public class Mines extends Application
|
||||
|
||||
this.buttons[index].setText(buttonText);
|
||||
|
||||
this.flags = this.flagged[index] == FLAG ? this.flags + FLAG : this.flags - FLAG;
|
||||
this.flags = this.flagged[index] == FLAG ?
|
||||
this.flags + FLAG :
|
||||
this.flags - FLAG;
|
||||
}
|
||||
|
||||
private GridPane createGrid(final int width, final int height)
|
||||
{
|
||||
private GridPane createGrid(
|
||||
final int width,
|
||||
final int height
|
||||
) {
|
||||
final GridPane grid;
|
||||
grid = new GridPane();
|
||||
|
||||
@@ -266,12 +283,19 @@ public class Mines extends Application
|
||||
button.setOnMouseEntered(e -> button.setCursor(javafx.scene.Cursor.HAND));
|
||||
button.setOnMouseExited(e -> button.setCursor(javafx.scene.Cursor.DEFAULT));
|
||||
button.setOnMouseClicked(e -> {
|
||||
if (e.getButton() == MouseButton.PRIMARY) {
|
||||
if (e.getButton() == MouseButton.PRIMARY)
|
||||
{
|
||||
reveal(index);
|
||||
}
|
||||
else if (e.getButton() == MouseButton.SECONDARY) {
|
||||
else if (e.getButton() == MouseButton.SECONDARY)
|
||||
{
|
||||
flag(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
// do nothing
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
grid.add(button, i, j);
|
||||
|
||||
Reference in new issue
Block a user