HitoriMitMaven/src/main/java/GUI/HitoriBoardPanel.java

95 lines
3.3 KiB
Java

package GUI;
import domain.GameSolver;
import domain.HitoriGameMoves;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.GridPane;
import java.util.Set;
public class HitoriBoardPanel extends GridPane {
private final HitoriGameMoves gameMoves;
private final GameSolver gameSolver;
private final GameUIController controller;
public HitoriBoardPanel(HitoriGameMoves gameMoves, GameSolver gameSolver, GameUIController controller) {
this.gameMoves = gameMoves;
this.gameSolver = gameSolver;
this.controller = controller;
setHgap(5);
setVgap(5);
setPadding(new Insets(10));
updateBoard();
}
public void updateBoard() {
getChildren().clear();
int[][] boardState = gameMoves.getBoard();
boolean[][] blackCells = gameMoves.getBlackCells();
boolean[][] whiteCells = gameMoves.getWhiteCells();
for (int row = 0; row < boardState.length; row++) {
for (int col = 0; col < boardState[row].length; col++) {
Button cellButton = createCellButton(row, col, boardState[row][col],
blackCells[row][col], whiteCells[row][col]);
add(cellButton, col, row);
}
}
}
public void showErrors() {
var errors = gameSolver.findIncorrectBlackMarks();
Set<String> errorPositions = controller.convertErrorsToSet(errors);
getChildren().clear();
int[][] boardState = gameMoves.getBoard();
boolean[][] blackCells = gameMoves.getBlackCells();
boolean[][] whiteCells = gameMoves.getWhiteCells();
for (int row = 0; row < boardState.length; row++) {
for (int col = 0; col < boardState[row].length; col++) {
Button cellButton = createCellButton(row, col, boardState[row][col],
blackCells[row][col], whiteCells[row][col]);
if (errorPositions.contains(row + "," + col) && blackCells[row][col]) {
cellButton.setStyle("-fx-background-color: red; -fx-text-fill: white;");
}
add(cellButton, col, row);
}
}
}
private Button createCellButton(int row, int col, int value, boolean isBlack, boolean isWhite) {
Button cellButton = new Button(String.valueOf(value));
cellButton.setPrefSize(50, 50);
styleCellButton(cellButton, isBlack, isWhite);
cellButton.setOnMouseClicked(event -> {
if (!controller.isPaused()) {
if (event.getButton() == MouseButton.PRIMARY) {
controller.handleLeftClick(row, col);
} else if (event.getButton() == MouseButton.SECONDARY) {
controller.handleRightClick(row, col);
}
}
});
return cellButton;
}
private void styleCellButton(Button button, boolean isBlack, boolean isWhite) {
if (isBlack) {
button.setStyle("-fx-background-color: black; -fx-text-fill: white;");
} else if (isWhite) {
button.setStyle("-fx-background-color: white; -fx-text-fill: black; -fx-border-color: gray;");
} else {
button.setStyle("-fx-background-color: lightgray; -fx-text-fill: black;");
}
}
}