Gridworld killAll

Just need help with a class that kills everythingingrid and replaces w

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 7495 Views - Last Post: 16 June 2009 - 10:58 PM Rate Topic: -----

#1 xirex7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-June 09

Gridworld killAll

Post icon  Posted 16 June 2009 - 06:28 PM

HI, working with gridworld, not my forte because I don't know the methods and the hierarchy that well. Anyways, working on kind of an atomic bomb critter which when placed in the grid, takes out every actor in the grid and replaces it with a fire critter. The class compiles, but doesn't run well:
Here's the error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: This actor is not in a grid.
at info.gridworld.actor.Actor.moveTo(Actor.java:158)
at kidnapCritter.makeMove(kidnapCritter.java:51)
at kidnapCritter.teleport(kidnapCritter.java:40)
at kidnapCritter.act(kidnapCritter.java:16)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at info.gridworld.actor.Critter.getMoveLocations(Critter.java:90)
at info.gridworld.actor.Critter.act(Critter.java:44)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at info.gridworld.actor.Critter.getMoveLocations(Critter.java:90)
at info.gridworld.actor.Critter.act(Critter.java:44)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at info.gridworld.grid.BoundedGrid.isValid(BoundedGrid.java:62)
at info.gridworld.grid.BoundedGrid.get(BoundedGrid.java:87)
at info.gridworld.actor.Actor.putSelfInGrid(Actor.java:121)
at atomicBomb.killAll(atomicBomb.java:28)
at atomicBomb.processActors(atomicBomb.java:14)
at info.gridworld.actor.Critter.act(Critter.java:43)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
and here's the code:

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


public class atomicBomb extends Critter
{
	private Location loc;
	public void processActors(ArrayList<Actor> actors)
	{
	   killAll(actors);
	}
	
   public void makeMove(Location loc)
	{

	}
   public void killAll(ArrayList<Actor> actorsx)
		{ 
				for(Actor m : actorsx)
				{
					
					m.removeSelfFromGrid();
					fire flame = new fire();
					flame.putSelfInGrid(getGrid(), loc);
				}   
		 }   
}



Is This A Good Question/Topic? 0
  • +

Replies To: Gridworld killAll

#2 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

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

Re: Gridworld killAll

Posted 16 June 2009 - 06:40 PM

Try saving the location then adding it, not all will go to the same location.
for(Actor m : actorsx)
{
Location old = m.getLocation();
m.removeSelfFromGrid();
fire flame - new fire();
flame.putSelfInGrid(getGrid(), old);
}


By getting the location of the actor, it means it will never be out of bounds because it was already in the grid in the first place.
Was This Post Helpful? 0
  • +
  • -

#3 xirex7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-June 09

Re: Gridworld killAll

Posted 16 June 2009 - 07:13 PM

Thanks a lot! I kind of figured that part out after I posted, but I can't get the critter to make it so that ire replaces everything in the grid. I'm going to check the appendix I found online for any methods I could use. If you have any suggestions in the meantime, much appreciated. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

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

Re: Gridworld killAll

Posted 16 June 2009 - 07:35 PM

Well I suppose it would get actors that are fire once you get started. When it is getting the actors, make a loop to cycle through the arraylist, if it is an instanceof fire, remove it from the arraylist so you dont travel to previous locations you have already traveled to. Did you want to do it one actor at a time or turns all actors to fire with one blow?
Was This Post Helpful? 0
  • +
  • -

#5 xirex7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-June 09

Re: Gridworld killAll

Posted 16 June 2009 - 07:38 PM

To answer quicky, one blow is what I want. But all I can do is get it so that it sets only the neighboring actors on fire. One blow, everything. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

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

Re: Gridworld killAll

Posted 16 June 2009 - 07:48 PM

ArrayList<Location> actorLocs = getOccupiedLocations();
Grid g = getGrid();
for(int i = 0; i<actorLocs.size(); i++)
{
g.get(actorLocs.get(i)).removeSelfFromGrid(); //dont think you need it but for keep sakes add it, not sure if it will just override it.
fire flame = new fire();
flame.putSelfInGrid(g, actorLocs.get(i));
}


Something like that?

This post has been edited by Fuzzyness: 16 June 2009 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#7 xirex7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-June 09

Re: Gridworld killAll

Posted 16 June 2009 - 07:54 PM

Thanks, didn't think of that, but I did use getOccupied Locations() earlier and it gives me a compile-time error saying that it can't find the symbol. I believe I imported the correct stuff:
import info.gridworld.actor.Critter;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;
import java.util.ArrayList;
import java.awt.Color;
import info.gridworld.grid.Grid;

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

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

Re: Gridworld killAll

Posted 16 June 2009 - 08:01 PM

Thats is what I was afraid of... I ran into same problem when I was learning grid world. You can't use all the locations class methods, forgot the reaosn my teacher told me why. Well here is what to do then:

1. Make an ArrayList of Actors
2. Make a variable for the max # columns
3. Make a variable for the max # rows
4. Make a loop to cycle through all of the rows
5. Make a loop inside the previous one to cycle through all the columns
6. Inside the nested loop, make a new location with the coordinates row,columns
7. Get what is at the location if != null, add it into the arraylist of actors
8. Use code we made from previous post to turn them all into fire!!!!

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

#9 xirex7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-June 09

Re: Gridworld killAll

Posted 16 June 2009 - 08:04 PM

wait, nevermind, stupid me: I just did this:
Grid g = getGrid();
ArrayList<Location> actorLocs = g.getOccupiedLocations();

and that part compiled, but it's not recognizing removeSelfFromGrid()
Everything is right, but I have no clue?


