0

私は APCS にいて、クリッターがグリッド内のアクターをランダムに選択してからその場所に移動するようにするクリッター クラスの拡張を作成する必要があります。したがって、それを「フォロー」します。私の先生は、変更できない最初のクラスとメソッドの名前を最初に教えてくれました。こちらが変更不可のランナーです。

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

 }
}

AnnoyingCritter は、ランダムに選択したアクターの場所に移動し、邪魔になる花や岩を食べなければなりません。また、一度に 1 スペースずつアクターの位置に移動する必要があります。迷惑な生き物にアクターがどこにいるかを見つけて、ランダムに見つける方法がわかりません。インポートも変更できません。

私はこれに何時間も立ち往生しているので助けてください。

4

2 に答える 2

0

Location クラスには強力なメソッドがいくつかあります。これを試して:

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

これにより、一度に 1 スペースずつ BFF に向かって移動する必要があります。

BFF を見つけるには:

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

locList(0) が自分のクリッターではないことを確認してください。

クイック リファレンス ガイドも読んでみてください。

于 2014-02-01T01:19:34.743 に答える