Пример #1
0
 private Grid solve(Grid grid) {
   if (grid.getRemainingSquares() == 0 && grid.getRemainingCandidates() == 0) {
     return grid;
   }
   Set<Move> moves = grid.findCandidateMoves();
   if (moves.isEmpty()) {
     return null;
   }
   Move bestMove = moves.iterator().next();
   if (bestMove.remainingSquares == 0 && bestMove.remainingCandidates == 0) {
     grid.printCandidates();
     System.out.println("\n" + bestMove + "\n");
     bestMove.executeMove(grid);
     return grid;
   }
   for (Move move : moves) {
     System.out.println("\n" + move + "\n");
     Grid clone = new Grid(grid);
     move.executeMove(clone);
     Grid result = clone.solve();
     if (result != null) {
       return result;
     }
   }
   return null;
 }
Пример #2
0
 public static Set<Move> findLegalMoves(Grid grid, int depth) {
   if (grid.getRemainingSquares() == 0 || depth == 0) {
     return null;
   } else {
     Set<Move> candidateMoves = grid.findCandidateMoves();
     Set<Move> legalMoves = new LinkedHashSet<Move>(candidateMoves);
     for (Move move : candidateMoves) {
       Grid clone = new Grid(grid);
       move.executeMove(clone);
       Set<Move> nextMoves = findLegalMoves(grid, depth - 1);
       if (nextMoves != null && nextMoves.isEmpty()) {
         legalMoves.remove(candidateMoves);
       }
     }
     return candidateMoves;
   }
 }
Пример #3
0
 public void executeMove(Grid grid) {
   grid.setValue(this.row, this.col, this.value);
   remainingSquares = grid.getRemainingSquares();
   remainingCandidates = grid.getRemainingCandidates();
 }