2013-02-10 90 views
0

我在APCS,我必须对生物类进行扩展,使生物随机选择网格中的任何角色,然后移动到其位置;从而“跟随”它。我的老师给了我们开头的课程名称和方法名称,以便不能更改。这是不能改变的跑步者。AnnoyingCritter GridWorld案例研究

import java.awt.Color; 

import info.gridworld.actor.Rock; 
import info.gridworld.actor.Actor; 
import info.gridworld.actor.Flower; 
import info.gridworld.actor.Bug; 
import info.gridworld.grid.Location; 
import info.gridworld.grid.BoundedGrid; 
import info.gridworld.actor.ActorWorld; 

public class AnnoyingCritterRunner 
{ 
public static void main(String[] args) 
{ 
    ActorWorld world = new ActorWorld(new BoundedGrid<Actor>(8,8)); 
    world.add(new Location(1, 1), new AnnoyingCritter()); 
    world.add(new Location(3, 1), new Rock()); 
    world.add(new Location(5, 2), new Actor()); 
    world.add(new Location(7, 6), new Flower()); 
    world.add(new Location(6, 6), new Actor()); 
    world.add(new Location(0, 5), new Actor()); 
    world.add(new Location(2, 6), new Bug(Color.GREEN)); 
    world.add(new Location(3, 5), new Actor()); 
    world.show(); 
} 
} 

的AnnoyingCritter类必须包含所有找到的“最好的朋友”和“顺藤摸瓜”所需的小动物的方法。

import info.gridworld.actor.Actor; 
import info.gridworld.actor.Critter; 
import info.gridworld.grid.Location; 
import java.awt.Color; 

import java.util.ArrayList; 

public class AnnoyingCritter extends Critter 
{ 
/* instance variables will be needed 
    * one for the bFF and one to set whether this Critter has a bFF (a boolean) 
    */ 

    public AnnoyingCritter() 
    { 
    /* make an AnnoyingCritter constructor that sets a color and starts with the 
    * boolean variable above being "false" 
    */ 
    Critter AnnoyingCritter = new Critter(); 
    AnnoyingCritter.setColor(Color.green); 

    } 

    private void pickBFF() 
{ 
    /* you'll need the grid, occupied locations, and some randomness to pick a friend 
    */ 

} 

public void processActors(ArrayList<Actor> actors) 
{ 
    /* this can be simple or complicated. the idea is to pick a BFF if 
    * one is needed 
    */ 
    if(!hasFriend) 
    { 

    } 
//and it eats flowers and rocks 
    for(Actor dude : actors) 
    { 

    } 
} 

public Location selectMoveLocation(ArrayList<Location> locs) 
{ 
    //you need a Grid 

    //you need a location 

    //you need to know where to go and if it's clear to move and then go there 

} 
} 

这个恼人的罪犯必须移动到它随机选择的演员的位置,并吃掉那些在路上的花和岩石。它还需要一次移动到一个空间的演员位置。我无法弄清楚如何让烦人的小动物找到演员的位置,然后随机找到一个。进口也不能改变。

请帮忙,因为我一直坚持了几个小时。

回答

0

我很确定你必须使用getLocation()来查找actor的位置。或者...您可以使用循环并为列和行选取随机数,直到找到演员。拍摄,这听起来像个好主意。我要去尝试一下。

0

Location类有一些强大的方法。试试这个:

int direction=getLocation().getDirectionToward(BFF.getLocation); 
moveTo(getLocation().getAdjacentLocation(direction)); 

这应该让你朝BFF的方向发展一次。

要找到一个BFF:

Grid<Actor> grid=getGrid(); 
ArrayList<Location> locList=grid.getOccupiedLocations(); 
BFF=grid.get(locList.get(0)); 

只要确保locList(0)是不是你自己的小动物。

请尝试阅读快速参考指南。