Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.util.ArrayList;
- import info.gridworld.grid.Grid;
- import info.gridworld.grid.Location;
- import info.gridworld.actor.*;
- public class RockHopper extends Bug
- {
- private Location moveLoc;
- public RockHopper()
- {
- this.setColor(Color.RED);
- this.setDirection(Location.EAST);
- }
- public void act()
- {
- if (canMove())
- move();
- else
- spawnRock();
- }
- public void move()
- {
- Grid<Actor> gr = getGrid();
- if(gr == null)
- return;
- Location loc = getLocation();
- if(gr.isValid(moveLoc))
- moveTo(moveLoc);
- else
- removeSelfFromGrid();
- Flower flower = new Flower(Color.RED);
- flower.putSelfInGrid(gr, loc);
- }
- public boolean canMove()
- {
- Grid<Actor> gr = getGrid();
- if (gr == null)
- return false;
- Location loc = getLocation();
- int myCol = loc.getCol();
- int rows = gr.getNumRows();
- int cols = gr.getNumCols();
- int r=0;
- Location jumpLoc;
- if(myCol == gr.getNumCols() -1)
- {
- setDirection(Location.WEST);
- r = (int) (Math.random() * rows);
- jumpLoc = new Location(r, loc.getCol());
- moveTo(jumpLoc);
- }
- if (myCol == 0 && getDirection() == Location.WEST)
- {
- setDirection(Location.EAST);
- }
- Location NE, E, SE;
- NE = loc.getAdjacentLocation(getDirection() + Location.HALF_LEFT);
- E = loc.getAdjacentLocation(getDirection());
- SE = loc.getAdjacentLocation(getDirection() + Location.HALF_RIGHT);
- Actor neighborNE,neighborE, neighborSE;
- neighborNE = gr.get(NE);
- neighborE = gr.get(E);
- neighborSE = gr.get(SE);
- if(neighborE instanceof Rock)
- {
- if(!gr.isValid(E))
- return false;
- moveLoc = E;
- return true;
- }
- else if(neighborNE instanceof Rock)
- {
- if(!gr.isValid(NE))
- return false;
- moveLoc = NE;
- return true;
- }
- else if(neighborSE instanceof Rock)
- {
- if(!gr.isValid(SE))
- return false;
- moveLoc = SE;
- return true;
- }
- return false;
- }
- private void spawnRock()
- {
- Grid<Actor> gr = getGrid();
- if(gr == null)
- return;
- Location loc = getLocation();
- ArrayList <Location> L = new ArrayList<Location>();
- Location next;
- Actor N;
- int i,j;
- for (i = 0; i < gr.getNumRows(); i++)
- for (j = 0; j < gr.getNumCols(); j++)
- {
- next = new Location(i,j);
- N = gr.get(next);
- if(!(N instanceof Rock) && !next.equals(loc))
- {
- L.add(next);
- }
- }
- i = 0;
- int p;
- Rock rock;
- while (i < 3 && i < L.size())
- {
- p = (int) (Math.random() * L.size());
- rock = new Rock();
- rock.putSelfInGrid(gr, L.get(p));
- L.remove(p);
- i++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement