Skip to content
Snippets Groups Projects
Commit 39ce81d8 authored by Nimrod Abramovich's avatar Nimrod Abramovich
Browse files

Cage updated to include info

parent 92752605
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
package coursework;
import java.util.Arrays;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
public class Cage {
private int[][] squareIds;
private int[] squareIds;
private int value;
private String operator;
private Square[][] gridSquares;
private Label[][] cageLabels;
public Cage(int[][] squareIds) {
public Cage(int[] squareIds, int value, String operator) {
this.squareIds = squareIds;
this.value = value;
this.operator = operator;
this.gridSquares = Main.getGridSquares();
this.cageLabels = Main.getCageLabels();
buildCage();
setCageInfo();
}
public void buildCage() {
for (int aIndex[] : squareIds) {
for (int bIndex[] : squareIds) {
if (!aIndex.equals(bIndex)) {
int rowDiff = aIndex[1] - bIndex[1];
int colDiff = aIndex[0] - bIndex[0];
if (squareIds.length == 1)
return;
for (int idA : squareIds) {
for (int idB : squareIds) {
if (idA != idB) {
int[] aCoord = idToCoord(idA);
int[] bCoord = idToCoord(idB);
int rowDiff = aCoord[1] - bCoord[1];
int colDiff = aCoord[0] - bCoord[0];
final double WIDTH = 2;
if (rowDiff == -1 && colDiff == 0) {
gridSquares[aIndex[0]][aIndex[1]].setRightStroke(2, Color.LIGHTGREY);
gridSquares[aCoord[0]][aCoord[1]].setRightStroke(WIDTH, Color.LIGHTGREY);
}
else if (rowDiff == 1 && colDiff == 0) {
gridSquares[aIndex[0]][aIndex[1]].setLeftStroke(2, Color.LIGHTGREY);
gridSquares[aCoord[0]][aCoord[1]].setLeftStroke(WIDTH, Color.LIGHTGREY);
}
else if (rowDiff == 0 && colDiff == -1) {
gridSquares[aIndex[0]][aIndex[1]].setBottomStroke(2, Color.LIGHTGREY);
gridSquares[aCoord[0]][aCoord[1]].setBottomStroke(WIDTH, Color.LIGHTGREY);
}
else if (rowDiff == 0 && colDiff == 1) {
gridSquares[aIndex[0]][aIndex[1]].setTopStroke(2, Color.LIGHTGREY);
gridSquares[aCoord[0]][aCoord[1]].setTopStroke(WIDTH, Color.LIGHTGREY);
}
}
}
}
}
public void setCageInfo() {
if (squareIds.length > 1)
Arrays.sort(squareIds);
int minVal = squareIds[0];
cageLabels[minVal][0].setText(value + " " + operator);
cageLabels[minVal][1].setText(value + " " + operator);
cageLabels[minVal][1].setVisible(false);
}
public static int[] idToCoord(int id) {
int row = id / Main.N;
int col = id % Main.N;
return new int[] {row, col};
}
}
\ No newline at end of file
......@@ -8,10 +8,12 @@ import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
......@@ -28,15 +30,16 @@ import javafx.stage.Stage;
public class Main extends Application {
final static public int N = 6;
final private double GRID_PERCENTAGE = 0.75;
final static private int N = 4;
private static Square[][] gridSquares = new Square[N][N];
private TextField[] gridNumbers = new TextField[N*N];
private static Label[][] cageLabels = new Label[N*N][2];
private Rectangle gridOutline;
Scene scene;
private boolean isWider;
private Rectangle previous = null;
private HBox previous = null;
private GridPane setupGrid() {
GridPane gridPane = new GridPane();
......@@ -56,8 +59,7 @@ public class Main extends Application {
private GridPane addToGridPane(GridPane gridPane, double size, int i, int j) {
Square square = setupGridSquare(size);
TextField textField = setupGridTextField(size);
HBox hbox = setupGridHBox(square, textField);
HBox hbox = setupGridHBox(square, textField, i, j);
Group group = square.getGroup();
gridSquares[i][j] = square;
......@@ -70,23 +72,27 @@ public class Main extends Application {
return gridPane;
}
private HBox setupGridHBox(Square square, TextField textField) {
private HBox setupGridHBox(Square square, TextField textField, int i, int j) {
HBox hbox = new HBox();
hbox.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
square.getRectangle().setFill(Color.LIGHTBLUE);
if (previous != null) {
previous.setFill(Color.WHITE);
}
previous = square.getRectangle();
});
VBox vbox = new VBox();
Label cageInfoLabel = new Label("");
Label balanceLabel = new Label("");
hbox.getChildren().add(textField);
cageLabels[i*N + j][0] = cageInfoLabel;
cageLabels[i*N + j][1] = balanceLabel;
vbox.getChildren().addAll(cageInfoLabel, textField, balanceLabel);
vbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(vbox);
hbox.setAlignment(Pos.CENTER);
hboxClickSquareEvent(hbox);
return hbox;
}
private Rectangle setupGridOutline(double size) {
gridOutline = new Rectangle(size*N + N*2.5, size*N + N*2.5, size*N + N*2.5, size*N + N*2.5);
gridOutline = new Rectangle(size*N + N*3, size*N + N*3, size*N + N*3, size*N + N*3);
gridOutline.setStroke(Color.BLACK);
gridOutline.setFill(Color.TRANSPARENT);
gridOutline.setStrokeWidth(2);
......@@ -96,10 +102,10 @@ public class Main extends Application {
private Square setupGridSquare(double size) {
Square square = new Square(size);
square.setTopStroke(2.5, Color.BLACK);
square.setBottomStroke(2.5, Color.BLACK);
square.setLeftStroke(2.5, Color.BLACK);
square.setRightStroke(2.5, Color.BLACK);
square.setTopStroke(3, Color.BLACK);
square.setBottomStroke(3, Color.BLACK);
square.setLeftStroke(3, Color.BLACK);
square.setRightStroke(3, Color.BLACK);
return square;
}
......@@ -125,6 +131,11 @@ public class Main extends Application {
loadGameTextInputButton.setPrefWidth(loadGameFileButton.getPrefWidth());
topHBox.getChildren().addAll(loadGameFileButton, showMistakesButton, loadGameTextInputButton);
loadGameFileButton.setOnAction(e -> {
});
return topHBox;
}
......@@ -144,6 +155,16 @@ public class Main extends Application {
return bottomHBox;
}
private void hboxClickSquareEvent(HBox hbox) {
hbox.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
if (previous != null) {
previous.setStyle("-fx-border-width:0px;");
}
hbox.setStyle("-fx-border-color:red; -fx-border-width:2px;");
previous = hbox;
});
}
private void clearButtonClickEvent(Button clearButton) {
clearButton.setOnAction(e -> {
for (TextField textField : gridNumbers) {
......@@ -171,13 +192,22 @@ public class Main extends Application {
}
private void setupCages() {
new Cage(new int[][] {{0,0}, {1,0}});
new Cage(new int[][] {{2,0}, {1,1}, {2,1}});
new Cage(new int[][] {{3,0}, {3,1}});
new Cage(new int[][] {{0,1}, {0,2}});
new Cage(new int[][] {{1,2}});
new Cage(new int[][] {{0,3},{1,3}});
new Cage(new int[][] {{2,2},{2,3},{3,2},{3,3}});
new Cage(new int[] {0,6},11,"+");
new Cage(new int[] {1,2},2,"/");
new Cage(new int[] {3,9},20,"x");
new Cage(new int[] {7,8},3,"-");
new Cage(new int[] {4,5,11,17},6,"x");
new Cage(new int[] {10, 16},3,"/");
new Cage(new int[] {12,13,18,19},240,"x");
new Cage(new int[] {14,15},6,"x");
new Cage(new int[] {24,25},6,"x");
new Cage(new int[] {20,26},6,"x");
new Cage(new int[] {21,27,28},7,"+");
new Cage(new int[] {22,23},30,"x");
new Cage(new int[] {30,31,32},8,"+");
new Cage(new int[] {33,34},2,"/");
new Cage(new int[] {29,35},9,"+");
}
public void resizeGrid(double gridX, double gridY, double gridWidth) {
......@@ -190,14 +220,18 @@ public class Main extends Application {
gridSquares[i][j].resizeLines(newValue * GRID_PERCENTAGE / N);
}
}
gridOutline.setWidth(newValue * GRID_PERCENTAGE + N*2.5);
gridOutline.setHeight(newValue * GRID_PERCENTAGE + N*2.5);
//gridOutline.setWidth(newValue * GRID_PERCENTAGE + N*2.5);
//gridOutline.setHeight(newValue * GRID_PERCENTAGE + N*2.5);
}
public static Square[][] getGridSquares() {
return gridSquares;
}
public static Label[][] getCageLabels() {
return cageLabels;
}
@Override
public void start(Stage stage) throws Exception {
VBox vBox = new VBox();
......@@ -218,8 +252,8 @@ public class Main extends Application {
stageWidthResizeEvent(stage, gridPane);
stageHeightResizeEvent(stage, gridPane);
stage.setMinWidth(600);
stage.setMinHeight(600);
stage.setMinWidth(400 + 50*N);
stage.setMinHeight(400 + 50*N);
stage.setScene(scene);
stage.setTitle("Mathdoku");
stage.show();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment