コード例 #1
0
ファイル: SolverTests.java プロジェクト: SDEagle/SudoQ
  @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);
    }
  }
コード例 #2
0
ファイル: SolverTests.java プロジェクト: SDEagle/SudoQ
  @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);
    }
  }