Last active
November 15, 2018 03:33
-
-
Save George3/6966711 to your computer and use it in GitHub Desktop.
Yahoo! Answers homework help for question: "Desperate java gridworld issues, help!!!?"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import info.gridworld.actor.Actor; | |
import info.gridworld.actor.ActorWorld; | |
import info.gridworld.grid.Grid; | |
import info.gridworld.grid.Location; | |
import java.awt.Color; | |
/** | |
* Yahoo! Answers homework help for question: "Desperate java gridworld issues, help!!!?" | |
* http://answers.yahoo.com/question/index?qid=20131010175243AAMJlkA | |
* ( Official & newer unofficial GridWorld library jar: http://horstmann.com/gridworld/ ) | |
* | |
* Question: | |
* "So i have to make an Actor that will teleport when i call the method. | |
* However, | |
* 1.) i can't make the default constructor | |
* 2.) I keep getting a Null Pointer error. | |
* It complies, but doesn't run as intended. Please help. My code is below:" | |
* | |
* @author wrestler | |
* | |
*/ | |
public class TeleporterActor extends Actor { | |
Location loc = getLocation(); | |
// FIXME: If getGrid() called here, gr will be null when act() called. | |
// See how Bug.java calls getGrid() right before calling | |
// move(), etc. since putSelfInGrid() has been called by then. | |
Grid<Actor> gr = getGrid(); | |
// make a default constructor that sets the direction to WEST | |
public void TeleporterActor() { // TODO: Remove "void" to make this a default constructor. | |
} | |
// make an initialization constructor that receives a direction | |
public TeleporterActor(int dir) { | |
int dir1 = dir; | |
dir1 = getDirection(); | |
} | |
private Location getRandomLocation() { | |
// Is this what you had? Yahoo! Answers cut off the second parameter's "Math...". | |
Location loc1 = new Location((int) (Math.random() * 7) | |
,(int) (Math.random() * 7)); | |
return loc1; | |
} | |
// the act method will move this actor to a random location | |
// as long as that location is valid and empty | |
/* FIXME: See "getGrid()" hint above Clicking on "Step" or "Run" button causes this NPE: | |
* | |
* Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException | |
* at TeleporterActor.act(TeleporterActor.java:63) | |
* at info.gridworld.actor.ActorWorld.step(ActorWorld.java:69) | |
* ...etc. | |
*/ | |
public void act() { | |
if (gr.isValid(getRandomLocation())) { | |
moveTo(getRandomLocation()); | |
} else { | |
moveTo(getLocation()); | |
} | |
} | |
public Color getColor() { | |
return null; | |
} | |
/** | |
* Program starting point: Based on main() in GridWorld's initial "BugRunner.java" example. | |
* @param args | |
* @throws InterruptedException | |
*/ | |
public static void main(String[] args) throws InterruptedException { | |
ActorWorld world = new ActorWorld(); | |
world.add(new TeleporterActor(Location.WEST)); | |
world.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need the answers