1 Replies - 5541 Views - Last Post: 11 March 2011 - 02:13 PM Rate Topic: -----

#1 conztantines  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-March 11

gridworld

Posted 10 March 2011 - 08:28 PM

trying to create a flea. A flea will move like a normal bug but on every tenth move it will jump to an available random spot in the grid. But i'm stuck

import info.gridworld.actor.Bug;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import java.awt.Color;
import java.util.*;
import info.gridworld.grid.Location;
import info.gridworld.grid.Grid;

public class fleaBug extends Bug
{
public fleaBug()
{
setColor(Color.BLUE);
}

public fleaBug(Color bugColor)
{
setColor(bugColor);
}

public void act()
{
if (canMove())
move();

else 
turn();
}

private int ct = 0;
public void move()
{
Grid<Actor> gr = getGrid();
if(move() >= 10)
ct++;
Location spot = new Location(randomRow, randomCol);
if(gr == null)
return;
Location loc = spot;
Location next = spot.getAdjacentLocation(getDirection());
if(gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
public boolean canMove()
{
Grid<Actor> gr = getGrid();
randomRow = (int)(Math.random()*gr.getNumRows());
randomCol = (int)(Math.random()*gr.getNumCols());
Location spot = new Location(randomRow, randomCol);
if(gr == null)
return false;
Location next = spot.getAdjacentLocation(getDirection());
if(!gr.isValid(next))
return false;
Actor neighbor = gr.get(next);
return (neighbor == null) || ( neighbor instanceof Flower);
}
}


This post has been edited by macosxnerd101: 10 March 2011 - 08:33 PM
Reason for edit:: Added code tags and moved to GridWorld


Is This A Good Question/Topic? 0
  • +

Replies To: gridworld

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: gridworld

Posted 11 March 2011 - 02:13 PM

First, please properly indent your code so others can read it. Second, what specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1