hello! i was just wondering if there was a way too loop through an array to return objects. Like for example i am working on my AI right now and i want it to automatically turn and go after the closest point but im not sure how to go about this. My gird is set up as an array and i want to loops through it to find the closest point this is what i have in the map class so far:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class map here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class map extends World
{
private int grid_;
public final int POINT = 1;
public final int EMPTY = 2;
/**
* Constructor for objects of class map.
*
*/
public map()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(30, 30, 20);
grid_ = new int;
//make all array indices = EMPTY
//put 5 POINTS in the board initially
initialize();
}
public void addPoint(int r, int c)
{
Points p = new Points();
addObject(p,r,c);
grid_ = POINT;
}
public void makeEmpty(int r, int c)
{
List<Points> l = getObjectsAt(r,c,Points.class);
removeObjects(l);
grid_ = EMPTY;
}
public void act()
{
if(Greenfoot.getRandomNumber(100) == 0)//1 in 100 acts
{
int rows = Greenfoot.getRandomNumber(getHeight());
int col = Greenfoot.getRandomNumber(getWidth());
addPoint(rows, col);
}
}
//add 5 points intially
private void initialize()
{
for(int i = 0; i < 5; i++)
{
Points P = new Points();
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(P,x,y);
}
}
}