This site requires JavaScript, please enable it in your browser!
Greenfoot back
TomazVDSN
TomazVDSN wrote ...

2016/2/1

Sokoban scenario

TomazVDSN TomazVDSN

2016/2/1

#
Hi guys, I have been trying to recreate a old game called Sokoban, we have to push boxes around a maze and place them in a defined place to win the level. I was able to get a fragment of the code from other IDE methoddology and recreate the level1 maze. See below the code I used.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Board extends World {

    private final int OFFSET = 30;
    private final int SPACE = 20;

    private Player player;
    private Area area;
    private Wall wall;
    private Block block;

    private String level1 =
        "    ######\n"
        + "    ##   #\n"
        + "    ##$  #\n"
        + "  ####  $##\n"
        + "  ##  $ $ #\n"
        + "#### # ## #   ######\n"
        + "##   # ## #####  ..#\n"
        + "## $  $          ..#\n"
        + "###### ### #@##  ..#\n"
        + "    ##     #########\n"
        + "    ########\n";

    public Board() {
        super(500,300, 1);
        loadLevel1();

    }

    public void loadLevel1() {

        int x = OFFSET;
        int y = OFFSET;

        for (int i = 0; i < level1.length(); i++) {
            char item = level1.charAt(i);

            if (item == '\n') {
                y += SPACE;
                //if (this.w < x) {this.w = x;}

                x = OFFSET;

            } else if (item == '#') {
               
                addObject(new Wall(),x,y);
                x += SPACE;

            } else if (item == '$') {

                addObject(new Block(),x,y);
                x += SPACE;

            } else if (item == '.') {

                addObject(new Area(),x,y);
                x += SPACE;

            } else if (item == '@') {

                addObject(new Player(),x,y);
                x += SPACE;
            } else if (item == ' ') {
                x += SPACE;
            } 

        }
    }

}
It works well while in the Board, however when I transfer the code to a Class under Actor and try to use it, it crashes. Matter of fact Greenfoot application freezes.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Board extends World {
    private ScenarioLoader sl;

    public Board() {
        super(500,300, 1);
        
        ScenarioLoader sl= new ScenarioLoader();
        sl.loadLevel();

    }

}
The Class ScenarioLoader:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class ScenarioLoader extends Actor
{

    private final int OFFSET = 30;
    private final int SPACE = 20;

    private Player player;
    private Area area;
    private Wall wall;
    private Block block;
    
    private String level1 =
            "    ######\n"
            + "    ##   #\n"
            + "    ##$  #\n"
            + "  ####  $##\n"
            + "  ##  $ $ #\n"
            + "#### # ## #   ######\n"
            + "##   # ## #####  ..#\n"
            + "## $  $          ..#\n"
            + "###### ### #@##  ..#\n"
            + "    ##     #########\n"
            + "    ########\n";

    public void act() 
    {
        // Add your action code here.
    }

    public void loadLevel(){
        int x = OFFSET;
        int y = OFFSET;

        for (int i = 0; i < level1.length(); i++) {
            char item = level1.charAt(i);

            if (item == '\n') {
                y += SPACE;
                //if (this.w < x) {this.w = x;}

                x = OFFSET;

            } else if (item == '#') {

                getWorld().addObject(new Wall(),x,y);
                x += SPACE;

            } else if (item == '$') {

                getWorld().addObject(new Block(),x,y);
                x += SPACE;

            } else if (item == '.') {

                getWorld().addObject(new Area(),x,y);
                x += SPACE;

            } else if (item == '@') {

                getWorld().addObject(new Player(),x,y);
                x += SPACE;
            } else if (item == ' ') {
                x += SPACE;
            } 

        }
    }

}

Should I make the ScenarioLoader under a subClass of the Board ? By the way this is the screenshot:
danpost danpost

2016/2/1

