"ip/git@git.soton.ac.uk:soclabs/chipkit_flow.git" did not exist on "c6259cafc1dc9cf9c30ac1ba23142125eb05a99a"
Select Git revision
Handler.java
Handler.java 8.72 KiB
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
public class Handler {
private Window window = new Window();
private int n = 4;
private int[][] cellValues = new int[n][n];
public void helpButton(Button help, GridPane grid) {
help.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
if(help.getText().equals("Enable Help")) {
help.setText("Disable Help");
highlight(grid);
for(Node cell : grid.getChildren()) {
enableHighlight((Cell) cell, grid);
}
} else {
help.setText("Enable Help");
setTransparent(grid);
}
}
});
}
public void submitButton(Button submit) {
submit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
for(int i = 0; i < getN(); i ++) {
for(int j = 0; j < getN(); j ++) {
System.out.print(getCellValues()[i][j]);
}
System.out.println();
}
if(verify()) {
window.win("Congratulations!", "You won the game!");
} else {
window.win("Too bad...", "You lost the game");
}
}
});
}
public void clearBoard(Button clear, GridPane grid) {
clear.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
window.getAlertDialog("Are you sure you want to clear the board?").showAndWait().ifPresent(response -> {
if(response == ButtonType.YES) {
clear(grid);
}
});
}
});
}
public void doubleClick(Cell cell) {
cell.getTextField().setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if(mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
if(mouseEvent.getClickCount() == 2) {
window.showButtons(cell, getN());
}
}
}
});
}
public void memoriseValues(Cell cell) {
cell.getTextField().textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observableValue, String oldValue,
String newValue) {
if (cell.isNumeric(newValue)) {
if (Integer.parseInt(newValue) > getN() || Integer.parseInt(newValue) == 0) {
cell.getTextField().clear();
} else {
getCellValues()[cell.getRow()][cell.getColumn()] = Integer.parseInt(newValue);
}
} else if (!newValue.matches("\\d*")) {
cell.getTextField().setText(newValue.replaceAll("[^\\d]", ""));
}
}
});
}
public void enableHighlight(Cell cell, GridPane grid) {
cell.getTextField().textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
setTransparent(grid);
highlight(grid);
}
});
}
public void draw(GridPane grid) {
for(int i = 0; i < getN(); i ++) {
for (int j = 0; j < getN(); j++) {
Cell cell = new Cell(getN(), i, j);
drawLines(i, j, cell);
doubleClick(cell);
memoriseValues(cell);
grid.add(cell, j, i, 1, 1);
}
}
}
public void drawLines(int i, int j, Cell cell) {
cell.setStyle("-fx-border-color: black; -fx-border-width: 1 1 0 0;");
if(j == 0) {
cell.setStyle("-fx-border-color: black; -fx-border-width: 1 1 0 1;");
}
if(i == getN() - 1 && j == 0) {
cell.setStyle("-fx-border-color: black; -fx-border-width: 1 1 1 1;");
}
if( i == getN() - 1 && j != 0) {
cell.setStyle("-fx-border-color: black; -fx-border-width: 1 1 1 0;");
}
}
public void clear(GridPane grid) {
for(int i =0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
cellValues[i][j] = 0;
getCell(grid, i, j).getTextField().clear();
getCell(grid, i, j).setBackground(new Background(new BackgroundFill(Color.TRANSPARENT,
CornerRadii.EMPTY, Insets.EMPTY)));
}
}
}
public void setTransparent(GridPane grid) {
for(Node cell : grid.getChildren()) {
((Cell) cell).setBackground(new Background(new BackgroundFill(Color.TRANSPARENT,
CornerRadii.EMPTY, Insets.EMPTY)));
}
}
public void highlight(GridPane grid) {
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
if(cellValues[i][j] != 0) {
if(!verifyRow(cellValues[i][j], i)) {
highlightRow(i, grid, true);
}
if(!verifyColumn(cellValues[i][j], j)) {
highlightColumn(j, grid, true);
}
}
}
}
}
public boolean verify() {
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
if(cellValues[i][j] !=0 ) {
if(verifyRow(cellValues[i][j], i) == false || verifyColumn(cellValues[i][j], j) == false) {
System.out.println(cellValues[i][j] + " " + i + " " + j);
return false;
}
}
}
}
return true;
}
public boolean verifyRow(int x, int row) {
int count = 0;
for(int i = 0; i < n; i ++) {
if(cellValues[row][i] == x) {
count++;
}
}
if(count == 1) {
return true;
}
return false;
}
public boolean verifyColumn(int x, int column) {
int count = 0;
for(int i = 0; i < n; i ++) {
if(cellValues[i][column] == x) {
count++;
}
}
if(count == 1) {
return true;
}
return false;
}
public void highlightRow(int row, GridPane grid, boolean highlight) {
if(highlight) {
for(int i = 0; i < n; i ++) {
getCell(grid, row, i).setBackground(new Background(new BackgroundFill(Color.YELLOW,
CornerRadii.EMPTY, Insets.EMPTY)));
}
} else {
for(int i = 0; i < n; i ++) {
getCell(grid, row, i).setBackground(new Background(new BackgroundFill(Color.TRANSPARENT,
CornerRadii.EMPTY, Insets.EMPTY)));
}
}
}
public void highlightColumn(int column, GridPane grid, boolean highlight) {
if(highlight) {
for(int i = 0; i < n; i ++) {
getCell(grid, i, column).setBackground(new Background(new BackgroundFill(Color.YELLOW,
CornerRadii.EMPTY, Insets.EMPTY)));
}
} else {
for(int i = 0; i < n; i ++) {
getCell(grid, i, column).setBackground(new Background(new BackgroundFill(Color.TRANSPARENT,
CornerRadii.EMPTY, Insets.EMPTY)));
}
}
}
public Cell getCell(GridPane grid, int i, int j) {
for(Node cell : grid.getChildren()) {
if(((Cell) cell).getRow() == i && ((Cell) cell).getColumn() == j) {
return (Cell) cell;
}
}
return null;
}
public int[][] getCellValues() {
return cellValues;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
}