用于创建 UnboundedGrid 并显示它:
ActorWorld world = new ActorWorld(new UnboundedGrid<Actor>());
world.show();
用于创建 BoundedGrid(假设 10x10)并显示它:
ActorWorld world = new ActorWorld(new BoundedGrid<Actor>(10, 10));
world.show();
有什么方法可以创建自定义 UnboundedGrid 吗? 根据习惯,我的意思是其中一个参数(列或行)不是无界的。
我已经检查了 UnboundedGrid 的 GridWorld 源代码,它扩展了实现 Grid 的 AbstactGrid,但我不认为有任何方法可以做到这一点。
最佳答案
最好的想法是通过扩展 AbstractGrid 自己实现它。我建议使用映射数组来映射代表列与参与者的整数,数组的长度代表行数。 (请记住,您不能拥有通用数组,因此您必须使用原始类型。)如下所示:
public class CustomUnboundedGrid<E> extends AbstractGrid<E>
{
private Map[] data;
private int cols;
public CustomUnboundedGrid(int rows, int cols)
{
if(cols <= 0)
throw new IllegalArgumentException();
try
{
data = new Map[rows];
for(int i = 0; i < rows; ++i)
{
data[i] = new HashMap();
}
}
catch(NegativeArraySizeException e)
{
throw new IllegalArgumentException();
}
}
//override methods from AbstractGrid
}
关于java - GridWorld 自定义 UnboundedGrid<Actor>(2,无限),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14427094/