#
In your 14-line Board class code, you are declaring a new 's1' variable -- not using the 's1' field declared on line 4. Also, no ScenarioLoader object is being added into the world; so, because you are calling 'loadLevel' without the actor being in the world 'getWorld', within the method, will return 'null' and calling any method on a 'null' reference will cause a 'NullPointerException' (see lines 47, 52 57 and 62 of the ScenarioLoader class). Line 47 will be where the execution will stop due to '#' being the first character to attempt to add an object in the world.
Should I make the ScenarioLoader under a subClass of the Board ?
No. Also, I do not think it should be a subclass of Actor either. I think it should be an abstract class under java.lang.Object. You can pass the world object to the ScenarioLoader object when you call the 'loadLevel' method. As a suggestion, it would be easier to have the world gridded using large cells, creating your world with something like 'super(30, 20, 20)'. You can scale your actor images to suite the size of the cells.
TomazVDSN TomazVDSN

2016/2/1

#
danpost, I will try to understand your explanation by parts. 1) 14-line Board class code Let me know if this is what you meant.. if I have the declaration
4 : private ScenarioLoader sL;
I should not declare it again, so I fixed line 8 below
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Board extends World {
    private ScenarioLoader sl;
 
    public Board() {
        super(500,300, 1);
        sL= new ScenarioLoader();
        sL.loadLevel();
    }
}
TomazVDSN TomazVDSN

2016/2/1

#
2) To load ScenarioLoader in the World. I thought by just declaring it under Board it would be part of the World. Clearly by your comments this is not the case, and I don't know how to load a Class or Object in the World. Would yo give me a hand and tell me what i should do to load this Object in the World ? 3) I will have to learn what is a abstract class and how to create one, is the abstract class the one that is hanging without extends any other class ? 4) I am working on the Scaling the images and resize the Board. Thanks, Tomaz
danpost danpost

2016/2/1

#
TomazVDSN wrote...
danpost, I will try to understand your explanation by parts. 1) 14-line Board class code < Fix Omitted >
Correct (provided 'sL' was a typo and should be 's1' as declared -- on both lines 8 and 9).
TomazVDSN TomazVDSN

2016/2/1

#
.
danpost danpost

2016/2/2

