/** * JumperBug.java 01/17/14 * * @author - Mrs. Juarez * @author - Period 8 * @author - Id 212345|Ursuline Academy of Dallas * * @author - Honorbound: I received help from ... * */ /* * AP(r) Computer Science GridWorld Case Study: * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com) * * This code is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation. * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * @author Cay Horstmann * @author Chris Nevison * @author Barbara Cloud Wells */ import java.awt.Color; import info.gridworld.actor.Actor; import info.gridworld.actor.Flower; import info.gridworld.actor.Rock; import info.gridworld.grid.Grid; import info.gridworld.grid.Location; /** * A JumperBug */ public class JumperBugW extends Actor { /** * Constructs a Jumper bug that traces a square of a given side length * @param length the side length */ public JumperBugW() { setColor(Color.PINK); } public JumperBugW(Color juColor) { setColor(juColor); } /** * Moves to the next location of the square. */ public void act() { if(canJump()) jump(); else turn(); } public void turn() { setDirection(getDirection()+Location.HALF_RIGHT); //setDirection(getDirection()); } public void jump() { Grid<Actor> gr=getGrid(); if(gr==null){ return; } Location loc=getLocation(); Location next=loc.getAdjacentLocation(getDirection()); Actor neighbor = gr.get(next); if(neighbor == null||neighbor instanceof Flower){ if(gr.isValid(next)) moveTo(next); } else { Location twoAway = next.getAdjacentLocation(getDirection()); //Actor twoneighbor = gr.get(twoAway); if (neighbor instanceof Rock) { if (gr.isValid(twoAway)) moveTo(twoAway); } else removeSelfFromGrid(); } } public boolean canJump() { 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); if (neighbor == null|| (neighbor instanceof Flower) ) { return true; }else if(neighbor instanceof Rock){ Location twoAway = next.getAdjacentLocation(getDirection()); if (!gr.isValid(twoAway)) return false; return true; } return false; // Location twoAway = next.getAdjacentLocation(getDirection()); // if (!gr.isValid(twoAway)) // return false; // neighbor = gr.get(twoAway); // return (neighbor == null) || (neighbor instanceof Flower); } }