Gridworld anyone?

A few problems I'm Having with Gridworld

Page 1 of 1

5 Replies - 20881 Views - Last Post: 07 August 2009 - 10:33 AM Rate Topic: -----

#1 jab0015  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-October 08

Gridworld anyone?

Posted 10 February 2009 - 10:02 PM

Hey guys, I am building a new class called Jumper - which extends the Actor class
If you have no idea what gridworld is...
GridWorld is the case study for the 2008 AP CS exam (and beyond). GridWorld uses an engaging environment that allows students to create and test actors with a wide variety of behaviors.
The Official website is http://www.collegebo...sci_a/case.html

Anyway on to the actual program.
My Jumper is supposed to be able to do certain things
-Jump 2 spaces in the grid
- If it runs into any other actor besides another jumper it should eat it
- If it runs into another Jumper then just ignore it.
- If location two cells in front of jumper is out of grid, move ONE cell forward then turn 90 degress. then move ONE cell forward, else repeat 90 degrees until he can move and then step one cell ahead.
- If location one cell ahead of the Jumper is a rock, wait for two tries then crush the rock.

I think I have everything except the last two, If you have any knowledge in gridworld I would love some advice, Here is my code

import info.gridworld.actor.*;

import info.gridworld.grid.*;
import java.awt.*;


public class Jumper extends Actor
{
  public Jumper(Color jumperColor)
  {
	setColor(jumperColor);
  }
  
  public void act()
  {
	if (canMove())
	{ move();
	  move();}
	else
	  turn();
  }
  public boolean canMove()
  {
	Grid<Actor> gr = getGrid();
	if (gr == null)
	  return false;
	Location loc = getLocation();
	Location next = loc.getAdjacentLocation(getDirection());
	if (!gr.isValid(next))
	  return false;
	Actor neighbor = gr.get(next);
	return (neighbor == null) || (neighbor instanceof Jumper) || (neighbor instanceof Flower);
  }
  public void move()
  {
	Grid<Actor> gr = getGrid();
	if (gr == null)
	  return;
	Location loc = getLocation();
	Location next = loc.getAdjacentLocation(getDirection());
	if (gr.isValid(next))
	  moveTo(next);
	else
	  turn();
  }
  public void turn()
  {
	setDirection(getDirection() + Location.RIGHT);
  }
}



Also I will include the runner I use

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

import java.awt.Color;

/**
 * This class runs a world that contains jumper. <br />
 * This class is not tested on the AP CS A and AB exams.
 */
public class JumperRunner
{
	public static void main(String[] args)
	{
		ActorWorld world = new ActorWorld();
		Jumper alice = new Jumper(Color.BLUE);
		Jumper bob = new Jumper(Color.GREEN);
		world.add(new Bug());
		world.add(new Rock());
		world.add(new Flower(Color.RED));
		//world.add(new Location(4, 5), Flower);
		world.add(new Location(4, 3), alice);
		world.add(new Location(5, 5), bob);
		world.show();
	}
}



Is This A Good Question/Topic? 1

Replies To: Gridworld anyone?

#2 wolfwithinme  Icon User is offline

  • New D.I.C Head

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

Re: Gridworld anyone?

Posted 30 April 2009 - 06:36 PM

Umm hi, im making a new GridWorld actor called 'LaughingMan' and for some reason i get these errors:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at LaughingMan.processActors(LaughingMan.java:62)
at LaughingMan.act(LaughingMan.java:108)...

