Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.Stack;
- import java.util.stream.IntStream;
- /*
- This is what it crashes with. I've marked the points it throws errors at.
- Exception in thread "main" java.lang.StackOverflowError
- at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
- at java.util.stream.MatchOps$MatchOp.evaluateSequential(Unknown Source)
- at java.util.stream.MatchOps$MatchOp.evaluateSequential(Unknown Source)
- at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
- at java.util.stream.IntPipeline.allMatch(Unknown Source)
- */
- public class Driver {
- @SuppressWarnings({ "rawtypes", "unchecked", "resource", "unused" })
- public static void main(String[] args) throws FileNotFoundException {
- int[][] puzzle = new int[9][9];
- Stack rowStack = new Stack();
- Stack colStack = new Stack();
- File puzzles = new File("puzzles.txt");
- if (!puzzles.exists()) {
- System.out.println("The file has gone missing.");
- System.exit(0);
- }
- Scanner puzzleFile = new Scanner(puzzles);
- String puzzleName = puzzleFile.nextLine();
- while (puzzleFile.hasNextInt()) {
- for (int i = 0; i < puzzle.length; i++) {
- for (int j = 0; j < puzzle.length; j++) {
- int num = puzzleFile.nextInt();
- puzzle[i][j] = num;
- }
- }
- }
- int num = 0;
- for (int k = 0; k < puzzle.length; k++) {
- rowStack.push(puzzle[num][k]);
- }
- for (int l = 0; l < puzzle.length; l++) {
- colStack.push(puzzle[l][num]);
- }
- rowStack.pop();
- // solve(puzzle, num);
- printArray(puzzle);
- if (!solve(puzzle, num)) {
- System.out.println(Arrays.deepToString(puzzle).replace("],", "]\n").replace("[[", "[").replace("]]", "]"));
- }
- // System.out.println(printArray(puzzle));
- // System.out.println("rowStack: " + rowStack);
- // System.out.println("colStack: " + colStack);
- // System.out.println("Thing: " + rowStack.contains(7));
- }
- public static boolean rowFind(int rowNum, int puzzle[][]) {
- /*
- * for (int i = 0; i < puzzle.length; i++) { if (puzzle[i][rowNum] == rowNum) {
- * return true; } } return false;
- */
- boolean[] constraint = new boolean[puzzle.length];
- return IntStream.range(rowNum, puzzle.length).allMatch(column -> checkAll(puzzle, rowNum, constraint, column));
- } // This crashes it.
- public static boolean columnFind(int colNum, int puzzle[][]) {
- /*
- * for (int i = 0; i < puzzle.length; i++) { if (puzzle[colNum][i] == colNum) {
- * return true; } } return false;
- */
- boolean[] constraint = new boolean[puzzle.length];
- return IntStream.range(colNum, puzzle.length).allMatch(row -> checkAll(puzzle, row, constraint, colNum));
- }
- private static boolean boxCheck(int rowNum, int colNum, int puzzle[][]) {
- /*
- * int row = rowNum - rowNum % 3; int col = colNum - colNum % 3;
- *
- * for (int i = row; i < col + 3; i++) { for (int j = col; j < col + 3; j++) {
- * if (puzzle[i][j] == num) { return true; } } }
- *
- * return false;
- */
- boolean[] constraint = new boolean[puzzle.length];
- int rowStart = (rowNum / 3) * 3;
- int rowEnd = rowStart + 3;
- int columnStart = (colNum / 3) * 3;
- int columnEnd = columnStart + 3;
- for (int r = rowStart; r < rowEnd; r++) {
- for (int c = columnStart; c < columnEnd; c++) {
- if (!checkAll(puzzle, r, constraint, c)) {
- return false;
- }
- }
- }
- return true;
- }
- // private static boolean isGood(int row, int col, int num, int puzzle[][]) {
- private static boolean isGood(int rowNum, int colNum, int num, int[][] puzzle) {
- // return rowFind(row, puzzle) && columnFind(col, puzzle) && boxCheck(row, col,
- // puzzle, num);
- return (rowFind(rowNum, puzzle) && columnFind(colNum, puzzle) && boxCheck(rowNum, colNum, puzzle));
- } //this also crashes it.
- public static boolean checkAll(int[][] puzzle, int rowNum, boolean[] constraint, int colNum) {
- if (puzzle[rowNum][colNum] != 0) {
- if (!constraint[puzzle[rowNum][colNum] - 1]) {
- constraint[puzzle[rowNum][colNum] - 1] = true;
- } else {
- return false;
- }
- }
- return true;
- }
- public static boolean solve(int puzzle[][], int num) {
- /*
- * for (int row = 0; row < puzzle.length; row++) { for (int col = 0; col <
- * puzzle.length; col++) { if (puzzle[row][col] == 0) { for (int number = 1;
- * number <= puzzle.length; number++) { if (isGood(row, col, number, puzzle)) {
- * puzzle[row][col] = number;
- *
- * if (solve(puzzle, number)) { return true; } else { puzzle[row][col] = 0; } }
- * }
- *
- * return false; } } }
- *
- * return true;
- */
- for (int row = 0; row < puzzle.length; row++) {
- for (int column = 0; column < puzzle.length; column++) {
- if (puzzle[row][column] == 0) {
- for (int k = 0; k <= 0; k++) {
- puzzle[row][column] = k;
- if (isGood(row, column, k, puzzle) && solve(puzzle, k)) {
- return true; //This throws over 1000 errors.
- }
- puzzle[row][column] = 0;
- }
- return false;
- }
- }
- }
- return true;
- }
- public static String printArray(int puzzle[][]) {
- return Arrays.deepToString(puzzle).replace("],", "]\n").replace("[[", "[").replace("]]", "]");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement