public ArrayList<Node> getSuccessors(Node parent, int nX, int nY, Memory mem) { ArrayList<Node> successors = new ArrayList<Node>(); // up if (mem.validPosition(nX, nY - 1)) { successors.add(new Node(parent, nX, nY - 1, 'u')); } // down if (mem.validPosition(nX, nY + 1)) { successors.add(new Node(parent, nX, nY + 1, 'd')); } // left if (mem.validPosition(nX - 1, nY)) { successors.add(new Node(parent, nX - 1, nY, 'l')); } // right if (mem.validPosition(nX + 1, nY)) { successors.add(new Node(parent, nX + 1, nY, 'r')); } // Stay still successors.add(new Node(parent, nX, nY, this.getHeading())); return successors; }
public void run(PPP ppp, int maxMoves, boolean verbose, boolean showSteps) { short[] pos = this.getPos(); if ((pos[0] > (ppp.size * 2)) || (pos[1] > ppp.size)) { System.err.println("Bot started on an invalid Position!"); System.err.printf( "Position: %d,%d :: PPP is %dx%d, ignoring the boundary wall\n", pos[0], pos[1], 2 * ppp.size, ppp.size); System.exit(1); } short goalX = (short) (ppp.size * 2); short goalY = ppp.size; int moves = this.route_taken.size(); this.aprioriPlan(goalX, goalY); // If we have a limited memory, we won't be able to see the route taken across the whole map // So create an overview map (which won't be used by the bot) to print the route on later Memory routeMap = null; if (this.currentMem instanceof LimitedMemory) { routeMap = new Memory(ppp); } if (verbose) { if (this.planned_route.size() > 0) { if (this.apriori != null) { System.out.println("\nPlanned route via A Priori knowledge"); this.apriori.prettyPrintRoute(this.planned_route); // this.printPlannedRoute(); } } System.out.println(); } while (!this.state.isPos(goalX, goalY)) { if (this.currentMem instanceof LimitedMemory) { this.currentMem.rePlot(this.getPos()); } this.sense(ppp); // this.currentMem.prettyPrintRoute(route_taken); try { this.plan(); } catch (Exception e) { System.out.println("Route Taken"); this.currentMem.prettyPrintRoute(this.route_taken); System.out.println("\n\nRoute Planned"); this.currentMem.prettyPrintRoute(this.planned_route); System.out.println("\n\n Reachability"); this.currentMem.prettyPrintRoute(this.route_taken, true); System.err.print(e); e.printStackTrace(); System.exit(1); } try { // this.move(ppp); if (showSteps) { this.currentMem.prettyPrintRoute(this.route_taken); System.out.printf("Step %d\n\n", moves); Thread.sleep(this.STEP_TIME); } this.move(ppp); } catch (InvalidMoveError e) { if (verbose) { System.err.println(e + " -- replanning\n"); } } catch (InterruptedException e) { e.printStackTrace(); } moves++; if (moves >= maxMoves) { break; } } // Sense around goal pos to tidy up the map if (this.currentMem instanceof LimitedMemory) { this.currentMem.rePlot(this.getPos()); } this.sense(ppp); boolean success = false; if (this.state.isPos(goalX, goalY)) { if (verbose) { System.out.printf("Reached Goal %d,%d in %d moves\n", goalX, goalY, moves); } success = true; } else { if (verbose) { System.out.printf("Failed to reach goal in %d moves\n", moves); } } if (verbose) { if (routeMap == null) { routeMap = this.currentMem; } System.out.println("\nRoute Taken"); routeMap.prettyPrintRoute(route_taken); System.out.println("\nReachability Map"); routeMap.prettyPrintRoute(null, true); // this.printTakenRoute(); } this.finished(moves, success); }