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

added file load functionality

parent 71a92a62
Branches
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.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
......@@ -31,18 +32,21 @@ import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.VLineTo;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
final static public int N = 6;
private Scene scene;
private Stage stage;
final private double GRID_PERCENTAGE = 0.75;
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 HBox previous = null;
......@@ -111,6 +115,7 @@ public class Main extends Application {
square.setBottomStroke(3, Color.BLACK);
square.setLeftStroke(3, Color.BLACK);
square.setRightStroke(3, Color.BLACK);
square.resizeLines(size);
return square;
}
......@@ -137,14 +142,18 @@ public class Main extends Application {
topHBox.getChildren().addAll(loadGameFileButton, showMistakesButton, loadGameTextInputButton);
final String PATH = "C:\\Java Eclipse Workspace\\Programming II Coursework\\mathdokuCage.txt";
loadGameFileButton.setOnAction(e -> {
try {
BufferedReader file = new BufferedReader(new FileReader(PATH));
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text Files", "*.txt"));
File selectedFile = fileChooser.showOpenDialog(stage);
BufferedReader file = new BufferedReader(new FileReader(selectedFile));
String line;
while ((line = file.readLine()) != null) {
System.out.println(line);
createNewCage(line);
}
file.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
......@@ -155,6 +164,24 @@ public class Main extends Application {
return topHBox;
}
private void createNewCage(String line) {
String[] splitLine = line.split(" ");
String[] shiftedIds = splitLine[1].split(",");
int[] ids = new int[shiftedIds.length];
for (int i = 0; i < shiftedIds.length; i++) {
ids[i] = Integer.parseInt(shiftedIds[i]) - 1;
}
if (splitLine[0].contains("÷")) {
splitLine[0] = splitLine[0].replace("÷", "");
}
int value = Integer.parseInt(splitLine[0].substring(0, splitLine[0].length()- 1));
String operator = splitLine[0].substring(splitLine[0].length() - 1);
new Cage(ids, value, operator);
}
private HBox setupBottomHBox() {
HBox bottomHBox = new HBox();
bottomHBox.setSpacing(10);
......@@ -207,25 +234,6 @@ public class Main extends Application {
});
}
private void setupCages() {
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) {
double newValue = isWider ? scene.getHeight() : scene.getWidth();
for (int i = 0; i < N; i++) {
......@@ -258,8 +266,6 @@ public class Main extends Application {
GridPane gridPane = setupGrid();
setupCages();
HBox topHBox = setupTopHBox();
HBox bottomHBox = setupBottomHBox();
......
......@@ -40,7 +40,8 @@ public class Square {
}
public void resizeLines(double newSize) {
top.setEndX(newSize);
top.setEndX(newSize );
right.setStartX(newSize);
right.setEndX(newSize);
right.setEndY(newSize);
......@@ -48,6 +49,7 @@ public class Square {
bottom.setStartX(newSize);
bottom.setStartY(newSize);
bottom.setEndY(newSize);
left.setStartY(newSize);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment