11 Replies - 2717 Views - Last Post: 20 April 2009 - 05:45 PM Rate Topic: -----

#1 Cowjuicer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-December 08

Gridworld does not display.

Posted 10 March 2009 - 05:17 PM

Hello, I am working with the Gridworld Case Study. I have the following code written, and my compiler (I use JCreator) is not alerting me to any errors. It would seem as if this should run smoothly... but once I run the code, the black output box comes up and the cursor blinks at me: the Gridworld GUI is never displayed. I have tried waiting for as long as nearly 20 minutes and nothing comes up (it should be as little as a few seconds, MAYBE 10 at the most). Is this a result of my code or perhaps a unique problem?

Code (a class and a runner):

import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import info.gridworld.actor.Flower;
import info.gridworld.grid.Location;
import info.gridworld.grid.Grid;

class Skipper extends Actor
{
	public boolean canSkip()
	{
		Grid<Actor>gr = getGrid();
		if (gr == null)
			return false;
		Location loc = getLocation();
		Location first = loc.getAdjacentLocation(getDirection());
		if (!gr.isValid(first))
			return false;
		Actor firstNeighbor=gr.get(first);
		if ((firstNeighbor==null) || (firstNeighbor instanceof Flower))
		{
		}
		else
			return false;
		Location last = first.getAdjacentLocation(getDirection());
		if (!gr.isValid(last))
			return false;
		Actor secondNeighbor=gr.get(last);
		return (secondNeighbor==null) || (secondNeighbor instanceof Flower);	
					
	}
	
	public void turnAround()
	{
		setDirection(getDirection()+180);
	}
	
	public void act()
	{
		if(canSkip())
		{
			Location loc = getLocation();
			Location next = loc.getAdjacentLocation(getDirection());
			Location go = next.getAdjacentLocation(getDirection());
			moveTo(go);
		}
		else
		{
			turnAround();
		}
	}
}

public class SkipRunner
{
	public static void main(String[] args)
	{
		ActorWorld world = new ActorWorld();
		world.add(new Skipper());
		world.add(new Rock());
		world.show();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Gridworld does not display.

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Gridworld does not display.

Posted 10 March 2009 - 06:59 PM

Never heard of Gridworld ... you might need to post a link to it if ever somebody wants to help you...
but honnestly there are a lot of "basic" Java questions in this forum I doubt somebody will take the time of downloading a package to be able to answer your question because: this would be kind of a one shut and if the problem is within the package nobody will learn for it.

If I fix a problem in a plain Java JComboBox dozens of other readers may appreciate and benefit from it

about Gridworld ?

May be you can ask the Gridworld forum or try to downsize your problem duplicating the behaviour of GridWorld
Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Gridworld does not display.

Posted 10 March 2009 - 07:12 PM

Do you have the jar in the jdk? gridworld.jar needs to be in the jdk.
Options -> Configure -> JDK Profiles - > select current jdk, hit edit. Browse down and try to find gridworld.jar
If you dont have that you need to add it then it will work
Was This Post Helpful? 1
  • +
  • -

#4 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Gridworld does not display.

Posted 10 March 2009 - 07:28 PM

Gridworld is a topic that they use along with Hello World tutorial to get use to learn the basic methods and how they work. You only work with gridworld about 5 or so times in my AP Comp Sci 1 class, but 25% of the Final is on Gridworld. I can send you the jar for it if you wanna mess with it, its pretty useless though.
Was This Post Helpful? 0
  • +
  • -

#5 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1523
  • View blog
  • Posts: 8,301
  • Joined: 07-September 06

Re: Gridworld does not display.

Posted 10 March 2009 - 07:31 PM

For anyone wanting to download the gridworld jar file here it is (just change extension to .jar):

Attached File  gridworld.txt (97.72K)
Number of downloads: 115

@OP:
Okay, I got it to work:

import info.gridworld.actor.*;
import info.gridworld.grid.*;

public class SkipRunner
{
    public static void main(String[] args)
    {
    	Grid<Actor> g = new BoundedGrid<Actor>(10, 10);
        ActorWorld world = new ActorWorld(g);
        world.add(new Skipper());
        world.add(new Rock());
        world.show();
    }
}


import info.gridworld.actor.*;
import info.gridworld.grid.*;

class Skipper extends Actor
{
    public boolean canSkip()
    {
        Grid<Actor>gr = getGrid();
        if (gr == null)
            return false;
        Location loc = getLocation();
        Location first = loc.getAdjacentLocation(getDirection());
        if (!gr.isValid(first))
            return false;
        Actor firstNeighbor=gr.get(first);
        if ((firstNeighbor==null) || (firstNeighbor instanceof Flower))
        {
        }
        else
            return false;
        Location last = first.getAdjacentLocation(getDirection());
        if (!gr.isValid(last))
            return false;
        Actor secondNeighbor=gr.get(last);
        return (secondNeighbor==null) || (secondNeighbor instanceof Flower);    
                    
    }
    
    public void turnAround()
    {
        setDirection(getDirection()+180);
    }
    
    public void act()
    {
        if(canSkip())
        {
            Location loc = getLocation();
            Location next = loc.getAdjacentLocation(getDirection());
            Location go = next.getAdjacentLocation(getDirection());
            moveTo(go);
        }
        else
        {
            turnAround();
        }
    }
}



Don't forget to add GridWorld to your jars, though as you are using JCreator you shouldn't have to worry about it.

HTH
Was This Post Helpful? 1
  • +
  • -

#6 Cowjuicer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-December 08

Re: Gridworld does not display.

Posted 10 March 2009 - 08:56 PM

Thanks to everyone who replied; I apologize for not including the necessary files and I will make sure to do so next time.

Edit: This does not seem to have fixed the problem but you tried and that's all that matters to me. I'll take another look at it in the morning.

This post has been edited by Cowjuicer: 10 March 2009 - 09:01 PM

Was This Post Helpful? 0
  • +
  • -

#7 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1523
  • View blog
  • Posts: 8,301
  • Joined: 07-September 06

Re: Gridworld does not display.

Posted 11 March 2009 - 06:59 AM

Hm, sorry it didn't work for you. It will probably be something to do with the include path to the gridworld jar file then as the code should be sound.

Might I suggest that you download eclipse? That is what I use so it would be easier for me to help figure out what the problem is, then we can see about porting it back to JCreator.
Was This Post Helpful? 0
  • +
  • -

#8 wolfwithinme   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-April 09

Re: Gridworld does not display.

Posted 18 April 2009 - 08:53 AM

ok i may be new here, but im also doing the GridWorld case study, and I'm at the part of having to develop your own actor. For mine I chose to make mine a 'LaughingMan' that also utilizes another actor called 'Hacked', and works by that whenever a 'Critter' passes the 'LaughingMan', 'LaughingMan' replaces the 'Critter' with the 'Hacked' actor for three steps, then the 'Hacked' actor will remove itself from the grid. I've included the Student Manual, as well as my code for both 'Hacked' and 'LaughingMan', and I'm not asking for the every problem in my code to be solved, I just want to know how to make 'Hacked' and 'LaughingMan' work together.
/**
 * @Hacked.java
 *
 *
 * @Lee Savide
 * @version 1.00 2009/4/17
 */

import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.util.ArrayList;
import java.awt.Color;

/**
 * A 'Hacked' actor is also an actor that doesn't move through it's world, but processes
 * other actors by deleting actors who "eat" 'Bug' actors when they pass the 'LaughingMan' actor. 
 * 'Hacked' actors come when the 'LaughingMan' actor calls to delete the 'Critter' actor.
 * Upon deleting a Critter, the 'LaughingMan' has them replaced by a 'Hacked' actor, which appears as a barcode. <br />
 * The implementation of this class isn't testable on the AP CS A and AB exams.
 */

public class Hacked extends Actor
{
	public Hacked()
	{
	Hacked hack = new Hacked();
	}
	
	public void act()
	{
		if(getGrid == null)
		{
		return;
		}
	ArrayList<Actor> actors = getActors();
	processActors(actors);
	actors.add(hack);
	}
	
	public ArrayList<Actor> getActors()
	{
	return.getgetGrid().getNeighbors(getLocation());
	}
	
	public void processActors(ArrayList<Actor> actors)
	{
	ArrayList<Actor> actors = getActors();
		for(Actor a : actors)
		{
			if(a instanceof Critter)
			{
			a.getOccupiedLocations().removeSelfFromGrid();
			}
		}
	}
	
	public ArrayList<Location> getMoveLocations()
	{
	return getGrid().getEmptyAdjacentLocations(getLocation());
	}
	
	public Location selectMoveLocation(ArrayList<Location> loc)
	{
	int n = loc.size();
		if(n == 0)
		{
		return getLocation();
		}
	int r = (int)(Math.random() * n);
	return loc.get(r);
	}
	
	public void makeMove(Location loc)
	{
	putSelfInGrid(Grid<Critter> gr, Location loc)
	moveTo(loc);
	}
	
	public void selfRemove()
	{
		if(actors.move(3) == true)
		{
		setDirection(getDirection() + Location.HALF_CIRCLE);
		actors.remove(hack);
		}
	}
}


/**
 * @LaughingMan.java
 * 
 * @Lee Savide
 * @version 1.00 2009/4/17
 */

import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Bug;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.util.ArrayList;
import java.awt.Color;

/**
 * A 'LaughingMan' is an actor that doesn't move through its world, but processes
 * other actors by deleting actors who "eat" 'Bug' actors when they pass the 'LaughingMan' actor. 
 * In other words, ChameleonCritters, CrabCritters, and Critters all get destroyed by LaughingMan.
 * Also, upon deleting a Critter, they are replaced by a 'Hacked' actor, which appears as a barcode. <br />
 * The implementation of this class isn't testable on the AP CS A and AB exams.
 */

public class LaughingMan extends Actor
{
	private static final Color DEFAULT_COLOR = Color.BLUE;
	
	public LaughingMan() 
	{
	
	}
	
	public void act()
	{
		if (getGrid() == null)
		{
		return;
		}
	ArrayList<Actor> actors = getActors();
	processActors(actors);
	}
	
	public ArrayList<Actor> getActors()
	{
	return getGrid().getNeighbors(getLocation());
	}
	
	public void processActors(ArrayList<Actor> actors)
	{
		for (Actor a : actors)
		{
			if(!(a instanceof Bug) && !(a instanceof Rock) && !(a instanceof Actor))
			{	
			a.getOccupiedLocations().removeSelfFromGrid();
			}
		}
	}
}


Please give me only the solution to have 'LaughingMan' implement or extend 'Hacked' correctly. i can do the rest on my own. thanks to anyone who can give me support on this.

Also, 'LaughingMan' doesn't move in the grid. It waits for other actors to pass by it's location in any of the 8 squares surrounding it, if that helps.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#9 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1523
  • View blog
  • Posts: 8,301
  • Joined: 07-September 06

Re: Gridworld does not display.

Posted 18 April 2009 - 10:00 AM

I believe your editing needs to be done mostly in the process method. You will need to get the location that the actor is in prior to removing is and then get the grid, and add a Hacked instance to that location.

	public void processActors(ArrayList<Actor> actors)
	{
		for (Actor a : actors)
		{
			if(!(a instanceof Bug) && !(a instanceof Rock) && !(a instanceof Actor))
			{	
			a.getOccupiedLocations().removeSelfFromGrid();
			}
		}
	}


Above is the method that needs to be edited.

I don't believe that LaughingMan needs to implement or extend the Hacked class at all, just needs to know it exists, which is already will as long as it is in the same project.

HTH
Was This Post Helpful? 0
  • +
  • -

#10 wolfwithinme   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-April 09

Re: Gridworld does not display.

Posted 18 April 2009 - 10:59 AM

Thank you for that. ^_^ The rest of the code is fine though, right?
Was This Post Helpful? 0
  • +
  • -

#11 wolfwithinme   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-April 09

Re: Gridworld does not display.

Posted 20 April 2009 - 05:31 PM

Ok, now my code works for those two. But now i have a problem: i don't know how to get a decent Runner program for the two. Could anyone help me please? I've included the 2 classes and the so far failing runner class, but if the problem does lie in the LaughingMan class or the Hacked class, please tell me. The error I got for the runner class was only in the general output, but not the build output, and said this: "Exception in thread "main" java.lang.StackOverflowError at LaughingMan.<init>(LaughingMan.java:34)" then repeated the "at LaughingMan.<init>(LaughingMan.java:34)" statement about a hundred times and then said "Process Completed" but no grid world popped up.
/**
 * @LaughingMan.java
 * 
 * @Lee Savide
 * @version 1.00 2009/4/17
 */

import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.util.ArrayList;
import java.awt.Color;

/**
 * A 'LaughingMan' is an actor that doesn't move through its world, but processes
 * other actors by deleting actors who "eat" 'Bug' actors when they pass the 'LaughingMan' actor. <br />
 * The implementation of this class isn't testable on the AP CS A and AB exams.
 */
 
public class LaughingMan extends Critter
{
	private Hacked hack;
	private Critter critter;
	private Grid<Actor> grid;
	private Location loc;
	private Location next;
	private ArrayList<Location> location;
	private ArrayList<Actor> actors;
	private int direction;
	private static final Color DEFAULT_COLOR = Color.BLUE;
	private Color color;
	
	public LaughingMan() 
	{
	LaughingMan laughingman = new LaughingMan();
	grid = null;
	location = null;
	direction = Location.NORTH;
	color = DEFAULT_COLOR;
	}
	
	public void act()
	{
		if (getGrid() == null)
		{
		return;
		}
	actors = getActors();
	processActors(actors);
	ArrayList<Location> moveLocs = getMoveLocations();
	Location loc = selectMoveLocation(moveLocs);
	makeMove(loc);
	}
	
	public ArrayList<Actor> getActors()
	{
	return getGrid().getNeighbors(getLocation());
	}
	
	public void processActors(ArrayList<Actor> actors)
	{
		for (Actor a : actors)
		{
			if(!(a instanceof Bug) && !(a instanceof Rock) && !(a instanceof Actor))
			{	
			
			}
		}
	}
	
	public ArrayList<Location> getMoveLocations()
	{
	return getGrid().getOccupiedAdjacentLocations(getLocation());
	}
	
	public Location selectMoveLocation(ArrayList<Location> locs)
	{
	int n = locs.size();
		if (n == 0)
		{
		return getLocation();
		}
	int r = (int)(Math.random() * n);
	return locs.get(r);
	}
	
	public void processActors()
	{
	loc = getLocation();
	location = getMoveLocations();
	for(Actor a : actors)
		{
			if(a instanceof Critter)
			{
			grid.getOccupiedAdjacentLocations(loc);
			hack.putSelfInGrid(grid, loc);
			}
		}
	}
}


/**
 * @Hacked.java
 *
 *
 * @Lee Savide
 * @version 1.00 2009/4/17
 */
 
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.util.ArrayList;
import java.awt.Color;

public class Hacked extends Actor
{
	private Hacked hack;
	private Critter critter;
	private ArrayList<Actor> actors; 
	private Grid<Actor> grid;
	private Location location;
	private Location loc;
	private Location next;
	private int direction;
	
	public Hacked(Color color)
	{
	grid = null;
	location = null;
	color = null;
	}
	
	public void act()
	{
		if(getGrid() == null)
		{
		return;
		}
	actors = getActors();
	processActors(actors);
	grid.put(loc, hack);
	}
	
	public ArrayList<Actor> getActors()
	{
	return getGrid().getNeighbors(getLocation());
	}
	
	public void processActors(ArrayList<Actor> actors)
	{
	actors = getActors();
		for(Actor a : actors)
		{
			if(a instanceof Critter)
			{
			
			}
		}
	}
	
	public ArrayList<Location> getMoveLocations()
	{
	return getGrid().getEmptyAdjacentLocations(getLocation());
	}
	
	public Location selectMoveLocation(ArrayList<Location> loc)
	{
	int n = loc.size();
		if(n == 0)
		{
		return getLocation();
		}
	int r = (int)(Math.random() * n);
	return loc.get(r);
	}
	
	public void removeSelfFromGrid()
	{
		if(grid == null)
		{
		throw new IllegalStateException("This actor is not contained in a grid.");
		}
		
		if (grid.get(location) != this)
		{
		throw new IllegalStateException("The grid contains a different actor at location " + location + ".");
		}
	grid.remove(location);
	}
}



And here's the epic fail of a Runner class:
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.awt.Color;

public class LaughingManRunner
{
	public static void main(String[] args)
	{
	ActorWorld world = new ActorWorld();
	LaughingMan warai = new LaughingMan();
	Rock r1 = new Rock();
	Rock r2 = new Rock();
	world.add(new Location (5, 1), r1);
	world.add(new Location (5, 9), r2);
	world.add(new Location (5, 5), warai);
	}
}



By the way, just in case you were wondering, "LaughingMan" refers to my avatar; that's what it's called in the anime Ghost in the Shell. Just a random fact about why it's named that way :D
Was This Post Helpful? 0
  • +
  • -

#12 wolfwithinme   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-April 09

Re: Gridworld does not display.

Posted 20 April 2009 - 05:45 PM

HOLD IT; I got it to work never mind
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1