Javafx 扫雷艇上的显示板

Javafx 扫雷艇上的显示板,javafx,Javafx,我正在做一个扫雷舰的程序,我想在玩家点击炸弹后得到一点帮助来展示整个棋盘。代码如下: package narkalieutis; import java.util.ArrayList; import java.util.List; import java.util.Optional; import javafx.scene.control.MenuBar; import javafx.application.Application; import javafx.event.ActionEven

我正在做一个扫雷舰的程序,我想在玩家点击炸弹后得到一点帮助来展示整个棋盘。代码如下:

package narkalieutis;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javafx.scene.control.MenuBar;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;

public class Narkalieutis extends Application {

private int TILE_SIZE = 50;
private static final int W = 500;
private static final int H = 500;

private int X_TILES = W / TILE_SIZE;
private int Y_TILES = H / TILE_SIZE;
private final String[] gameType = {"Easy", "Medium", "Hard", "Very Hard"};
private String difficulty;

@FXML
private Menu gameMenu;

private Tile[][] grid = new Tile[X_TILES][Y_TILES];
private Scene scene;

public MenuBar menu() {
    gameMenu = new Menu("Difficulty");
    for(String game : gameType){
        MenuItem menuItem = new MenuItem(game);
        menuItem.setUserData(game);
        menuItem.setOnAction((ActionEvent event) -> {
            selectGame(event);
        });
        gameMenu.getItems().add(menuItem);
    }
    MenuBar menuBar = new MenuBar(gameMenu);
    return menuBar;
}

private void selectGame(ActionEvent event) {
    MenuItem menuItem = (MenuItem)event.getSource();
    difficulty = (String)menuItem.getUserData();
    switch (difficulty) {
        case "Easy":
            TILE_SIZE = 2*TILE_SIZE;
            break;
        case "Medium":
            TILE_SIZE = 100;
            break;
        case "Hard":
            TILE_SIZE = 50;
            break;
        case "Very Hard":
            TILE_SIZE = 40;
            break;
        default:
            break;
    }
}

private Parent createContent() {
    VBox root = new VBox();
    Pane content = new Pane();
    content.setPrefSize(W, H);

    for (int y = 0; y < Y_TILES; y++) {
        for (int x = 0; x < X_TILES; x++) {
            Tile tile = new Tile(x, y, Math.random() < 0.2);

            grid[x][y] = tile;
            content.getChildren().add(tile);
        }
    }

    for (int y = 0; y < Y_TILES; y++) {
        for (int x = 0; x < X_TILES; x++) {
            Tile tile = grid[x][y];

            if (tile.hasBomb)
                continue;

            long bombs = getNeighbors(tile).stream().filter(t ->      t.hasBomb).count();

            if (bombs > 0)
                tile.text.setText(String.valueOf(bombs));
        }
    }
    root.getChildren().addAll(menu(), content);

    return root;
}

private List<Tile> getNeighbors(Tile tile) {
    List<Tile> neighbors = new ArrayList<>();

    int[] points = new int[] {
          -1, -1,
          -1, 0,
          -1, 1,
          0, -1,
          0, 1,
          1, -1,
          1, 0,
          1, 1
    };

    for (int i = 0; i < points.length; i++) {
        int dx = points[i];
        int dy = points[++i];

        int newX = tile.x + dx;
        int newY = tile.y + dy;

        if (newX >= 0 && newX < X_TILES
                && newY >= 0 && newY < Y_TILES) {
            neighbors.add(grid[newX][newY]);
        }
    }

    return neighbors;
}

private class Tile extends StackPane {
    private int x, y;
    private boolean hasBomb;
    private boolean isOpen = false;

    private Rectangle board = new Rectangle(TILE_SIZE - 2, TILE_SIZE - 2);
    private Text text = new Text();

  Alert alert = new Alert(AlertType.CONFIRMATION);

    public Tile(int x, int y, boolean hasBomb) {
        this.x = x;
        this.y = y;
        this.hasBomb = hasBomb;

        board.setStroke(Color.LIGHTGRAY);

        text.setFont(Font.font(18));
        text.setText(hasBomb ? "X" : "");
        text.setVisible(false);

        getChildren().addAll(board, text);

        setTranslateX(x * TILE_SIZE);
        setTranslateY(y * TILE_SIZE);

        setOnMouseClicked(e -> open());
    }

