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

2014/11/23

save and load

wc0708 wc0708

2014/11/23

#
I write the game with save the location of the last capture of chess.
public void save()
    {
        String save = "";
        Actor[] allChess =  GWorld.getAllObjects("Chess");

        if ( allChess != null )
        {
            for ( int i=0; i<allChess.length; i++)
            {
                Chess ch = (Chess) allChess[i];
                save = save + ch.getX() + " " + ch.getY();
                save = save + "\n";
            }
         }
       }
    
I just know how to save it. But what is the code of void load()? Who can help??
danpost danpost

2014/11/23

#
I am a bit confused with the code given. If you are saving just the coordinates of the pieces, then there must be an arrangement of how they are placed in the string (or in the array that 'getAllObjects' returns). If that is the case, then the array should never be 'null'. If it is not the case, then I cannot see how one can determine the order of coordinate pairs in the string as far as which piece would go at each location. Please explain.
wc0708 wc0708

2014/11/23

#
let me modify something
// Get all chess pieces.
Actor[] allChess =GWorld.getAllObjects("Chess");


//save the chess pieces
if ( allChess != null)
  {
     for(int i=0; i<allChess.length; i++)
     {
       chess ch = (Chess) allChess[i];
       // Save the ith piece of chess here...


    }
 }
It is the hint it given. The code I write is just a guess.
danpost danpost

2014/11/23

#
Please post the 'getAllObjects' method of the GWorld class and if there are any fields used in it that are declared within the GWorld class, please show how those fields are declared and assigned values.
wc0708 wc0708

2014/11/23

#
Actually there are four classes
    String save;
    public void save()
    {  
     save = "";
     Actor[] allTrap = GWorld.getAllObjects("Trap");
     Actor[] allWater = GWorld.getAllObjects("Water");
     Actor[] allCave = GWorld.getAllObjects("Cave");
     Actor[] allChess = GWorld.getAllObjects("Chess");
     
    
     for(int i=0; i<allTrap.length; i++)  
     {    
        Trap t = (Trap)allTrap[i];   
        save = save + t.getX()  +" "+ t.getY() + " " + "Trap" ;
        save = save+"\n"  ;  
     }   
       for(int i=0; i<allWater.length; i++)  
     {    
        Water w = (Water)allWater[i];   
        save = save + w.getX()  +" "+ w.getY() + " " + "Water" ;
        save = save+"\n"  ;  
     }
       for(int i=0; i<allCave.length; i++)  
     {    
        Cave c = (Cave)allCave[i];   
        save = save + c.getX()  +" "+ c.getY() + " " + "Cave" ;
        save = save+"\n"  ;  
     }
       for(int i=0; i<allChess.length; i++)  
     {    
        Chess ch = (Chess)allChess[i];   
        save = save + ch.getX()  +" "+ ch.getY() + " " + "Chess" ;
        save = save+"\n"  ;  
     }
     
     System.out.println(save);

    }
And it return to the coordinates of current location 2 8 Trap 4 8 Trap 3 7 Trap 2 0 Trap 3 1 Trap 4 0 Trap 1 3 Water 1 4 Water 1 5 Water 2 3 Water 2 4 Water 2 5 Water 4 3 Water 4 4 Water 4 5 Water 5 3 Water 5 4 Water 5 5 Water 3 0 Cave 3 8 Cave 0 8 Chess 6 8 Chess 6 6 Chess 1 7 Chess 5 7 Chess 2 6 Chess 4 6 Chess 0 6 Chess 6 0 Chess 0 0 Chess 0 2 Chess 5 1 Chess 1 1 Chess 4 2 Chess 2 2 Chess 6 2 Chess Now I need to reset it and the board become empty. What should I write in void load() to get the image of the saved board?
danpost danpost

2014/11/23

#
Will this data be saved in a file and then read back in? or just stored in the 'save' field and parsed?
wc0708 wc0708

2014/11/23

#
just stored in the 'save' field and parsed
danpost danpost

2014/11/23

#
Since it appears you only have one object on any one location at a time, it would be easier to use one string where the position in the string determines the position in the grid. Then, you only need use one character to represent the type of object and the space character for an empty location to build the string (I used the last character in the name of the actor type since they were all different).
String save;

public void save()
{
    save = "";
    for (int x=0; x<getWorld().getWidth(); x++)
    {
        for (int y=0; y<getWorld().getHeight(); y++)
        {
            char c = ' ';
            if (!getWorld().getObjectsAt(x, y, null).isEmpty())
            {
                Object obj = getWorld().getObjectsAt(x, y, null).get(0);
                if (obj instanceof Chess) c = 's';
                if (obj instanceof Water) c = 'r';
                if (obj instanceof Trap) c = 'p';
                if (obj instanceof Cave) c = 'e';
            }
            save += c;
        }
    }
}

public void load()
{
    getWorld().removeObjects(getWorld().getObjects(null));
    for (int n=0; n<save.length(); n++)
    {
        char c = save.charAt(n);
        if (c == ' ') continue;
        int x = n%9;
        int y = n/9;
        Actor actor = null;
        if (c == 's') actor = new Chess();
        if (c == 'r') actor = new Water();
        if (c == 'p') actor = new Trap();
        if (c == 'e') actor = new Cave();
        if (actor != null) getWorld().addObject(actor, x, y);
    }
}
If these method are located in the world class, remove all 'getWorld().' occurrences.
You need to login to post a reply.