oops, didn't see the post! thanks, I'll try that!
So what your suggesting I do is create my own getOccupiedLocations
Thanks! I'll try it
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

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

Re: Gridworld killAll

Posted 16 June 2009 - 08:06 PM

Take it out and compile it. I believe if you add a Object ontop of another object in Gridworld, it will take the most previous one. Suppose you could say that it "eats" it.


Dont dismiss this just yet, if you can do it in less code then lets try. Post the code you have so far for this method so I can see what im working with. brb ( run out to get food gimme 10 min)

This post has been edited by Fuzzyness: 16 June 2009 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

#11 xirex7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-June 09

Re: Gridworld killAll

Posted 16 June 2009 - 08:16 PM

I'm getting a pretty bad error message. Here it is:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: This actor is not in a grid.
at info.gridworld.actor.Actor.moveTo(Actor.java:158)
at kidnapCritter.makeMove(kidnapCritter.java:51)
at kidnapCritter.teleport(kidnapCritter.java:40)
at kidnapCritter.act(kidnapCritter.java:16)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at info.gridworld.actor.Critter.getMoveLocations(Critter.java:90)
at info.gridworld.actor.Critter.act(Critter.java:44)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at info.gridworld.actor.Critter.getMoveLocations(Critter.java:90)
at info.gridworld.actor.Critter.act(Critter.java:44)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at info.gridworld.grid.BoundedGrid.isValid(BoundedGrid.java:62)
at info.gridworld.grid.BoundedGrid.get(BoundedGrid.java:87)
at info.gridworld.actor.Actor.putSelfInGrid(Actor.java:121)
at atomicBomb.killAll(atomicBomb.java:28)
at atomicBomb.processActors(atomicBomb.java:14)
at info.gridworld.actor.Critter.act(Critter.java:43)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at info.gridworld.actor.Critter.getMoveLocations(Critter.java:90)
at info.gridworld.actor.Critter.act(Critter.java:44)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at info.gridworld.actor.Critter.getMoveLocations(Critter.java:90)
at info.gridworld.actor.Critter.act(Critter.java:44)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
And here's what I have so far; the actual project is HUGE (I commented out the idea you gave me, so take a look at that, but it compiles with run-time errors due to instability with the code that is not commented out:
import info.gridworld.actor.Critter;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;
import java.util.ArrayList;
import java.awt.Color;
import info.gridworld.grid.Grid;


public class atomicBomb extends Critter
{
	private Location loc;
	public void processActors(ArrayList<Actor> actors)
	{
	   killAll(actors);
	}
	
   public void makeMove(Location loc)
	{

	}
   public void killAll(ArrayList<Actor> actorsx)
		{ 

			Grid g = getGrid();
			ArrayList<Location> actorLocs = g.getOccupiedLocations();
			for(int i = 0; i<actorLocs.size(); i++)
			{
				fire flame = new fire();
				flame.putSelfInGrid(g, actorLocs.get(i));
			}


		 } 
   // public ArrayList<Location> getVictims()
   // {
	   // ArrayList<Location> theLocations = new ArrayList<Location>();

		// Look at all grid locations.
	   // for (int r = 0; r < getNumRows(); r++)
		//{
			//for (int c = 0; c < getNumCols(); c++)
			//{
			  //  Location loc = new Location(r, c);
			  //  if (get(loc) != null)
			//		theLocations.add(loc);
			//}
		//}
	//}
}


Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

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

Re: Gridworld killAll

Posted 16 June 2009 - 08:51 PM

Well, May be a littl ebit more code but I think the nested loops should be what you use now. Dont really wanna deal with all of those issues, sheesh I loathe Gridworld!
Was This Post Helpful? 0
  • +
  • -

#13 xirex7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-June 09

Re: Gridworld killAll

Posted 16 June 2009 - 08:58 PM

pt. taken. I h8 gridworld with every fiber of my body. I'll figure something out and' probably post something later tonight or in the morning. Thanks for all of your help. Really appreciate it! You helped me more than my teacher could have. He seriously knows nothing about gridworld; this is his first year teaching AP Computerscience A.
Thanks again!
Was This Post Helpful? 0
  • +
  • -

#14 xirex7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-June 09

Re: Gridworld killAll

Posted 16 June 2009 - 10:10 PM

If your still up, here's an update. doesn't work, but maybe you can still help me out. I get a critter not in grid error. Check out the coding:
import info.gridworld.actor.Critter;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;
import java.util.ArrayList;
import java.awt.Color;
import info.gridworld.grid.Grid;


public class atomicBomb extends Critter
{
	private Location loc;
	public void processActors(ArrayList<Actor> actors)
	{
	   killAll(actors);
	}
	
   public void makeMove(Location loc)
	{

	}
   public void killAll(ArrayList<Actor> actorsx)
		{ 
		ArrayList<Location> theLocations = new ArrayList<Location>();
		for (int r = 0; r < getGrid().getNumRows(); r++)
		{
			for (int c = 0; c < getGrid().getNumCols(); c++)
			{
				Location loc = new Location(r, c);
				if (getGrid().get(loc) != null)
			   theLocations.add(loc);
			}
		}
			for( Location x : theLocations)
			{
				putSelfInGrid(getGrid(), x);
			} 
		}
   
}


Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

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

Re: Gridworld killAll

Posted 16 June 2009 - 10:15 PM

then do a is valid call in the loop before adding it. not sure why it is getting that. I really cant remember if a location is row, column or column, row. think it is row column. Maye want to switch those and see.

if(getGrid().isValid(loc) && getGrid().get(loc) != null)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2