2013-02-10 92 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)是不是你自己的小動物。

請嘗試閱讀快速參考指南。