Part3
Set 3
-
loc1.getRow();
-
false
-
(4,4)
-
135度
-
getAdjacentLocation方法返回沿着给定方向旋转之后最接近的相邻cell的坐标(行、列)位置
Set 4
-
Grid g; int using = g.getOccupiedLocations().size();该方法返回grid对象中所有被占用的位置数量。 int number = g.getNumRows()*g.getNumCols()计算grid中所有的位置数量。 int empty = number-using;即可获得所有未被占用的位置数量。
-
Grid g; g.isValid(new location(10,10));如果在grid中,则返回true,否则返回false
-
Grid是一个接口,在java中接口声明的方法可以由其它类继承并实现,Grid的方法实现可以在AbstractGrid 、 BoundeGrid、UnboundeGrid 类中找到。BoundeGrid、UnboundeGrid 类继承了AbstractGrid抽象类,并且实现了AbstractGrid类中未实现的所有方法。
-
在ArrayList返回返回多个对象会更好,因为如果使用数组来返回对象,我们就需要更多的开销来记录对象的数量,位置等信息,声明不同大小的数组来进行记录,加大了工作量。而ArrayList的使用则不需要考虑大小的的问题。
Set 5
-
一个Actor拥有color、direction、location三个属性,还有一个和grid的接口。
-
初始化的color是blue,初始化的方向是north。
-
Actor既有状态又有行为,是一个可以实例化的对象,而接口不允许被声明实例对象,也不允许实现具体的方法。
//(a):
//一个actor不能将它本身放入grid两次,如果放入两次,会抛出IllegalStateException异常。
//example:修改BugRunner.java文件
public class BugRunner{
public static void main(String[] args){
ActorWorld w = new ActorWorld();
//create Bug
Bug b = new Bug();
//放入两次
w.add(b);
b.putSelfInGrid(b.getGrid(),b.getLocation());
w.show();
}
}
//(b)
//一个已经被移除grid的actor不能被再次移出
//example:修改BugRunner.java文件
public class BugRunner{
public static void main(String[] args){
ActorWorld w = new ActorWorld();
//create Bug
Bug b = new Bug();
//放入两次
w.add(b);
w.show();
b.removeSelfFromGrid();
b.removeSelfFromGrid();
}
}
//(c)
//可以,一个actor如果被移出grid,它可以再次将自己放回grid中
//example:修改BugRunner.java文件
public class BugRunner{
public static void main(String[] args){
ActorWorld w = new ActorWorld();
//create Bug
Bug b = new Bug();
Bug temp = b.getGrid();
//放入两次
w.add(b);
w.show();
b.removeSelfFromGrid();
b.putSelfInGrid(temp,new Location(3,3));
}
}
-
通过两种途径实现
setDirection(getDirection()+Location.RIGHT); setDirection(getDirection()+90);
Set 6
-
在GridWorld/framework/info/gridworld/actor/Bug.java文件中
-
同上
public boolean canMove() { Grid<Actor> gr = getGrid(); if (gr == null) return false; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); //下面的if语句保证了Bug不会走出边界。 if (!gr.isValid(next)) return false; Actor neighbor = gr.get(next); //返回语句的判断条件保证Bug不会碰撞到rock return (neighbor == null) || (neighbor instanceof Flower); // ok to move into empty location or onto flower // not ok to move onto any other actor } }
-
在GridWorld/framework/info/gridworld/grid/Grid.java文件中
boolean isValid(Location loc); E get(Location loc); //上面两个方法被canMove方法调用,因为这两个方法确定Bug移动到的下一个位置是正确的(无其它actor,未越过grid的边界)。
-
在GridWorld/framework/info/gridworld/grid/Location.java
public Location getAdjacentLocation(int direction); //该方法由Bug调用来查找下一个可能的位置。
-
在GridWorld/framework/info/gridworld/actor/Actor.java
public int getDirection(); public Grid<Actor> getGrid(); public Location getLocation(); //以上三个方法由canMove方法继承,确定一个Actor的location,direction,grid
-
会将自己移出grid。
if (gr.isValid(next)) moveTo(next); else removeSelfFromGrid();
-
loc作为变量保存一个Bug移动前的location,也就是行和列的位置,该变量可以用来在Bug移动之前的位置插入flower对象。
-
public class Flower extends Actor
可以看到Flower类继承了Actor类,并可以调用Actor的getColor方法,获得Actor实例对应的颜色,因此Bug和flower的颜色是相同的。 -
不会,如果只调用removeSelfFromGrid方法,不会在之前的位置留下花。当在Bug类的move方法中调用该removeSelfFromGrid方法,会在之前位置留下一朵花。在Actor.java中可以看到该方法的具体实现:
//在Actor.java文件,不会 public void removeSelfFromGrid() { if (grid == null) throw new IllegalStateException( "This actor is not contained in a grid."); if (grid.get(location) != this) throw new IllegalStateException( "The grid contains a different actor at location " + location + "."); grid.remove(location); grid = null; location = null; } //在Bug.java文件,会 if (gr.isValid(next)) moveTo(next); else removeSelfFromGrid(); Flower flower = new Flower(getColor()); flower.putSelfInGrid(gr, loc);
-
在Bug.java文件:
//loc未Bug移动之前的location Flower flower = new Flower(getColor()); flower.putSelfInGrid(gr, loc);
-
调用一次turn方法会旋转45度,需要连续调用4次。