(the rest of the errors are irrelevant)
Also note that 'LaughingMan' leaves a 'Hacked' actor in the place of another critter or chameleoncritter, and a new LaughingMan in the place of a Jumper or CrabCritter. kudos to the guy above for making the Jumper, since I based the 'next' location off of the Jumper, but made changes to only include certain actors within the parameters of that location, so thank you. ^_^
Could some one point me in the right direction of what the heck I'm doing wrong here?
heres my code:
LaughingMan.java
/**
 * @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 LaughingMan warai;
	private Hacked hack;
	private Grid<Actor> grid;
	private ArrayList<Actor> actors;
	private ArrayList<Location> location;
	private ArrayList<Location> hackLocs;
	private Location next;
	private Location loc;
	private static final int direction = Location.NORTH;
	private static final Color DEFAULT_COLOR = Color.BLUE;
	private Color c1;
	
	/**
	 * Default 'LaughingMan', colored blue & facing north in the grid.
	 */
	public LaughingMan()
	{
	getGrid();
	getLocation();
	location = new ArrayList<Location>();
	location.add(getLocation());
	setDirection(direction);
	setColor(c1);
	}
	
	/**
	 * 'LaughingMan' will take other actors and 
	 * make them into new 'LaughingMan' actors when
	 * they enter any of the 8 locations around the 
	 * 'LaughingMan', or it will replace the actor 
	 * with a 'Hacked' actor. If not, 'LaughingMan'
	 * leaves the actor alone.
	 */
	@Override
	public void processActors(ArrayList<Actor> actors)
	{
	hack = new Hacked();
	grid = getGrid();
	loc = getLocation();
	hackLocs = getMoveLocations(loc);
	actors = getActors();
		for(Actor a : actors)
		{
			if((a instanceof CrabCritter) || (a instanceof Jumper))
			{
			next = a.getLocation();
			a.removeSelfFromGrid();
			actors.add(warai);
			putSelfInGrid(grid, next);
			}
			
			else if((a instanceof ChameleonCritter) || (a instanceof Critter))
			{
			next = a.getLocation();
			hack = new Hacked();
			actors.add(hack);
			a.removeSelfFromGrid();
			hack.putSelfInGrid(grid, next);
			}
			
			else if(a instanceof LaughingMan)
			{
			//Do nothing.
			}
			
			else if((a instanceof Flower) || (a instanceof Hacked) || (a instanceof Bug) || (a instanceof Rock))
			{
			//Do nothing.
			}
			
			else
			{
			// Do nothing.
			}
		}
	}
	
	/**
	 * When 'LaughingMan' acts, it will identify the other actors & their locations,
	 * then select one randomly and either destroy that actor with a 'Hacked' actor, or leave it alone.
	 */
	public void act()
	{
		if(getGrid() == null)
		{
		return;
		}
	processActors(actors);
	}

	/**
	 * Identifies the locations that a 'LaughingMan' can access.
	 * @return the other actors within the 8 locations
	 * surrounding the 'LaughingMan'.
	 */
	public ArrayList<Location> getMoveLocations(Location loc)
	{
	return getGrid().getOccupiedAdjacentLocations(getLocation());
	}
	
	/**
	 * Identifies the other actors in the grid.
	 * @return the other actors in the grid
	 */
	public ArrayList<Actor> getActors()
	{
	return getGrid().getNeighbors(getLocation());
	}
	
	/**
	 * Prints info about a 'LaughingMan' to the screen.
	 * @return the class name and motto of 'LaughingMan'.
	 */
	public String toString()
	{
	return getClass().getName() + " I Thought What I'd Do Was, I'd Pretend I Was One Of Those Deaf-Mutes";
	}
}