#
TomazVDSN wrote...
2) To load ScenarioLoader in the World. I thought by just declaring it under Board it would be part of the World.
Declaring a field of type ScenarioLoader and assigning a ScenarioLoader object to it will indeed give the world a scenario loader; but, it does not have to be actually in the world (although, as the ScenarioLoader class is coded, it will need to be actually added into the world for the 'loadLevel' method to execute. The problem is that the use of 'getWorld' in the 'loadLevel' method requires that the ScenarioLoader object be an Actor object and that it be added into the world with the 'addObject' method. However, a ScenarioLoader object does not need to be an Actor object -- it would not be visually important in the world and does not interact with any other actors in the world (it just makes no sense in having it declared to be of type Actor or adding it into the world). You can create one from your Board class with:
s1 = new ScenarioLoader(this);
and change the constructor to accept the world with:
public ScenarioLoader(World world)
Within the constructor, you will need to store the world in a previously declared field:
// declaration (outside constructor)
private World world;

// storing world (from within constructor)
this.world = world;
Then, instead of using 'getWorld()' -- use 'world'. As such, you can declare the class with:
public class ScenarioLoader
By default, since what it extends is not given, it will extend the java.lang.Object class. It could just as well be declared with:
public class ScenarioLoader extends java.lang.Object
All this is rather important, but not in this way. I would like to steer you in a direction where you would have a better structure for what you are trying to do -- like have an abstract Level class that your world would grab the level data from and build the world from that. Unfortunately, I do not have any examples of this on the site (I did create a Sokoban scenario that duplicates one on the site -- but, did not want to totally duplicate someone else's scenario on the site -- even with slight modifications). I had a Levels class that contained an array of data for representing the level data. It contained methods for getting specific level data and the level number. The method for actually using the level data to add the actors into the world was in my one subclass of World.
danpost danpost

2016/2/2

#
TomazVDSN wrote...
3) I will have to learn what is a abstract class and how to create one, is the abstract class the one that is hanging without extends any other class ?
The java tutorial page on abstract methods and classes should have the information you need. At the bottom of the page there is a statement about the type of methods that can be used in an abstract class. Since all the level data is constant (static final), it seems pointless to creates object from the class. If you did not make the class abstract, any and all objects created from the class would be exactly the same. Even a non-final static field can be used for the current level of the project since its value will be universal in nature (you will only need one field in your project to hold which level is active). Be aware, however, that non-final static fields need to be initially set by code that is executed before the actual running of the project (usually in the constructor of the initial world).
TomazVDSN TomazVDSN

2016/2/4

#
danpost, Thank you ! I tried your suggestions and it worked.
import greenfoot.*; 

public class Board extends World {
    private ScenarioLoader sL;

    public Board() {
        super(500,300, 1);
        
        sL = new ScenarioLoader(this);
        sL.loadLevel();
    }
}
And here the fragment of the ScenarioLoder
import greenfoot.*; 
public class ScenarioLoader extends java.lang.Object
{

    private final int OFFSET = 30;
    private final int SPACE = 20;

    private World world;
    private Player player;
    private Area area;
    private Wall wall;
    private Block block;
    private String level1 =
        "    ######\n"
        + "    ##   #\n"
        + "    ##$  #\n"
        + "  ####  $##\n"
        + "  ##  $ $ #\n"
        + "#### # ## #   ######\n"
        + "##   # ## #####  ..#\n"
        + "## $  $          ..#\n"
        + "###### ### #@##  ..#\n"
        + "    ##     #########\n"
        + "    ########\n";
        
    public ScenarioLoader(World world){
        this.world = world;
    }
TomazVDSN TomazVDSN

2016/2/4

#
Following the same reasoning, I took the string scenariolevel and put in a new class called ScenarioLevel, declared it inside ScenarioLoader and pass the string through the method getScenarioLevel. see the code below.
import java.util.*;

public class ScenarioLevel
{

    private String[] scenarioLevel = new String[11];

    public ScenarioLevel ()
    {
        
    }
    
    public String getScenarioLevel(int levelNumber)
    {
        scenarioLevel[1] = 
          "    ######\n"
        + "    ##   #\n"
        + "    ##$  #\n"
        + "  ####  $##\n"
        + "  ##  $ $ #\n"
        + "#### # ## #   ######\n"
        + "##   # ## #####  ..#\n"
        + "## $  $          ..#\n"
        + "###### ### #@##  ..#\n"
        + "    ##     #########\n"
        + "    ########\n";

        scenarioLevel[2] = "##";
        scenarioLevel[3] = "###";
        scenarioLevel[4] = "####";
        scenarioLevel[5] = "#####";
        scenarioLevel[6] = "##### #";
        scenarioLevel[7] = "##### ##";
        scenarioLevel[8] = "##### ###";
        scenarioLevel[9] = "##### ####";
        scenarioLevel[10] = "##### ######";

        return scenarioLevel[levelNumber];
    }
}
Now a have a ScenarioLoader class independent of the ScenarioLevel class, where ScenarioLevel pass the strings to the ScenarioLoader and Scenario Loader put everything in the Board.
danpost danpost

2016/2/4

#
The data can be declared outside the methods and can be made to be constant. Also, counting in java starts at zero; so, it is best to store the level value internally starting at zero. When displaying the value externally to the user, then display one more than its value.:
public class ScenarioLevel
{
    private static final String[] scenarioLevel =
    {
        // level 0
        "    ######\n" +
        "    ##   #\n" +
        "    ##$  #\n" +
        "  ####  $##\n" +
        "  ##  $ $ #\n" +
        "#### # ## #   ######\n" +
        "##   # ## #####  ..#\n" +
        "## $  $          ..#\n" +
        "###### ### #@##  ..#\n" +
        "    ##     #########\n" +
        "    ########\n",
        // level 1
        "##",
        // level 2
        "###",
        // level 3
        "####",
        // level 4
        "#####",
        // level 5
        "##### #",
        // level 6
        "##### ##",
        // level 7
        "##### ###",
        // level 8
        "##### ####",
        // level 9
        "##### ######"
    };
 
    public String getScenarioLevel(int levelNumber)
    {
        return scenarioLevel[levelNumber];
    }
}
TomazVDSN TomazVDSN

2016/2/5

#
danpost, It cleans up things a lot ! - Let me try this as well.
You need to login to post a reply.