konkon / Config.java
Config.java
Raw
import javafx.application.Platform;
import javafx.scene.control.Alert;

import java.io.*;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class Config {

    BufferedReader reader;
    Alert error = new Alert(Alert.AlertType.ERROR);
    ArrayList<Integer> cellsToCheck = new ArrayList<>();
    List<String> configLines;
    ArrayList<Character> validOperators = new ArrayList<>();
    ArrayList<CageConfig> cageConfigs = new ArrayList<>();

    Config(String input) {
        error.setTitle("Puzzle Loading Error");
        validOperators.add('+');
        validOperators.add('*');
        validOperators.add('x');
        validOperators.add('/');
        validOperators.add('-');
        validOperators.add('÷');
        configLines = Arrays.asList(input.split("\n"));
    }

    Config(File file) {
        error.setTitle("Puzzle Loading Error");
        validOperators.add('+');
        validOperators.add('*');
        validOperators.add('x');
        validOperators.add('/');
        validOperators.add('-');
        validOperators.add('÷');
        configLines = checkConfigFromFile(file);

    }

    public ArrayList<CageConfig> getCages() {
        if (checkInputValidity(configLines)) {
            return cageConfigs;
        } else {
            return null;
        }
    }

    public ArrayList<String> checkConfigFromFile(File file) {

        ArrayList<String> localConfig = new ArrayList<>();
        cellsToCheck = new ArrayList<>();
        try {
            reader = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8));
        } catch (NullPointerException | IOException e) {
            error.setHeaderText("File Not Found");
            error.setContentText("I'm impressed you even triggered this");
            Platform.runLater(() -> {
                error.showAndWait();
            });
            return null;
        }

        String line = getLine();
        while (line != null) {
            localConfig.add(line);
            line = getLine();
        }
        return localConfig;
    }

    // checks whole string to see if its valid
    public boolean checkInputValidity(List<String> lines) {
        int count = 1;
        for (String configLine : lines) {
            if (!isLineValid(configLine)) {
                error.setHeaderText("Error reading config");
                error.setContentText("On line " + count + " there was a formatting error\n" + configLine);
                Platform.runLater(() -> {
                    error.showAndWait();
                });
                return false;
            }
            count++;
        }
        if (cellsToCheck.size() == 0) {
            error.setHeaderText("Error reading file");
            error.setContentText("No Cells Found");
            Platform.runLater(() -> {
                error.showAndWait();
            });
            return false;
        }

        Collections.sort(cellsToCheck);
        int largestNumb = cellsToCheck.get(cellsToCheck.size() - 1);
        if (!(largestNumb % Math.sqrt(largestNumb) == 0)) {
            error.setHeaderText("Error reading file");
            error.setContentText("File provided does not describe a MathDoku grid (not square)");
            Platform.runLater(() -> {
                error.showAndWait();
            });
            return false;
        } else if (cellsToCheck.size() != cellsToCheck.get(cellsToCheck.size() - 1)) {
            error.setHeaderText("Error reading file");
            error.setContentText("File provided does not describe a MathDoku grid (not square)");
            Platform.runLater(() -> {
                error.showAndWait();
            });
            return false;
        }

        for (String line : lines) {
            String[] cage = line.split(" ")[1].split(",");
            int[] cells = new int[cage.length];
            for (int i = 0; i < cage.length; i++) {
                cells[i] = Integer.parseInt(cage[i]);
            }

            for (int cell : cells) {
                if (cells.length != 1) {
                    boolean adjFound = false;
                    double n = Math.sqrt(largestNumb);
                    for (int adjCell : cells) {
                        if ((cell == (adjCell + 1) && (adjCell % n != 0))
                                || ((cell == (adjCell - 1)) && (cell % n != 0))
                                || cell == (adjCell + n)
                                || cell == (adjCell - n)) {
                            adjFound = true;
                            break;
                        }
                    }
                    if (!adjFound) {
                        error.setHeaderText("Error reading file");
                        error.setContentText("Cage contains non-adjacent cells\n" + line);
                        Platform.runLater(() -> {
                            error.showAndWait();
                        });
                        return false;
                    }
                }
            }
        }
        return true;
    }

    // checks every line to see if its valid
    public boolean isLineValid(String line) {
        Character operator = null;
        Integer target = null;
        ArrayList<Integer> cageCells = new ArrayList<>();

        String[] lineSplit = line.split(" ");

        if (lineSplit.length != 2) {
            return false;
        }

        String[] cells = lineSplit[1].split(",");
        for (String cell : cells) {
            if (isNumber(cell) && !cellsToCheck.contains(Integer.parseInt(cell))) {
                if (Integer.parseInt(cell) > 0) {
                    cellsToCheck.add(Integer.parseInt(cell));
                    cageCells.add(Integer.parseInt(cell));
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        if (lineSplit[1].split(",").length == 1 && !isNumber(lineSplit[0])) {
            return false;
        } else if (!isNumber(lineSplit[0])) {
            operator = lineSplit[0].charAt(lineSplit[0].length() - 1);
            if (!isNumber(lineSplit[0].substring(0, lineSplit[0].length() - 1))) {
                return false;
            } else if(!validOperators.contains(operator)) {
                return false;
            }
            target = Integer.parseInt(lineSplit[0].substring(0, lineSplit[0].length() - 1));
        } else if (isNumber(lineSplit[0])) {
            target = Integer.parseInt(lineSplit[0]);
            operator = 's';
        }
        if(cells.length != 1 && operator == 's') {
            return false;
        }
        cageConfigs.add(new CageConfig(operator, target, cageCells));
        return true;
    }

    boolean isNumber(String number) {
        try {
            Integer d = Integer.parseInt(number);
            return true;
        } catch (NumberFormatException nfe) {
            return false;
        }
    }

    String getLine() {
        try {
            return this.reader.readLine();
        } catch (IOException e) {
            System.out.println(e);
            return "";
        }
    }

    public ArrayList<CageConfig> getCageConfigs() {
        return cageConfigs;
    }
}