    public void open() {
        if (isOpen)
            return;

        if (hasBomb) {
    board.setVisible(true);
        Optional<ButtonType> result = alert.showAndWait();
            if (result.isPresent() && result.get() == ButtonType.OK) {
                scene.setRoot(createContent());   
            }
           return;
        }

        isOpen = true;
        text.setVisible(true);
        board.setFill(null);

        if (text.getText().isEmpty()) {
            getNeighbors(this).forEach(Tile::open);
        }

        switch (text.getText()) {
            case "1":
                text.setFill(Color.BLUE);
                break;
            case "2":
                text.setFill(Color.FORESTGREEN);
                break;
            case "3":
                text.setFill(Color.RED);
                break;
            case "4":
                text.setFill(Color.DARKBLUE);
                break;
            case "5":
                text.setFill(Color.MAROON);
                break;
            case "6":
                text.setFill(Color.AQUAMARINE);
                break;
            case "7":
                text.setFill(Color.BLACK);
                break;
            case "8":
                text.setFill(Color.GRAY);
                break;
            default:
                break;
        }
    }
}

@Override
public void start(Stage stage) throws Exception {
    scene = new Scene(createContent());

    stage.setScene(scene);
    stage.show();     
}

public static void main(String[] args) {
    launch(args);
    }
}
narkalieutis包;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Optional;
导入javafx.scene.control.MenuBar;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.Alert;
导入javafx.scene.control.Alert.AlertType;
导入javafx.scene.control.ButtonType;
导入javafx.scene.control.Menu;
导入javafx.scene.control.MenuItem;
导入javafx.scene.layout.Pane;
导入javafx.scene.layout.StackPane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Rectangle;
导入javafx.scene.text.Font;
导入javafx.scene.text.text;
导入javafx.stage.stage;
导入javafx.scene.layout.VBox;
公共类Narkalieutis扩展了应用程序{
私人内部瓷砖尺寸=50;
专用静态最终int W=500;
专用静态最终int H=500;
私有int X_TILES=W/TILE_尺寸;
私人内部瓷砖=H/瓷砖尺寸;
私有最终字符串[]游戏类型={“容易”、“中等”、“难”、“非常难”};
私人串困难;
@FXML
私人菜单游戏菜单;
私有磁贴[][]网格=新磁贴[X_磁贴][Y_磁贴];
私密场景;
公共菜单栏菜单(){
游戏菜单=新菜单(“难度”);
用于(字符串游戏:游戏类型){
MenuItem MenuItem=新MenuItem(游戏);
menuItem.setUserData(游戏);
menuItem.setOnAction((ActionEvent事件)->{
选择游戏(事件);
});
gameMenu.getItems().add(menuItem);
}
菜单栏菜单栏=新菜单栏(游戏菜单);
返回菜单栏;
}
私有void selectGame(ActionEvent事件){
MenuItem MenuItem=(MenuItem)事件。getSource();
难度=(字符串)menuItem.getUserData();
转换(难度){
案例“简单”:
瓷砖尺寸=2*瓷砖尺寸;
打破
案例“中等”:
瓷砖尺寸=100;
打破
案例“硬”:
瓷砖尺寸=50;
打破
案例“非常困难”:
瓷砖尺寸=40;
打破
违约:
打破
}
}
私有父createContent(){
VBox root=新的VBox();
窗格内容=新建窗格();
content.setPrefSize(W,H);
对于(int y=0;yt.hasBomb.count();
如果(炸弹>0)
tile.text.setText(String.valueOf(bombs));
}
}
root.getChildren().addAll(菜单(),内容);
返回根;
}
私有列表GetNeights(平铺){
列表邻居=新的ArrayList();
int[]点=新int[]{
-1, -1,
-1, 0,
-1, 1,
0, -1,
0, 1,
1, -1,
1, 0,
1, 1
};
对于(int i=0;i=0&&newX=0&&newYopen());
}
公开作废{
if(等参线)
返回;
如果(炸弹){
board.setVisible(true);
可选结果=alert.showAndWait();
if(result.isPresent()&&result.get()==ButtonType.OK){
setRoot(createContent());
}
返回;
}
isOpen=真;
text.setVisible(true);
board.setFill(空);
if(text.getText().isEmpty()){
getNeights(this.forEach)(Tile::open);
}
开关(text.getText()){
案例“1”:
text.setFill(颜色为蓝色);
打破
案例“2”:
text.setFill(颜色为绿色);
打破
案例“3”:
text.setFill(颜色为红色);
打破
案例“4”:
text.setFill(Color.DARKBLUE);
打破
案例“5”:
text.setFill(颜色:褐红色);
打破
案例“6”:
text.setFill(颜色:海蓝宝石);
打破
案例“7”:
text.setFill(颜色为黑色);
打破
案例“8”:
text.setFill(颜色为灰色);
打破
违约:
打破
}
}
}
@凌驾
public void start(Stage)引发异常{
场景=新场景(createContent());
舞台场景;
stage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}
我试图让它,这样当球员失去了一个游戏的内容