Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6. import java.util.stream.IntStream;
  7.  
  8. /*
  9. This is what it crashes with. I've marked the points it throws errors at.
  10. Exception in thread "main" java.lang.StackOverflowError
  11.     at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
  12.     at java.util.stream.MatchOps$MatchOp.evaluateSequential(Unknown Source)
  13.     at java.util.stream.MatchOps$MatchOp.evaluateSequential(Unknown Source)
  14.     at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
  15.     at java.util.stream.IntPipeline.allMatch(Unknown Source)
  16. */
  17.  
  18. public class Driver {
  19.  
  20.     @SuppressWarnings({ "rawtypes", "unchecked", "resource", "unused" })
  21.     public static void main(String[] args) throws FileNotFoundException {
  22.  
  23.         int[][] puzzle = new int[9][9];
  24.         Stack rowStack = new Stack();
  25.         Stack colStack = new Stack();
  26.         File puzzles = new File("puzzles.txt");
  27.         if (!puzzles.exists()) {
  28.             System.out.println("The file has gone missing.");
  29.             System.exit(0);
  30.         }
  31.  
  32.         Scanner puzzleFile = new Scanner(puzzles);
  33.         String puzzleName = puzzleFile.nextLine();
  34.  
  35.         while (puzzleFile.hasNextInt()) {
  36.  
  37.             for (int i = 0; i < puzzle.length; i++) {
  38.                 for (int j = 0; j < puzzle.length; j++) {
  39.                     int num = puzzleFile.nextInt();
  40.                     puzzle[i][j] = num;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         int num = 0;
  46.  
  47.         for (int k = 0; k < puzzle.length; k++) {
  48.             rowStack.push(puzzle[num][k]);
  49.         }
  50.  
  51.         for (int l = 0; l < puzzle.length; l++) {
  52.             colStack.push(puzzle[l][num]);
  53.         }
  54.  
  55.         rowStack.pop();
  56.         // solve(puzzle, num);
  57.         printArray(puzzle);
  58.  
  59.         if (!solve(puzzle, num)) {
  60.             System.out.println(Arrays.deepToString(puzzle).replace("],", "]\n").replace("[[", "[").replace("]]", "]"));
  61.         }
  62.  
  63.         // System.out.println(printArray(puzzle));
  64.         // System.out.println("rowStack: " + rowStack);
  65.         // System.out.println("colStack: " + colStack);
  66.         // System.out.println("Thing: " + rowStack.contains(7));
  67.     }
  68.  
  69.     public static boolean rowFind(int rowNum, int puzzle[][]) {
  70.         /*
  71.          * for (int i = 0; i < puzzle.length; i++) { if (puzzle[i][rowNum] == rowNum) {
  72.          * return true; } } return false;
  73.          */
  74.         boolean[] constraint = new boolean[puzzle.length];
  75.         return IntStream.range(rowNum, puzzle.length).allMatch(column -> checkAll(puzzle, rowNum, constraint, column));
  76.     } // This crashes it.
  77.  
  78.     public static boolean columnFind(int colNum, int puzzle[][]) {
  79.         /*
  80.          * for (int i = 0; i < puzzle.length; i++) { if (puzzle[colNum][i] == colNum) {
  81.          * return true; } } return false;
  82.          */
  83.         boolean[] constraint = new boolean[puzzle.length];
  84.         return IntStream.range(colNum, puzzle.length).allMatch(row -> checkAll(puzzle, row, constraint, colNum));
  85.     }
  86.  
  87.     private static boolean boxCheck(int rowNum, int colNum, int puzzle[][]) {
  88.         /*
  89.          * int row = rowNum - rowNum % 3; int col = colNum - colNum % 3;
  90.          *
  91.          * for (int i = row; i < col + 3; i++) { for (int j = col; j < col + 3; j++) {
  92.          * if (puzzle[i][j] == num) { return true; } } }
  93.          *
  94.          * return false;
  95.          */
  96.         boolean[] constraint = new boolean[puzzle.length];
  97.         int rowStart = (rowNum / 3) * 3;
  98.         int rowEnd = rowStart + 3;
  99.  
  100.         int columnStart = (colNum / 3) * 3;
  101.         int columnEnd = columnStart + 3;
  102.  
  103.         for (int r = rowStart; r < rowEnd; r++) {
  104.             for (int c = columnStart; c < columnEnd; c++) {
  105.                 if (!checkAll(puzzle, r, constraint, c)) {
  106.                     return false;
  107.                 }
  108.  
  109.             }
  110.         }
  111.         return true;
  112.  
  113.     }
  114.  
  115.     // private static boolean isGood(int row, int col, int num, int puzzle[][]) {
  116.     private static boolean isGood(int rowNum, int colNum, int num, int[][] puzzle) {
  117.         // return rowFind(row, puzzle) && columnFind(col, puzzle) && boxCheck(row, col,
  118.         // puzzle, num);
  119.  
  120.         return (rowFind(rowNum, puzzle) && columnFind(colNum, puzzle) && boxCheck(rowNum, colNum, puzzle));
  121.     } //this also crashes it.
  122.  
  123.     public static boolean checkAll(int[][] puzzle, int rowNum, boolean[] constraint, int colNum) {
  124.  
  125.         if (puzzle[rowNum][colNum] != 0) {
  126.             if (!constraint[puzzle[rowNum][colNum] - 1]) {
  127.                 constraint[puzzle[rowNum][colNum] - 1] = true;
  128.             } else {
  129.                 return false;
  130.             }
  131.         }
  132.         return true;
  133.     }
  134.  
  135.     public static boolean solve(int puzzle[][], int num) {
  136.         /*
  137.          * for (int row = 0; row < puzzle.length; row++) { for (int col = 0; col <
  138.          * puzzle.length; col++) { if (puzzle[row][col] == 0) { for (int number = 1;
  139.          * number <= puzzle.length; number++) { if (isGood(row, col, number, puzzle)) {
  140.          * puzzle[row][col] = number;
  141.          *
  142.          * if (solve(puzzle, number)) { return true; } else { puzzle[row][col] = 0; } }
  143.          * }
  144.          *
  145.          * return false; } } }
  146.          *
  147.          * return true;
  148.          */
  149.         for (int row = 0; row < puzzle.length; row++) {
  150.             for (int column = 0; column < puzzle.length; column++) {
  151.                 if (puzzle[row][column] == 0) {
  152.                     for (int k = 0; k <= 0; k++) {
  153.                         puzzle[row][column] = k;
  154.                         if (isGood(row, column, k, puzzle) && solve(puzzle, k)) {
  155.                             return true; //This throws over 1000 errors.
  156.                         }
  157.                         puzzle[row][column] = 0;
  158.                     }
  159.                     return false;
  160.                 }
  161.             }
  162.         }
  163.         return true;
  164.     }
  165.  
  166.     public static String printArray(int puzzle[][]) {
  167.         return Arrays.deepToString(puzzle).replace("],", "]\n").replace("[[", "[").replace("]]", "]");
  168.     }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement