示例#1
0
  @Test
  public void testNoConstraintSaturation() {
    sudoku.getField(Position.get(0, 0)).setCurrentValue(0);
    sudoku.getField(Position.get(1, 0)).setCurrentValue(0);

    sudoku.setComplexity(Complexity.arbitrary);
    Solver solver = new Solver(sudoku);
    assertEquals(solver.validate(null, false), ComplexityRelation.INVALID);
  }
示例#2
0
  @Test
  public void testSolveOneManuallyApplied() {
    sudoku.getField(Position.get(0, 0)).setCurrentValue(0);
    sudoku.getField(Position.get(5, 0)).setCurrentValue(6);
    sudoku.getField(Position.get(7, 0)).setCurrentValue(8);
    sudoku.getField(Position.get(1, 1)).setCurrentValue(2);
    sudoku.getField(Position.get(4, 1)).setCurrentValue(1);
    sudoku.getField(Position.get(8, 1)).setCurrentValue(7);
    sudoku.getField(Position.get(2, 2)).setCurrentValue(8);
    sudoku.getField(Position.get(3, 2)).setCurrentValue(5);
    sudoku.getField(Position.get(6, 2)).setCurrentValue(4);
    sudoku.getField(Position.get(2, 3)).setCurrentValue(4);
    sudoku.getField(Position.get(3, 3)).setCurrentValue(2);
    sudoku.getField(Position.get(6, 3)).setCurrentValue(8);
    sudoku.getField(Position.get(1, 4)).setCurrentValue(0);
    sudoku.getField(Position.get(4, 4)).setCurrentValue(7);
    sudoku.getField(Position.get(8, 4)).setCurrentValue(1);
    sudoku.getField(Position.get(0, 5)).setCurrentValue(5);
    sudoku.getField(Position.get(5, 5)).setCurrentValue(3);
    sudoku.getField(Position.get(0, 6)).setCurrentValue(2);
    sudoku.getField(Position.get(7, 6)).setCurrentValue(0);
    sudoku.getField(Position.get(1, 7)).setCurrentValue(3);
    sudoku.getField(Position.get(8, 7)).setCurrentValue(6);
    sudoku.getField(Position.get(2, 8)).setCurrentValue(6);
    sudoku.getField(Position.get(6, 8)).setCurrentValue(2);

    Solution solution = new Solution();
    while (solution != null) {
      solution = solver.solveOne(false);
      if (solution == null) break;
      Iterator<SolveDerivation> it = solution.getDerivationIterator();
      SolveDerivation sd = null;
      while (it.hasNext()) {
        sd = it.next();
      }
      if (solution.getAction() != null) {
        solution.getAction().execute();
        assertTrue(
            this.sudoku.getField(sd.getFieldIterator().next().getPosition()).getCurrentValue()
                != Field.EMPTYVAL);
      } else {
        solution = null;
      }
    }

    for (Field f : this.sudoku) {
      assertTrue(f.getCurrentValue() != -1);
    }
  }
