GridWorld Program - Spontaneous Error

Error that appears occasionally

Page 1 of 1

2 Replies - 3648 Views - Last Post: 12 December 2013 - 02:36 PM Rate Topic: -----

#1 Rekmesh  Icon User is offline

  • New D.I.C Head

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

GridWorld Program - Spontaneous Error

Post icon  Posted 13 February 2009 - 08:02 PM

Hello all

I'm working on a extra side project that uses the GridWorld case study and I'm getting an error that appears occasionally and I don't know how to fix it.

Here's the error message:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: The grid contains a different actor at location (8, 1).
	at info.gridworld.actor.Actor.moveTo(Actor.java:160)
	at TeleportationCritter.makeMove(TeleportationCritter.java:60)
	at TeleportationCritter.teleport(TeleportationCritter.java:45)
	at TeleportationCritter.act(TeleportationCritter.java:24)
	at info.gridworld.actor.ActorWorld.step(ActorWorld.java:69)
	at info.gridworld.gui.GUIController.step(GUIController.java:134)
	at info.gridworld.gui.GUIController$2.actionPerformed(GUIController.java:109)
	at javax.swing.Timer.fireActionPerformed(Unknown Source)
	at javax.swing.Timer$DoPostEvent.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)




Here is my code:

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

/*
 *TeleportationCritter "teleports" to a random location in the grid
 * 
 * If the space is occupied by another actor, the actor is put in the original place of the TeleportationCritter
 *
 */

public class TeleportationCritter extends Critter
{
  Location nextTele;
  public TeleportationCritter()
  {
	setColor(Color.GREEN);
  }
  
  public void act()
  {
	teleport(); 
  }
  
  //Teleports critter to random spot
  public void teleport()
  {
	Boolean state = false;
	Random ran = new Random();
	
	while (state == false)
	{
	  
	  int row = ran.nextInt(getGrid().getNumRows());
	  int col = ran.nextInt(getGrid().getNumCols());
	  
	  nextTele = new Location(row,col);
	  
	  if (!(getGrid().isValid(nextTele) == false))
		state = true;
	}
	
	makeMove(nextTele);
  }
  
  //Moves actor to new destination. If location is occupied, remove the actor
  public void makeMove(Location loc)
  {
	if (getGrid().get(loc) != null)
	{
	  Actor occupant = getGrid().get(loc);
	  Location originalLoc = getLocation();
	  System.out.println("Critter found at " + occupant.getLocation());
	  occupant.removeSelfFromGrid();
	  moveTo(loc);
	  occupant.putSelfInGrid(getGrid(), originalLoc);
	  occupant.setColor(getColor());
	  System.out.println("Critter Moved");
	}
	else
	  moveTo(loc);
  }
}



Can anyone help?
Thanks in advance

This post has been edited by Rekmesh: 14 February 2009 - 05:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: GridWorld Program - Spontaneous Error

#2 Rekmesh  Icon User is offline

  • New D.I.C Head

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

Re: GridWorld Program - Spontaneous Error

Posted 14 February 2009 - 05:28 PM

Still having problems! Can anyone help me?
Was This Post Helpful? 0
  • +
  • -

#3 Atdhe Bekteshi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-December 13

Re: GridWorld Program - Spontaneous Error

Posted 12 December 2013 - 02:36 PM

Dude the code you provied didn't give me any errors, basically the critter is teleporting but it randomly dissapers.
Besides that you can't change method act() and also you don't to call teleport, you can have that as a critter method.
Check out my code:
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.util.Random;
import java.awt.Color;
import java.util.ArrayList;
public class TeleportCritter extends Critter
{
  Location nextTeleport;
  public TeleportCritter()
  {
    super();
  }
  public Location selectMoveLocation(ArrayList<Location> locs) {
	Boolean currentState = false;
	Random ran = new Random();
	
	while (currentState == false)
	{
	  
	  int row = ran.nextInt(getGrid().getNumRows());
	  int col = ran.nextInt(getGrid().getNumCols());
	  
	  nextTeleport = new Location(row,col);
	  
	  if (!(getGrid().isValid(nextTeleport) == false))
		currentState = true;
	}
	
	return nextTeleport;
  }
  public void makeMove(Location loc)
  {
	if (getGrid().get(loc) != null)
	{
	  Actor occupant = getGrid().get(loc);
	  
	  Location originalLoc = getLocation();
	  
	  occupant.removeSelfFromGrid();
	  
	  moveTo(loc);
	  
	  occupant.putSelfInGrid(getGrid(), originalLoc);
	  
	  occupant.setColor(getColor());
	}
	else
	  moveTo(loc);
  }
}


Hope this helps and thank you for posting this.

This post has been edited by macosxnerd101: 12 December 2013 - 02:39 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1