1 Replies - 7065 Views - Last Post: 29 May 2012 - 07:03 PM Rate Topic: -----

#1 TheRealMobius  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 29-May 12

Saving and Loading the state of a grid

Posted 29 May 2012 - 01:46 PM

Hello humans!

I'm working on a Gridworld project that involves being capable of storing actors within a grid of a fixed size (7x12) so that they can be later restored into the world when a trigger is set off.

I'm having a little bit of trouble doing this, as I THINK that I have the idea of a system to use to load and save the grids, but for some reason, I can't seem to get it to load the data I want it too into the world itself.

I'd asking if anyone could possibly take a look at what I have so far, and point out anything you think might be causing the issue. I've been looking at it for a while and nothing in particular is coming to me.

I do apologize if this ends up with me missing something extremely obvious, but I'm not seeing what's the issue (at least at the moment)

I have 3 classes that work together for this:
  • a World() [runs the world and current grid]

  • an abstract Room() [storing actors in grids *template*]

  • the actual Room1() [the first of several, holds the default data for the room and future data]


World.java
import info.gridworld.actor.*;
import java.util.ArrayList;
import info.gridworld.grid.*;

import java.awt.KeyboardFocusManager;
import java.awt.KeyEventDispatcher;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;

public class World
{
    public static ArrayList<Room> rooms = new ArrayList<Room>();
    public static ActorWorld world = new ActorWorld(new BoundedGrid(7,12));
    public static Link hero = new Link();
    public static Room cRoom;
    public static Actor[][] objs = new Actor[7][12];

    public static void main(String args[])
    {
        // Get rooms
        getRooms();
        // load Room1
        cRoom = rooms.get(0);
        objs = cRoom.getRoomData();
        loadRoom();
        // Add Keyboard Input here
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher(){
                public boolean dispatchKeyEvent(KeyEvent event)
                {
                    String key = KeyStroke.getKeyStrokeForEvent(event).toString();
                    if (key.equals("pressed UP")){
                        hero.move("up");}
                    if (key.equals("pressed RIGHT")){
                        hero.move("right");}
                    if (key.equals("pressed DOWN")){
                        hero.move("up");}
                    if (key.equals("pressed LEFT")){
                        hero.move("up");}
                    if (key.equals("pressed z")){
                        hero.attack("sword");}
                    return false;
                }
            });
        world.show();
    }

    private static void getRooms()
    {
        rooms.add(new Room1());
    }

    public static void loadRoom(){
        for (int r = 0; r > 7; r++)
        {
            for (int c = 0; c > 12; c++)
            {
                Location targ = new Location(r,c);
                world.add(targ,new Rock());
            }
        } 
    }

    public static void saveRoom(){
        for (int r = 0; r > 7; r++)
        {
            for (int c = 0; c > 12; c++)
            {
                Location targ = new Location(r,c);
                objs[r][c] = world.getGrid().get(targ);
            }
        }
    }

    public static void blackScreen(){
        for (int r = 0; r > 7; r++)
        {
            for (int c = 0; c > 12; c++)
            {
                Location targ = new Location(r,c);
                world.add(targ,new Actor());
            }
        }
    }
    
    public static ArrayList<Room> test(){
        return rooms;
    }
}



Room.java
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.util.ArrayList;

public abstract class Room
{
    // The data of the room, 
    Actor[][] objs = new Actor[7][12];

    public Room(){
    }

    abstract public void setupRoom();

    public Actor[][] getRoomData()
    {
        return objs;
    }
}



Room1.java
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.util.ArrayList;

public class Room1 extends Room
{
    public Room1()
    {
            super();
        setupRoom();
    }
    
    public void setupRoom()
    {
        //  This would server as the Room's border (generic)
        for (int r = 0; r < 7; r++){
            for (int c = 0; c < 12; c++){
                objs[r][0] = new Rock();
               objs[r][11] = new Rock();
               objs[0][c] = new Rock();
               objs[6][c] = new Rock();
            }
        }
        objs[3][5] = new Actor(); 
    }
}



I realize that save does nothing atm, that will be implemented later

What it SHOULD do is, main() method in World() would first call getRooms(). getRooms() creates a new instance of Room1 that will be added to the ArrayList rooms. When instantiated, Room1 should set the default data for the room in the actor array[][] objs. Once rooms has the data, main() gets Room1 and stores it in cRoom, and then gets the objs array[][] from the room. The loadRoom() method is then called, which should get the data from objs, and add an actor to a location corresponding with the index. *Skip keyboard input, that's for something else* the world should then be displayed.

My issue is the nothing is loaded in the end. It seems that the program will not take the data from thee subclass, or even world, as i attempted to add things directly from World().

I will note that if i put a statement such as...
world.add(new Location(4,2),new Rock());

in main(), it will be loaded in the grid.

Any help would be greatly appreciated, and speed help would be marvelous (as I have ~10 days to finish this)

I do thank you for taking the time to read this, and hope you have a great day.

P.S. (I REALLY hope the error isn't obvious >.>)

Is This A Good Question/Topic? 0
  • +

Replies To: Saving and Loading the state of a grid

#2 TheRealMobius  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 29-May 12

Re: Saving and Loading the state of a grid

Posted 29 May 2012 - 07:03 PM

Well, once again, I'm an idiot, seems that the error is that I used a < rather than > in the for loops in world.java. Logical Errors, so obvious, but so hard to find -_-'

I'm sorry for wasting space on the forums with my dumb question, although thanks for stopping by.

Hopefully future posts are more helpful to myself and others >.>
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1