Hacked.java
/**
 * @Hacked.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;


public class Hacked extends Actor
{
	private Hacked hack;
	private Grid<Actor> grid;
	private ArrayList<Actor> actors;
	private ArrayList<Location> location;
	private ArrayList<Location> hackLocs;
	private Location loc;
	private Location next;
	private int numSteps;
	private int sideLength;
	private int length;
	private Color c1;
	private static final int direction = Location.NORTH;
	
	/**
	 * Default 'Hacked', facing north in the grid.
	 */
	public Hacked()
	{
	location = new ArrayList<Location>();
	getGrid();
	getLocation();
	setDirection(direction);
	c1 = null;
	}
	
	/**
	 * 'Hacked' proccesses actors by identifying the other actors,
	 * and then acting if 'canHack()' is true.
	 */
	public void process(ArrayList<Actor> actors)
	{
	grid = getGrid();
	loc = getLocation();
	actors = getActors();
	removeSelf();
	}
	
	/**
	 * A 'Hacked' actor acts by deleting
	 * itself after taking a single step.
	 */
	public void act()
	{
		if(getGrid() == null)
		{
		return;
		}
	actors = getActors();
	process(actors);
	}
	
	/**
	 * Indicates when 'Hacked' needs to remove itself.
	 */
	public void removeSelf()
	{
	length = 1;
	sideLength = length;
		for(numSteps = 0; numSteps < sideLength; numSteps++)
		{
		removeSelfFromGrid();
		}
	}
	
	/**
	 * Identifies the other actors in the grid.
	 * @return the other actors and their locations.
	 */
	public ArrayList<Actor> getActors()
	{
	return getGrid().getNeighbors(getLocation());
	}
	
	public ArrayList<Location> getMoveLocations()
	{
	hackLocs = getGrid().getOccupiedAdjacentLocations(getLocation());
	return hackLocs;
	}
}


Please also note that 'Hacked' is working exactly as i want it to, but if you see any bugs or unneeded code, please tell me so i can get it to run as efficiently as possible. Thank you!
-wolfwithinme


Also, Sorry if i forgot some of the comments. I figured to only add comments to the methods i know are definitively working, but I never got around to adding them all. sorry! -_-
Was This Post Helpful? 0
  • +
  • -

#3 wolfwithinme  Icon User is offline

  • New D.I.C Head

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

Re: Gridworld anyone?

Posted 30 April 2009 - 06:50 PM

Hmm I just found out something about 'LaughingMan' : it is replacing the Critter and ChameleonCritter actors with 'Hacked' actors properly, so that's a good sign so far. but its still getting run-time errors. Also, after replacing the CrabCritter and Jumper actors with new LaughingMan actors, they replace themselves with actors, so I'm confused why it's replacing them when i programmed it to do nothing upon finding instances of LaughingMan actors. Any help here?

This post has been edited by wolfwithinme: 01 May 2009 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 wolfwithinme  Icon User is offline

  • New D.I.C Head

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

Re: Gridworld anyone?

Posted 02 May 2009 - 07:47 PM

Never mind; the whole thing is working as intended. Both actors are implemented correctly.
Was This Post Helpful? 0
  • +
  • -

#5 Rekmesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 15-September 08

Re: Gridworld anyone?

Posted 03 May 2009 - 12:25 AM

First, just calling move() two times in the act() method will not allow you to complete some of the criteria of the Jumper Actor such as doing a specific action if an actor is in the 2nd spot. You will need another method that can determine the location two spaces ahead of the Jumper and then check if its valid, contains other actors, etc. Check the location class and the Grid<E> class and you can find some methods that will allow you to do this.

http://www.horstmann...dworld/javadoc/

Hope this helps! :)
Was This Post Helpful? 0
  • +
  • -

#6 CSDragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 07-August 09

Re: Gridworld anyone?

Posted 07 August 2009 - 10:33 AM

Gridworld is incredibly fun to modify, and you can go much farther than what you have. My APCompsci classmates used gridworld re-tools and hacks for our final. You can make some truly incredible things like a Fire Emblem game simulator (what I did) that plays through clicking, not by the run button, or Tetris, or any number of things.
You can download mine if you want, it's really cool.

The big thing we learned was it is that if you really want to do anything to gridworld, you must unpack it. that means putting every gridworld java and class file in one folder, and removing all imports to eachoter (but not imports to things like javax.swing)

Happy coding.

Edit: Err, remove "Fortress.java" first if you download it. It shouldn't have been in there.

This post has been edited by CSDragon: 07 August 2009 - 10:51 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1