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

regex for input & change N added for game load

parent 52327c69
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -8,6 +8,9 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
......@@ -174,12 +177,12 @@ public class Main extends Application {
topHBox.setAlignment(Pos.BOTTOM_CENTER);
topHBox.setSpacing(10);
Button loadGameFileButton = new Button("Load game - file");
Button loadGameFileButton = new Button("Load Game - File");
CheckBox showMistakesCheck = new CheckBox("Show mistakes");
if (showMistakes) {
showMistakesCheck.setSelected(true);
}
Button loadGameTextInputButton = new Button("Load game - text input");
Button loadGameTextInputButton = new Button("Load Game - Text");
loadGameFileButton.setPrefWidth(200);
loadGameTextInputButton.setPrefWidth(loadGameFileButton.getPrefWidth());
......@@ -348,21 +351,7 @@ public class Main extends Application {
}
}
///////////////////////////////////////////////////////////
private void generateRandomGameEvent() {
// new popup selecting difficultly and n
Stage popupStage = new Stage();
popupStage.initModality(Modality.APPLICATION_MODAL);
popupStage.initOwner(stage);
VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setAlignment(Pos.TOP_CENTER);
HBox hboxBottom = new HBox();
hboxBottom.setSpacing(86);
hboxBottom.setPadding(new Insets(10));
private Slider setupSlider() {
Slider slider = new Slider(2,8,1);
slider.setPrefWidth(150);
slider.setMaxWidth(150);
......@@ -374,7 +363,10 @@ public class Main extends Application {
slider.setSnapToTicks(true);
slider.setValue(N);
slider.setPadding(new Insets(10, 0, 0, 0));
return slider;
}
private HBox setupRadioHBox() {
HBox hboxMiddle = new HBox();
ToggleGroup tg = new ToggleGroup();
RadioButton easy = new RadioButton("Easy");
......@@ -389,14 +381,31 @@ public class Main extends Application {
radioVBox.setSpacing(5);
hboxMiddle.getChildren().addAll(new Label("Difficulty:"), radioVBox);
return hboxMiddle;
}
///////////////////////////////////////////////////////////
private void generateRandomGameEvent() {
Stage popupStage = new Stage();
popupStage.initModality(Modality.APPLICATION_MODAL);
popupStage.initOwner(stage);
VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setAlignment(Pos.TOP_CENTER);
HBox hboxBottom = new HBox();
hboxBottom.setSpacing(86);
hboxBottom.setPadding(new Insets(10));
Slider slider = setupSlider();
Button generateButton = new Button("Generate");
generateButton.setAlignment(Pos.TOP_RIGHT);
generateButton.setOnAction(e -> {
popupStage.close();
if ((int) slider.getValue() != N) {
N = (int) slider.getValue();
resetGrid();
}
RandomGenerator rg = new RandomGenerator(N);
gridCages = rg.generate();
});
......@@ -411,10 +420,8 @@ public class Main extends Application {
hboxTop.setSpacing(3);
hboxTop.setAlignment(Pos.CENTER);
hboxBottom.getChildren().addAll(cancelButton, generateButton);
vbox.getChildren().addAll(hboxTop, hboxMiddle, hboxBottom);
vbox.getChildren().addAll(hboxTop, setupRadioHBox(), hboxBottom);
popupStage.setScene(new Scene(vbox, 250, 250));
popupStage.show();
......@@ -427,16 +434,39 @@ public class Main extends Application {
File selectedFile = fileChooser.showOpenDialog(stage);
BufferedReader file = new BufferedReader(new FileReader(selectedFile));
String line;
while ((line = file.readLine()) != null) {
List<String> fileLines = file.lines().collect(Collectors.toList());
int max = -1;
for (String line : fileLines) {
String[] rightHalfLine = line.split(" ")[1].split(",");
int[] lineIds = new int[rightHalfLine.length];
for (int i = 0; i < lineIds.length; i++) {
lineIds[i] = Integer.parseInt(rightHalfLine[i]);
if (lineIds[i] > max) {
max = lineIds[i];
}
}
}
if (Math.sqrt(max) != N) {
N = (int) Math.sqrt(max);
resetGrid();
}
for (String line : fileLines) {
createNewCage(line);
}
file.close();
} catch (FileNotFoundException e1) {
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
}
catch (IOException e2) {
e2.printStackTrace();
}
catch (NullPointerException e3) {}
}
private void loadTextButtonClickEvent() {
......@@ -446,10 +476,17 @@ public class Main extends Application {
VBox vbox = new VBox();
vbox.setPadding(new Insets(10));
vbox.setSpacing(10);
vbox.setSpacing(5);
HBox hboxBottom = new HBox();
hboxBottom.setSpacing(163);
HBox hbox = new HBox();
hbox.setSpacing(162);
HBox hboxTop = new HBox();
hboxTop.setSpacing(5);
hboxTop.setAlignment(Pos.CENTER);
Slider slider = setupSlider();
TextArea textArea = new TextArea();
Button cancelButton = new Button("Cancel");
......@@ -460,6 +497,10 @@ public class Main extends Application {
Button loadButton = new Button("Load");
loadButton.setAlignment(Pos.TOP_RIGHT);
loadButton.setOnAction(e -> {
if ((int) slider.getValue() != N) {
N = (int) slider.getValue();
resetGrid();
}
String[] lines = textArea.getText().split("\n");
for (String line : lines) {
if (!line.equals(null) && !line.equals("")) {
......@@ -469,8 +510,10 @@ public class Main extends Application {
popupStage.hide();
});
hbox.getChildren().addAll(cancelButton, loadButton);
vbox.getChildren().addAll(textArea, hbox);
Label sizeLabel = new Label("Size (NxN):");
hboxTop.getChildren().addAll(sizeLabel, slider);
hboxBottom.getChildren().addAll(cancelButton, loadButton);
vbox.getChildren().addAll(hboxTop, textArea, hboxBottom);
popupStage.setScene(new Scene(vbox, 300, 300));
popupStage.show();
......@@ -612,11 +655,36 @@ public class Main extends Application {
private void textFieldDataChange(int i, int j) {
if (!clearing && !undoing) {
boolean validInput = true;
try {
int textInput = Integer.parseInt(gridNumbers[i*N + j].getText());
if (textInput < 1) {
validInput = false;
gridNumbers[i*N + j].setText("");
}
else if (textInput > N) {
validInput = false;
gridNumbers[i*N + j].setText(gridNumbers[i*N + j].getText().substring(0, 1));
}
}
catch (NumberFormatException ex) {
validInput = false;
if (gridNumbers[i*N + j].getText().length() == 2) {
gridNumbers[i*N + j].setText(gridNumbers[i*N + j].getText().substring(0, 1));
}
else {
gridNumbers[i*N + j].setText("");
}
}
if (validInput) {
undoStack.push(new GameState(GameState.getCurrentGameState(gridNumbers)));
if (undoButton.isDisabled()) {
undoButton.setDisable(false);
}
}
}
constraints.checkConstraints(j, i, showMistakes);;
constraints.checkWinCondition();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment