示例#1
0
  public static void main(String[] args) throws IOException {
    /*
    grid.setValues(0, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(1, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(2, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});

    grid.setValues(3, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(4, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(5, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});

    grid.setValues(6, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(7, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    grid.setValues(8, new int [] {0, 0, 0,  0, 0, 0,  0, 0, 0});
    */

    final StringBuilder buf = new StringBuilder();
    FileUtil.readFile(
        "puzzle.txt",
        new LineVisitor() {

          @Override
          public void processLine(String line) throws IOException {
            buf.append(line + "\n");
          }
        });

    Grid grid = new Grid(buf.toString());

    printGrid(grid);
    // grid.printCandidateMoves();
    // System.out.println();
    // Grid clone = new Grid(grid);
    // printGrid(clone);
    printGrid(grid.solve());
  }
示例#2
0
  public void step() {
    Settings.AdvanceMoveNumber();
    System.out.println(Settings.getMoveNumber());
    Grid<Chess_Actor> gr = getGrid();
    ArrayList<Chess_Actor> actors = new ArrayList<Chess_Actor>();
    for (Location loc : gr.getOccupiedLocations()) actors.add(gr.get(loc));

    for (Chess_Actor a : actors) {
      // only act if another actor hasn't removed a
      // if (a.getGrid() == gr)
      //    a.act();
    }
  }
示例#3
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;
   }
 }
示例#4
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;
 }
示例#5
0
 public void executeMove(Grid grid) {
   grid.setValue(this.row, this.col, this.value);
   remainingSquares = grid.getRemainingSquares();
   remainingCandidates = grid.getRemainingCandidates();
 }
示例#6
0
  public static void main(String[] args) {
    // Grid Successor function
    GridSuccessorFunction function = new GridSuccessorFunction();
    // new Grid
    Grid puzzle = new Grid(11, 7);
    // setup some blocks and goal and robot position
    puzzle.setRobot(2, 5);
    puzzle.setGoal(8, 5);
    puzzle.setBlock(0, 2, 1, 2);
    puzzle.setBlock(1, 2, 1, 3);
    puzzle.setBlock(1, 2, 2, 2);
    puzzle.setBlock(2, 0, 2, 1);
    puzzle.setBlock(2, 1, 2, 2);
    puzzle.setBlock(2, 1, 3, 1);
    puzzle.setBlock(1, 3, 1, 4);
    puzzle.setBlock(1, 4, 2, 4);
    puzzle.setBlock(0, 4, 1, 4);
    puzzle.setBlock(2, 4, 2, 5);
    puzzle.setBlock(2, 5, 3, 5);
    puzzle.setBlock(2, 5, 2, 6);
    puzzle.setBlock(4, 0, 4, 1);
    puzzle.setBlock(5, 0, 5, 1);
    puzzle.setBlock(6, 0, 6, 1);
    puzzle.setBlock(4, 1, 4, 2);
    puzzle.setBlock(5, 1, 5, 2);
    puzzle.setBlock(6, 1, 6, 2);
    puzzle.setBlock(4, 3, 5, 3);
    puzzle.setBlock(5, 3, 5, 4);
    puzzle.setBlock(5, 3, 6, 3);
    puzzle.setBlock(4, 5, 4, 6);
    puzzle.setBlock(4, 4, 4, 5);
    puzzle.setBlock(5, 4, 5, 5);
    puzzle.setBlock(6, 4, 6, 5);
    puzzle.setBlock(5, 5, 5, 6);
    puzzle.setBlock(6, 5, 6, 6);
    puzzle.setBlock(8, 0, 8, 1);
    puzzle.setBlock(7, 1, 8, 1);
    puzzle.setBlock(8, 1, 8, 2);
    puzzle.setBlock(8, 2, 9, 2);
    puzzle.setBlock(9, 2, 9, 3);
    puzzle.setBlock(9, 2, 10, 2);
    puzzle.setBlock(9, 3, 9, 4);
    puzzle.setBlock(9, 4, 10, 4);
    puzzle.setBlock(8, 4, 9, 4);
    puzzle.setBlock(8, 4, 8, 5);
    puzzle.setBlock(7, 5, 8, 5);
    puzzle.setBlock(8, 5, 8, 6);

    // searching stuff
    // A* agenda
    Agenda<Grid> agenda = new AgendaListA<Grid>();
    // Searching framework with correct MOVES,PUZZLE and FUNCTION
    SearchingFramework<RobotMove, Grid, GridSuccessorFunction> search =
        new SearchingFramework<RobotMove, Grid, GridSuccessorFunction>(function, puzzle, agenda);

    System.out.println(puzzle);
    search.Search(); // do the search
    List<RobotMove> result = new ArrayList<RobotMove>();
    result.addAll(search.getResult());
    // do the moves and print state
    for (RobotMove robotMove : result) {
      puzzle.makeMove(robotMove);
      System.out.println(robotMove);
      System.out.println(puzzle);
    }
    System.out.println(result);
  }