public Position chooseAction(PacmanState state) {
    if (plan != null){
        return plan.pop();
    }
    int counter = 0;
    //...

    while(!toVisit.isEmpty()){
        counter += 1;

        //...
    }
    System.out.println("Visited states: " + counter);
    plan = new Stack<>();
    // ...
}