Quote
EDIT: Yes, this is all from the Gridworld API. I -hate- Gridworld.
Quote
When it acts, there is a 20% chance it will move towards a PacMan Critter and a 80% chance it will move randomly in ANY direction (which, by the definition of randomness, may include moving towards a PacMan Critter).
Ghosts eat PacMan critters but nothing else.
When a Ghost moves from a location A to a location B, location A should have a pill if it had one before the ghost, or not have a pill if it didn’t have one before the ghost. (Think about using class level Booleans to accomplish this.)
(If your Ghost Critter doesn’t work correctly, your project score will not be higher than an 80.)
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.awt.Color;
import java.util.ArrayList;
/**
*
* @author Zach
*/
public class Ghost extends Critter {
Grid<Actor> gr = getGrid();
private boolean lastMove = true;
private boolean thisMove = true;
public Ghost(Color col) {
setColor(col);
}
@Override
public void act() {
if(getGrid() == null)
return;
ArrayList<Actor> actors = getActors();
processActors(actors);
ArrayList<Location> moveLocs = getMoveLocations();
Location loc = selectMoveLocation(moveLocs);
makeMove(loc);
}
@Override
public void processActors(ArrayList<Actor> actors) {
eatPacMan();
}
public void eatPacMan() {
Location loc = getLocation();
Location loc2;
Actor a;
for(int dir = 0;dir<360;dir+=15) {
loc2 = loc.getAdjacentLocation(dir);
if(gr.isValid(loc2)) {
a = gr.get(loc2);
if(a instanceof PacMan)
a.removeSelfFromGrid();
}
}
}
public ArrayList<Location> getMoveLocation() {
ArrayList<Location> locs = gr.getValidAdjacentLocations(getLocation());
for(int i = locs.size()-1; i>0 ;i++) {
if(!(gr.get(locs.get(i)) instanceof PacMan) &&
!(gr.get(locs.get(i)) instanceof Pill))
locs.remove(i);
}
return locs;
}
@Override
public Location selectMoveLocation(ArrayList<Location> locs) {
int n = locs.size();
Location loc = getLocation();
double r = Math.random();
if(r <= 0.2)
return loc.getAdjacentLocation(loc.getDirectionToward(findPacMan()));
else
return locs.get((int)(Math.random() * n));
}
@Override
public void makeMove(Location loc) {
Location loc2 = getLocation();
if(loc == null)
removeSelfFromGrid();
else {
if(gr.get(loc) instanceof Pill)
thisMove = true;
else
thisMove = false;
moveTo(loc);
if(lastMove == true)
gr.put(loc2, new Pill());
}
lastMove = thisMove;
}
public Location findPacMan() {
Location loc = new Location(0, 0);
ArrayList<Actor> actors = getActors();
for(Actor a: actors) {
if(a instanceof PacMan)
loc = a.getLocation();
}
return loc;
}
}
Error thrown:
Quote
at Ghost.eatPacMan(Ghost.java:47)
at Ghost.processActors(Ghost.java:36)
at Ghost.act(Ghost.java:28)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$4.actionPerformed(GUIController.java:247)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Thrown at:
public void eatPacMan() {
Location loc = getLocation();
Location loc2;
Actor a;
for(int dir = 0;dir<360;dir+=15) {
loc2 = loc.getAdjacentLocation(dir);
if(gr.isValid(loc2)) { //line 47
a = gr.get(loc2);
if(a instanceof PacMan)
a.removeSelfFromGrid();
}
}
}
Requested code:
public boolean isValid(Location loc) {
return 0 <= loc.getCol() && loc.getRow() < getNumRows()
&& 0 <= loc.getCol() && loc.getCol() < getNumCols();
}
//I am not given the source code for the below method
public Location getAdjacentLocation(int direction)
returns the adjacent location in the direction that is closest to direction
Quote
-E getAdjacentLocation(Direction dir) returns a location in direction dir from object E
-E getLocation() returns the location of object E
- getActors() returns an ArrayList<Actor> of all actors on the grid
- getGrid() returns the grid of type Actor which all of the objects lie in
I can provide the source code for all of these methods and any others in this program if needed. Thanks for taking a look.
- Zach
This post has been edited by Simple_Condolences: 22 March 2010 - 07:08 PM

New Topic/Question
Reply



MultiQuote







|