首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AnnoyingCritter GridWorld案例研究

AnnoyingCritter GridWorld案例研究
EN

Stack Overflow用户
提问于 2013-02-10 12:45:50
回答 2查看 2.8K关注 0票数 0

我在APCS中,我必须对critter类进行扩展,让critter随机选择网格中的任何角色,然后移动到它的位置;从而“跟随”它。我的老师给了我们开始的类和方法的名字,一开始是不能改变的。这是不能更改的runner。

代码语言:javascript
复制
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类必须包含生物找到它的“最好朋友”和“跟随它”所需的所有方法。

代码语言:javascript
复制
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必须移动到它随机选择的角色所在的位置,并吃掉挡路的鲜花和石头。它还需要一次一个空间地移动到参与者的位置。我想不出如何让这只讨厌的生物找到演员的位置,然后再随机找到一个。导入也不能更改。

请帮帮忙,因为我已经被困在这个问题上好几个小时了。

EN

回答 2

Stack Overflow用户

发布于 2013-04-22 20:10:56

我非常确定您必须使用getLocation()来查找角色在哪里。或者..。您可以使用循环并为列和行选取随机数,直到找到参与者为止。糟了,这听起来是个好主意。我要试一下。

票数 0
EN

Stack Overflow用户

发布于 2014-02-01 09:19:35

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

代码语言:javascript
复制
int direction=getLocation().getDirectionToward(BFF.getLocation);
moveTo(getLocation().getAdjacentLocation(direction));

这应该会让你一次一个空间地接近最好的朋友。

要找到最好的朋友:

代码语言:javascript
复制
Grid<Actor> grid=getGrid();
ArrayList<Location> locList=grid.getOccupiedLocations();
BFF=grid.get(locList.get(0));

只要确保locList(0)不是您自己的生物即可。

也可以尝试阅读快速参考指南。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14794794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档