我的游戏有问题。问题是我想反驳一个正方形的边框是否只填充了红色或蓝色。 (玩家 1 和玩家 2)我必须以某种方式计算这些填充的方块。 `包控制器;我试图用 ChatGPT 解决它,但它似乎对 AI 来说很难,所以我来到了这个网站。请有人帮助我。
import javafx.fxml.FXML;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
public class GameController {
@FXML
private GridPane gridPane;
private boolean playerONE = true;
int i = 0;
int j = 0;
/**
* Initializes a 4x4 grid with for cycle
*/
public void initialize() {
gridPane.setGridLinesVisible(false);
gridPane.setPrefSize(200, 200);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
createVerticalLine();
createHorizontalLine();
}
}
for (i = 0; i < 4; i++) {
j = 4;
createHorizontalLine();
}
for (j = 0; j < 4; j++) {
i = 4;
createVerticalLine();
}
}
/**
* Creates a vertical line on the GridPane, it also paints it to red or blue if it is black.
*/
private void createVerticalLine()
{
Line line = new Line(0, 0, 0, 100);
line.setStrokeWidth(3);
StackPane stackPane = new StackPane(line);
stackPane.setOnMouseClicked(event -> {
if (event.getButton() == MouseButton.PRIMARY && playerONE && line.getStroke() == Color.BLACK) {
line.setStroke(Color.RED);
playerONE = false;
} else if (event.getButton() == MouseButton.PRIMARY && !playerONE && line.getStroke() == Color.BLACK) {
line.setStroke(Color.BLUE);
playerONE = true;
}
});
gridPane.add(stackPane, i * 2, j * 2 + 1);
}
/**
* Creates a horizontal line on the GridPane, it also paints it to red or blue if it is black.
*/
private void createHorizontalLine(){
Line line = new Line(0, 0, 100, 0);
line.setStrokeWidth(3);
StackPane stackPane = new StackPane(line);
stackPane.setOnMouseClicked(event -> {
if (event.getButton() == MouseButton.PRIMARY && playerONE && line.getStroke() == Color.BLACK) {
line.setStroke(Color.RED);
playerONE = false;
} else if (event.getButton() == MouseButton.PRIMARY && !playerONE && line.getStroke() == Color.BLACK) {
line.setStroke(Color.BLUE);
playerONE = true;
}
});
gridPane.add(stackPane, i * 2 + 1, j * 2);
}
}