0

I need help for my homework. The homework is to write a recursion method, which solve every simple level from the game "Sokoban". It should be done with the Cloneable interface and hashSets. That is why I'm asking this. I already have the recursion method written in my code, but it always gets me a StackoverflowError.

I hope you can help me!

import java.util.HashSet;

public class Solver implements Cloneable {
    HashSet<Level> fieldSet;
    public Solver () {
        fieldSet = new HashSet<Level>();
    }
        public final static char WALL = '#';                                
        public final static char PLAYER = '@';
        public final static char GOAL = '.';
        public final static char CHEST = '$';
        public final static char CHESTONGOAL = '*';
        public final static char PLAYERONGOAL = '+';
        public final static char FLOOR = ' ';

    public String solve (Level l) {
        Level fieldUp = l.clone();
        Player playerUp = fieldUp.getPlayer();
        playerUp.move(0, -1);
        if (this.fieldSet.add(fieldUp)) {
            if (!fieldUp.isCompleted()) {
                return "up, " + this.solve(fieldUp);
            } else {
                return "up.";
            }
        }

        Level fieldRight = l.clone();
        Player playerRight = fieldRight.getPlayer();
        playerRight.move(1, 0);
        if (this.fieldSet.add(fieldRight)) {
            if (!fieldRight.isCompleted()) {
                return "right, " + this.solve(fieldRight);
            } else {
                return "right.";
            }
        }
        Level fieldDown = l.clone();
        Player playerDown = fieldDown.getPlayer();
        playerDown.move(0, 1);
        if (this.fieldSet.add(fieldDown)) {
            if (!fieldDown.isCompleted()) {
                return "down, " + this.solve(fieldDown);
            } else {
                return "down.";
            }
        }
        Level fieldLeft = l.clone();
        Player playerLeft = fieldLeft.getPlayer();
        playerLeft.move(-1, 0);
        if (this.fieldSet.add(fieldLeft)) {
            if (!fieldLeft.isCompleted()) {
                return "left, " + this.solve(fieldLeft);
            } else {
                return "left.";
            }
        }
        return "";
    }
}
raz0light
  • 11
  • 1
  • 2
    That means you're recursing too many times before finally returning. The problem you're trying to solve is too "big" for a non-optimized recursion method. – Carcigenicate Feb 04 '17 at 15:23
  • Your solve logic appears to be flawed, e.g. if you move up, you'll never backtrack and try another direction. As for the StackOverflowError, try adding a `println()` statement inside `move()` to show every step taken. I'll likely help you figure out what is wrong. – Andreas Feb 04 '17 at 15:45
  • The console always shows "up", after i changed the moveDirection stuff into System.out.println("direction"); – raz0light Feb 04 '17 at 16:09
  • Possible duplicate of [What is a StackOverflowError?](http://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror) – Ole V.V. Feb 04 '17 at 16:19
  • @OleV.V. The poster knows they're using recursion, so I suspect the question is more about *why* stack overflow is occurring and how to avoid it, rather than what is a stack overflow. – pjs Feb 04 '17 at 18:32
  • Yes, @pjs, I think I see what you mean. Still the answers to that other question contain some guidance that might be of help here too. The original asker can best tell. – Ole V.V. Feb 04 '17 at 19:08
  • Its actually that what @OleV.V wrote. I already have the recursion part, because if not, i wouldn't get the Error, right? I want to know, how i can avoid it. – raz0light Feb 04 '17 at 21:06

0 Answers0