示例#3
0
  @Test
  public void testStandard16x16No2() {
    sudoku16x16.getField(Position.get(0, 0)).setCurrentValue(0);
    sudoku16x16.getField(Position.get(3, 0)).setCurrentValue(1);
    sudoku16x16.getField(Position.get(4, 0)).setCurrentValue(2);
    sudoku16x16.getField(Position.get(5, 0)).setCurrentValue(3);
    sudoku16x16.getField(Position.get(8, 0)).setCurrentValue(11);
    sudoku16x16.getField(Position.get(10, 0)).setCurrentValue(5);
    sudoku16x16.getField(Position.get(14, 0)).setCurrentValue(6);
    sudoku16x16.getField(Position.get(2, 1)).setCurrentValue(7);
    sudoku16x16.getField(Position.get(6, 1)).setCurrentValue(6);
    sudoku16x16.getField(Position.get(9, 1)).setCurrentValue(2);
    sudoku16x16.getField(Position.get(12, 1)).setCurrentValue(8);
    sudoku16x16.getField(Position.get(13, 1)).setCurrentValue(9);
    sudoku16x16.getField(Position.get(14, 1)).setCurrentValue(5);
    sudoku16x16.getField(Position.get(15, 1)).setCurrentValue(10);
    sudoku16x16.getField(Position.get(1, 2)).setCurrentValue(11);
    sudoku16x16.getField(Position.get(4, 2)).setCurrentValue(9);
    sudoku16x16.getField(Position.get(7, 2)).setCurrentValue(0);
    sudoku16x16.getField(Position.get(9, 2)).setCurrentValue(12);
    sudoku16x16.getField(Position.get(11, 2)).setCurrentValue(10);
    sudoku16x16.getField(Position.get(14, 2)).setCurrentValue(13);
    sudoku16x16.getField(Position.get(0, 3)).setCurrentValue(2);
    sudoku16x16.getField(Position.get(3, 3)).setCurrentValue(14);
    sudoku16x16.getField(Position.get(4, 3)).setCurrentValue(1);
    sudoku16x16.getField(Position.get(7, 3)).setCurrentValue(13);
    sudoku16x16.getField(Position.get(11, 3)).setCurrentValue(8);
    sudoku16x16.getField(Position.get(14, 3)).setCurrentValue(11);
    sudoku16x16.getField(Position.get(0, 4)).setCurrentValue(12);
    sudoku16x16.getField(Position.get(4, 4)).setCurrentValue(7);
    sudoku16x16.getField(Position.get(7, 4)).setCurrentValue(9);
    sudoku16x16.getField(Position.get(9, 4)).setCurrentValue(11);
    sudoku16x16.getField(Position.get(10, 4)).setCurrentValue(1);
    sudoku16x16.getField(Position.get(12, 4)).setCurrentValue(0);
    sudoku16x16.getField(Position.get(13, 4)).setCurrentValue(14);
    sudoku16x16.getField(Position.get(1, 5)).setCurrentValue(10);
    sudoku16x16.getField(Position.get(2, 5)).setCurrentValue(6);
    sudoku16x16.getField(Position.get(3, 5)).setCurrentValue(5);
    sudoku16x16.getField(Position.get(7, 5)).setCurrentValue(15);
    sudoku16x16.getField(Position.get(11, 5)).setCurrentValue(14);
    sudoku16x16.getField(Position.get(14, 5)).setCurrentValue(4);
    sudoku16x16.getField(Position.get(15, 5)).setCurrentValue(12);
    sudoku16x16.getField(Position.get(3, 6)).setCurrentValue(9);
    sudoku16x16.getField(Position.get(5, 6)).setCurrentValue(4);
    sudoku16x16.getField(Position.get(6, 6)).setCurrentValue(14);
    sudoku16x16.getField(Position.get(9, 6)).setCurrentValue(3);
    sudoku16x16.getField(Position.get(11, 6)).setCurrentValue(7);
    sudoku16x16.getField(Position.get(14, 6)).setCurrentValue(10);
    sudoku16x16.getField(Position.get(0, 7)).setCurrentValue(15);
    sudoku16x16.getField(Position.get(3, 7)).setCurrentValue(4);
    sudoku16x16.getField(Position.get(4, 7)).setCurrentValue(8);
    sudoku16x16.getField(Position.get(5, 7)).setCurrentValue(11);
    sudoku16x16.getField(Position.get(8, 7)).setCurrentValue(0);
    sudoku16x16.getField(Position.get(14, 7)).setCurrentValue(7);
    sudoku16x16.getField(Position.get(1, 8)).setCurrentValue(1);
    sudoku16x16.getField(Position.get(7, 8)).setCurrentValue(12);
    sudoku16x16.getField(Position.get(10, 8)).setCurrentValue(11);
    sudoku16x16.getField(Position.get(11, 8)).setCurrentValue(4);
    sudoku16x16.getField(Position.get(12, 8)).setCurrentValue(7);
    sudoku16x16.getField(Position.get(15, 8)).setCurrentValue(2);
    sudoku16x16.getField(Position.get(1, 9)).setCurrentValue(12);
    sudoku16x16.getField(Position.get(4, 9)).setCurrentValue(14);
    sudoku16x16.getField(Position.get(6, 9)).setCurrentValue(2);
    sudoku16x16.getField(Position.get(9, 9)).setCurrentValue(13);
    sudoku16x16.getField(Position.get(10, 9)).setCurrentValue(7);
    sudoku16x16.getField(Position.get(12, 9)).setCurrentValue(15);
    sudoku16x16.getField(Position.get(0, 10)).setCurrentValue(4);
    sudoku16x16.getField(Position.get(1, 10)).setCurrentValue(7);
    sudoku16x16.getField(Position.get(4, 10)).setCurrentValue(0);
    sudoku16x16.getField(Position.get(8, 10)).setCurrentValue(1);
    sudoku16x16.getField(Position.get(12, 10)).setCurrentValue(12);
    sudoku16x16.getField(Position.get(13, 10)).setCurrentValue(8);
    sudoku16x16.getField(Position.get(14, 10)).setCurrentValue(14);
    sudoku16x16.getField(Position.get(2, 11)).setCurrentValue(11);
    sudoku16x16.getField(Position.get(3, 11)).setCurrentValue(3);
    sudoku16x16.getField(Position.get(5, 11)).setCurrentValue(5);
    sudoku16x16.getField(Position.get(6, 11)).setCurrentValue(15);
    sudoku16x16.getField(Position.get(8, 11)).setCurrentValue(12);
    sudoku16x16.getField(Position.get(11, 11)).setCurrentValue(6);
    sudoku16x16.getField(Position.get(15, 11)).setCurrentValue(4);
    sudoku16x16.getField(Position.get(1, 12)).setCurrentValue(2);
    sudoku16x16.getField(Position.get(4, 12)).setCurrentValue(11);
    sudoku16x16.getField(Position.get(8, 12)).setCurrentValue(5);
    sudoku16x16.getField(Position.get(11, 12)).setCurrentValue(3);
    sudoku16x16.getField(Position.get(12, 12)).setCurrentValue(10);
    sudoku16x16.getField(Position.get(15, 12)).setCurrentValue(15);
    sudoku16x16.getField(Position.get(1, 13)).setCurrentValue(6);
    sudoku16x16.getField(Position.get(4, 13)).setCurrentValue(15);
    sudoku16x16.getField(Position.get(6, 13)).setCurrentValue(4);
    sudoku16x16.getField(Position.get(8, 13)).setCurrentValue(13);
    sudoku16x16.getField(Position.get(11, 13)).setCurrentValue(0);
    sudoku16x16.getField(Position.get(14, 13)).setCurrentValue(1);
    sudoku16x16.getField(Position.get(0, 14)).setCurrentValue(10);
    sudoku16x16.getField(Position.get(1, 14)).setCurrentValue(0);
    sudoku16x16.getField(Position.get(2, 14)).setCurrentValue(14);
    sudoku16x16.getField(Position.get(3, 14)).setCurrentValue(8);
    sudoku16x16.getField(Position.get(6, 14)).setCurrentValue(12);
    sudoku16x16.getField(Position.get(9, 14)).setCurrentValue(1);
    sudoku16x16.getField(Position.get(13, 14)).setCurrentValue(13);
    sudoku16x16.getField(Position.get(1, 15)).setCurrentValue(13);
    sudoku16x16.getField(Position.get(5, 15)).setCurrentValue(10);
    sudoku16x16.getField(Position.get(7, 15)).setCurrentValue(1);
    sudoku16x16.getField(Position.get(10, 15)).setCurrentValue(12);
    sudoku16x16.getField(Position.get(11, 15)).setCurrentValue(2);
    sudoku16x16.getField(Position.get(12, 15)).setCurrentValue(4);
    sudoku16x16.getField(Position.get(15, 15)).setCurrentValue(11);

    sudoku16x16.setComplexity(Complexity.arbitrary);
    Solver solver = new Solver(sudoku16x16);
    assertEquals(solver.validate(solution16x16, false), ComplexityRelation.CONSTRAINT_SATURATION);

    // copy solution to current value
    for (int j = 0; j < sudoku16x16.getSudokuType().getSize().getY(); j++) {
      for (int i = 0; i < sudoku16x16.getSudokuType().getSize().getX(); i++) {
        sudoku16x16
            .getField(Position.get(i, j))
            .setCurrentValue(solution16x16.get(Position.get(i, j)));
      }
    }

    // check constraints
    for (Constraint c : sudoku16x16.getSudokuType()) {
      assertTrue(c.isSaturated(sudoku16x16));
    }

    // print solution if wanted
    System.out.println("Solution (16x16) - Complexity: " + solver.sudoku.getComplexityValue());
    if (PRINT_SOLUTIONS) {
      StringBuilder sb = new StringBuilder();
      for (int j = 0; j < sudoku16x16.getSudokuType().getSize().getY(); j++) {
        for (int i = 0; i < sudoku16x16.getSudokuType().getSize().getX(); i++) {
          int value = sudoku16x16.getField(Position.get(i, j)).getCurrentValue();
          String op = value + "";
          if (value < 10) op = " " + value;
          if (value == -1) op = " x";
          sb.append(op + ", ");
        }
        sb.append("\n");
      }
      System.out.println(sb);
    }
  }
