Example #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);
  }
Example #2
0
  @Test
  public void testHashing() {
    Map<Position, Integer> map = new HashMap<Position, Integer>();
    for (int i = 0; i < 16; i++) {
      for (int j = 0; j < 16; j++) {
        map.put(Position.get(i, j), 16 * i + j);
      }
    }

    int count = 0;
    for (int i = 0; i < 16; i++) {
      for (int j = 0; j < 16; j++) {
        assertEquals(map.get(Position.get(i, j)), new Integer(count));
        count++;
      }
    }
  }
Example #3
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);
    }
  }
Example #4
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);
    }
  }
Example #5
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));
 }
Example #6
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);
    }
  }
Example #7
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);
 }
Example #8
0
public class TypeUniversalTests {

  PositionMap<Integer> map = new PositionMap<Integer>(Position.get(9, 9));

  int[] su1 = {
    9, 5, 8, 3, 1, 2, 7, 6, 4, 4, 6, 1, 5, 7, 9, 8, 2, 3, 3, 7, 2, 4, 6, 8, 9, 5, 1, 8, 9, 6, 1, 2,
    3, 5, 4, 7, 1, 4, 3, 7, 9, 5, 2, 8, 6, 5, 2, 7, 6, 8, 4, 3, 1, 9, 7, 8, 5, 9, 4, 1, 6, 3, 2, 2,
    1, 9, 8, 3, 6, 4, 7, 5, 6, 3, 4, 2, 5, 7, 1, 9, 8
  };

  @Test
  public void initialisationTest() {
    TypeBasic t;
    try {
      t = new TestUniSudoku(null);
      fail();
    } catch (Exception e) {
    }
    try {
      Map<String, ConstraintBehavior> m = new HashMap<String, ConstraintBehavior>();
      m.put("abcd", new UniqueConstraintBehavior());
      t = new TestUniSudoku(m);
    } catch (Exception e) {
    }
    try {
      Map<String, ConstraintBehavior> m = new HashMap<String, ConstraintBehavior>();

      m.put("abcd", null);
      t = new TestUniSudoku(m);
      fail();
    } catch (Exception e) {
    }
    try {
      Map<String, ConstraintBehavior> m = new HashMap<String, ConstraintBehavior>();
      m.put(null, null);
      t = new TestUniSudoku(m);
      fail();
    } catch (Exception e) {
    }
    try {
      Map<String, ConstraintBehavior> m = new HashMap<String, ConstraintBehavior>();
      m.put("abc", null);
      t = new TestUniSudoku(m);
      fail();
    } catch (Exception e) {
    }

    assertTrue(3 != Math.pow(2 + 0.0, 2.0));
    assertTrue(4 == Math.pow(2 + 0.0, 2.0));
  }

  @Test
  public void addExtraConstraintTest() {
    Map<String, ConstraintBehavior> m = new HashMap<String, ConstraintBehavior>();
    TypeUniversal t = new TestUniSudoku(m);
    try {
      t.addExtraConstraint(null, '9', null);
      fail();
    } catch (Exception e) {
    }
    try {
      t.addExtraConstraint("abcd", '4', null);
      fail();
    } catch (Exception e) {
    }
    try {
      t.addExtraConstraint("abc", '4', null);
      fail();
    } catch (Exception e) {
    }
    try {
      t.addExtraConstraint("abc", '4', new UniqueConstraintBehavior());
      fail();
    } catch (Exception e) {
    }
  }

  @Test
  public void addBlockConstraintsTest() {
    Map<String, ConstraintBehavior> m = new HashMap<String, ConstraintBehavior>();
    TypeUniversal t = new TestUniSudoku(m);
    try {
      t.addBlockConstraints(null, null);
      fail();
    } catch (Exception e) {
    }
    try {
      t.addBlockConstraints("abcd", null);
      fail();
    } catch (Exception e) {
    }
    try {
      t.addBlockConstraints("abc", null);
      fail();
    } catch (Exception e) {
    }
    try {
      t.addBlockConstraints("abc", new UniqueConstraintBehavior());
      fail();
    } catch (Exception e) {
    }
  }
}