示例#4
0
 @Test
 public void solveAllIncorrect() {
   sudoku.getField(Position.get(0, 0)).setCurrentValue(0);
   sudoku.getField(Position.get(1, 0)).setCurrentValue(0);
   assertFalse(solver.solveAll(true, false));
 }
示例#5
0
  @Test
  public void testSolveAllManuallyApplied() {
    sudoku.getField(Position.get(0, 0)).setCurrentValue(0);
    sudoku.getField(Position.get(5, 0)).setCurrentValue(6);
    sudoku.getField(Position.get(7, 0)).setCurrentValue(8);
    sudoku.getField(Position.get(1, 1)).setCurrentValue(2);
    sudoku.getField(Position.get(4, 1)).setCurrentValue(1);
    sudoku.getField(Position.get(8, 1)).setCurrentValue(7);
    sudoku.getField(Position.get(2, 2)).setCurrentValue(8);
    sudoku.getField(Position.get(3, 2)).setCurrentValue(5);
    sudoku.getField(Position.get(6, 2)).setCurrentValue(4);
    sudoku.getField(Position.get(2, 3)).setCurrentValue(4);
    sudoku.getField(Position.get(3, 3)).setCurrentValue(2);
    sudoku.getField(Position.get(6, 3)).setCurrentValue(8);
    sudoku.getField(Position.get(1, 4)).setCurrentValue(0);
    sudoku.getField(Position.get(4, 4)).setCurrentValue(7);
    sudoku.getField(Position.get(8, 4)).setCurrentValue(1);
    sudoku.getField(Position.get(0, 5)).setCurrentValue(5);
    sudoku.getField(Position.get(5, 5)).setCurrentValue(3);
    sudoku.getField(Position.get(0, 6)).setCurrentValue(2);
    sudoku.getField(Position.get(7, 6)).setCurrentValue(0);
    sudoku.getField(Position.get(1, 7)).setCurrentValue(3);
    sudoku.getField(Position.get(8, 7)).setCurrentValue(6);
    sudoku.getField(Position.get(2, 8)).setCurrentValue(6);
    sudoku.getField(Position.get(6, 8)).setCurrentValue(2);

    solver.solveAll(true, false);
    List<Solution> solutions = solver.getSolutions();
    for (Solution solution : solutions) {
      assertTrue(solution.getAction() != null);
      solution.getAction().execute();
      Iterator<SolveDerivation> it = solution.getDerivationIterator();
      SolveDerivation sd = null;
      while (it.hasNext()) {
        sd = it.next();
        assertTrue(sd != null);
      }
    }

    SudokuTestUtilities.printSudoku(sudoku);

    for (Field f : this.sudoku) {
      assertTrue(f.getCurrentValue() != Field.EMPTYVAL);
    }
  }
示例#6
0
 @Test
 public void solveOneIncorrect() {
   sudoku.getField(Position.get(0, 0)).setCurrentValue(0);
   sudoku.getField(Position.get(1, 0)).setCurrentValue(0);
   assertTrue(solver.solveOne(true) == null);
 }